Bird
Raised Fist0

Examine the following buggy code for Employee Free Time. Which line contains the subtle bug causing incorrect free time intervals?

medium🐞 Bug Identification Q14 of Q15
Intervals - Employee Free Time
Examine the following buggy code for Employee Free Time. Which line contains the subtle bug causing incorrect free time intervals?
ALine appending end event with (interval[1], -1)
BLine appending start event with (interval[0], 1)
CLine updating prev = time inside loop
DLine sorting events with key=lambda x: (x[0], -x[1])
Step-by-Step Solution
  1. Step 1: Understand sorting order impact

    End events must come before start events at same time to avoid false free intervals.
  2. Step 2: Identify bug in sorting key

    Sorting with key (x[0], -x[1]) puts start events before end events at ties, causing incorrect free time detection.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Correct sorting is (x[0], x[1]) to put end events before start [OK]
Quick Trick: End events must sort before start events at same time [OK]
Common Mistakes:
MISTAKES
  • Sorting start before end events at same timestamp
Trap Explanation:
PITFALL
  • Sorting start before end events causes zero-length or incorrect free intervals.
Interviewer Note:
CONTEXT
  • Tests attention to subtle sorting order bugs in sweep line implementations.
Master "Employee Free Time" in Intervals

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Intervals Quizzes