Bird
0
0

Given this simplified elevator state machine code snippet, what is the final state after these events: callElevator(), arriveFloor(), openDoor()?

medium📝 Analysis Q13 of 15
LLD - Design — Elevator System
Given this simplified elevator state machine code snippet, what is the final state after these events: callElevator(), arriveFloor(), openDoor()?
states = ['Idle', 'Moving', 'DoorOpen']
current_state = 'Idle'

def callElevator():
    global current_state
    if current_state == 'Idle':
        current_state = 'Moving'

def arriveFloor():
    global current_state
    if current_state == 'Moving':
        current_state = 'DoorOpen'

def openDoor():
    global current_state
    if current_state == 'DoorOpen':
        current_state = 'Idle'
AIdle
BMoving
CDoorOpen
DError
Step-by-Step Solution
Solution:
  1. Step 1: Trace callElevator()

    Starting at 'Idle', callElevator() changes state to 'Moving'.
  2. Step 2: Trace arriveFloor() and openDoor()

    arriveFloor() changes 'Moving' to 'DoorOpen', then openDoor() changes 'DoorOpen' back to 'Idle'.
  3. Final Answer:

    Idle -> Option A
  4. Quick Check:

    Idle after all events = Idle [OK]
Quick Trick: Follow state changes step-by-step [OK]
Common Mistakes:
MISTAKES
  • Skipping openDoor() effect
  • Assuming state stays at DoorOpen
  • Confusing event order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes