Bird
Raised Fist0

Given intervals = [[1,4],[2,5],[7,9]] and points = [2,5,8], what is the final output of the function call count_intervals_line_sweep(intervals, points)?

easy🧾 Code Trace Q12 of Q15
Intervals - Count of Intervals Containing Each Point
Consider the following Python function implementing the line sweep algorithm to count intervals containing each point. Given intervals = [[1,4],[2,5],[7,9]] and points = [2,5,8], what is the final output of the function call count_intervals_line_sweep(intervals, points)?
A[2, 1, 1]
B[2, 1, 0]
C[1, 2, 1]
D[2, 2, 1]
Step-by-Step Solution
  1. Step 1: Sort events and process starts

    Events: (1,+1), (2,+1), (2,0), (5,0), (5+1,-1), (4+1,-1), (7,+1), (8,0), (9+1,-1). Sorted order processes starts before points, points before ends.
  2. Step 2: Track active intervals and assign counts at points

    At point 2: active=2 (intervals [1,4],[2,5]), at point 5: active=1 (only [2,5] ends at 6), at point 8: active=0 (interval [7,9] ended at 10, but 8 < 10 so active=1 actually, re-check)
  3. Correction Step 2:

    At point 8: interval [7,9] is active, so active=1. So final counts: [2,1,1]
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Counting active intervals at each point matches expected output [OK]
Quick Trick: Remember to add 1 to interval ends for event ordering [OK]
Common Mistakes:
MISTAKES
  • Forgetting end+1 shifts interval end
  • Misordering events causing wrong active count
  • Off-by-one in active count at points
Trap Explanation:
PITFALL
  • Misordering events or using end instead of end+1 causes wrong counts at points equal to interval ends.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to trace line sweep event processing and off-by-one handling.
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