Bird
Raised Fist0

Which line contains a subtle bug that causes incorrect free time intervals?

medium🐞 Bug Identification Q7 of Q15
Intervals - Employee Free Time
Examine this snippet of sweep line code for Employee Free Time. Which line contains a subtle bug that causes incorrect free time intervals? ```python events.sort(key=lambda x: (x[0], -x[1])) # sort with start events before end events for time, e_type in events: if active == 0 and prev is not None and prev < time: free_times.append([prev, time]) active += e_type prev = time ```
ACondition 'prev < time' misses zero-length free intervals
BNot initializing 'active' before loop causes runtime error
CUpdating 'prev = time' before processing active count causes off-by-one errors
DSorting with start events before end events causes incorrect free time detection
Step-by-Step Solution
Solution:
  1. Step 1: Understand event sorting order affects active interval counting

    End events must come before start events at the same time to correctly close intervals before opening new ones.
  2. Step 2: Sorting with start events before end events causes active count to increment before decrement, misreporting free intervals

    This leads to falsely detecting free time where intervals actually overlap or touch.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Correct event ordering is end before start at same time [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
  • Candidates think start events first is intuitive but it breaks active interval tracking.
Interviewer Note:
CONTEXT
  • Tests subtle understanding of event ordering in sweep line.
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