Bird
Raised Fist0

The following code attempts to count intervals containing each point using a line sweep. Identify the bug that causes incorrect counts for points exactly at interval ends.

medium🐞 Bug Identification Q14 of Q15
Intervals - Count of Intervals Containing Each Point
The following code attempts to count intervals containing each point using a line sweep. Identify the bug that causes incorrect counts for points exactly at interval ends.
ALine adding interval end event should use end + 1 instead of end.
BSorting key should sort points before interval starts.
CActive count should be incremented after processing points, not before.
DResult array initialization size is incorrect.
Step-by-Step Solution
  1. Step 1: Identify event creation for interval ends

    Interval end events use 'end' instead of 'end + 1', so points exactly at interval end are processed after the interval ends.
  2. Step 2: Understand effect on counting

    Because end events are processed at 'end', points at 'end' see active count after decrement, missing that interval.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Changing to end + 1 fixes counting for points at interval ends [OK]
Quick Trick: Interval end event must be at end+1 to include points at end [OK]
Common Mistakes:
MISTAKES
  • Using end instead of end+1
  • Misordering events
  • Incorrect active count update
Trap Explanation:
PITFALL
  • Using end instead of end+1 looks correct but breaks counting at boundary points.
Interviewer Note:
CONTEXT
  • Tests candidate's attention to subtle off-by-one bugs in event-based interval counting.
Master "Count of Intervals Containing Each Point" 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