Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
O(1) List Pop
00:00
5 left

O(1) List Pop

EasyPython

Problem

Bloomberg Law may maintain in-memory work queues where processing order is unimportant. Implement a function that removes and returns the item at a specified index in O(1) time.

Because shifting elements would require linear time, preserve the complexity requirement by replacing the removed item with the final list element, then removing the final slot. This operation does not preserve the original order of the remaining items.

Formal Specification

Implement pop_constant_time(items, index):

  • items is a non-empty Python list of values.
  • index is a valid integer index from 0 through len(items) - 1.
  • Mutate items in place.
  • Return the value originally stored at items[index].
  • The order of the remaining values may change.
  • The operation must run in O(1) time and use O(1) additional space.

Constraints

  • 1 <= len(items) <= 10^5
  • 0 <= index < len(items)
  • Values may be any Python object
  • The order of remaining items may change

Function Signature

def pop_constant_time(items, index):
Interviewer

Your question is O(1) List Pop. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.