Bird
Raised Fist0

The following code attempts to implement the sweep line approach for car pooling. Which line contains a subtle bug that can cause incorrect capacity checks?

medium🐞 Bug Identification Q7 of Q15
Intervals - Car Pooling
The following code attempts to implement the sweep line approach for car pooling. Which line contains a subtle bug that can cause incorrect capacity checks?
AUsing a list instead of a heap for events
BLine appending (end, num) instead of (end, -num)
CNot checking capacity after each event update
DNot sorting events before processing
Step-by-Step Solution
Solution:
  1. Step 1: Identify event updates

    Passengers should be added at start and subtracted at end; appending (end, num) incorrectly adds passengers at drop-off.
  2. Step 2: Consequence of bug

    Capacity checks will be incorrect as passenger count never decreases at drop-off points.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    End event must decrement passengers -> bug in event sign [OK]
Quick Trick: End events must subtract passengers, not add [OK]
Common Mistakes:
MISTAKES
  • Forgetting to negate passenger count at end
  • Assuming sorting fixes sign errors
Trap Explanation:
PITFALL
  • Candidates often overlook sign in end events, causing subtle capacity miscalculations.
Interviewer Note:
CONTEXT
  • Tests ability to spot subtle sign bugs in event processing.
Master "Car Pooling" 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