Bird
0
0

In this elevator state machine code, what is the bug causing the elevator to never open doors?

medium📝 Analysis Q14 of 15
LLD - Design — Elevator System
In this elevator state machine code, what is the bug causing the elevator to never open doors?
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 = 'Idle'  # Bug here

def openDoor():
    global current_state
    if current_state == 'DoorOpen':
        current_state = 'Idle'
Acurrent_state is not initialized
BcallElevator() does not change state
CopenDoor() changes state incorrectly
DarriveFloor() sets state to 'Idle' instead of 'DoorOpen'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze arriveFloor() function

    arriveFloor() changes 'Moving' state directly to 'Idle', skipping 'DoorOpen'.
  2. Step 2: Understand effect on door opening

    Since state never becomes 'DoorOpen', openDoor() condition never triggers, so doors never open.
  3. Final Answer:

    arriveFloor() sets state to 'Idle' instead of 'DoorOpen' -> Option D
  4. Quick Check:

    arriveFloor() wrong state change = no door open [OK]
Quick Trick: Check if all states are reachable in transitions [OK]
Common Mistakes:
MISTAKES
  • Ignoring wrong state assignment
  • Assuming callElevator() is faulty
  • Overlooking openDoor() condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes