Bird
0
0

Identify the error in this SCAN algorithm implementation snippet where the head moves from 30 to 90 with requests at [20, 40, 60, 80]:

medium📝 Analysis Q14 of 15
LLD - Design — Elevator System
Identify the error in this SCAN algorithm implementation snippet where the head moves from 30 to 90 with requests at [20, 40, 60, 80]:
requests = [20, 40, 60, 80]
head = 30
for track in range(head, 100):
    if track in requests:
        print(f"Servicing {track}")
for track in range(head-1, -1, -1):
    if track in requests:
        print(f"Servicing {track}")
AThe first loop should go to 101, not 100
BThe first loop should include the head position
CThe second loop should start from head, not head-1
DThe second loop should start from 99, not head-1
Step-by-Step Solution
Solution:
  1. Step 1: Check range end in first loop

    range(head, 100) goes from 30 to 99 (exclusive end), missing disk edge at 100.
  2. Step 2: Confirm disk convention

    As per standard problems (disk size 100 includes edge 100), first loop should be range(head, 101) to reach 100.
  3. Final Answer:

    The first loop should go to 101, not 100 -> Option A
  4. Quick Check:

    range exclusive, edge 100 needs 101 [OK]
Quick Trick: Range end exclusive, use 101 for disk edge 100 [OK]
Common Mistakes:
MISTAKES
  • Confusing inclusive vs exclusive range ends
  • Starting second loop incorrectly
  • Assuming head position is included twice

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes