Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 feedbackSuccess 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
Elevator as a State Machine
00:00
5 left

Elevator as a State Machine

HardPython

Problem

Code an elevator as a state machine. Asked in the final round technical interview stage. OOD/implementation.

Implement a single-elevator simulator that processes events one at a time and updates the elevator's state. The elevator starts at floor 0 with doors closed and can move one floor per tick.

Function contract

def step_elevator(state, event):

  • state is a dict with keys: floor, direction, doors_open, targets, moving.
  • event is one of: {"type": "call", "floor": int}, {"type": "select", "floor": int}, or {"type": "tick"}.
  • Return the updated state dict.

Rules

  1. call and select add a target floor if it is not already present.
  2. On tick, if doors are open, close them first and do not move.
  3. If doors are closed and there is a target above or below, move one floor toward the nearest target in the current direction, or choose a direction if idle.
  4. When the elevator reaches a target floor, open the doors and remove that floor from targets.
  5. If no targets remain, the elevator becomes idle with direction = "idle".

Return the full updated state after each event.

Constraints

  • 0 <= floor <= 100
  • Targets are integers in the same floor range
  • The targets list may contain duplicates
  • Each event is one of call, select, or tick
  • The elevator moves at most one floor per tick

Function Signature

def step_elevator(state, event):
Interviewer

Your question is Elevator as a State Machine. 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.