Bird
Raised Fist0
Interview PrepintervalsmediumAmazonGoogle

Count of Intervals Containing Each Point

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Create events for interval starts

We add events for the start of each interval with type +1 to indicate an interval opening.

💡 Marking interval starts lets us know when intervals become active as we sweep.
Line:for start, end in intervals: events.append((start, 1, -1))
💡 Interval starts are the points where active intervals increase.
📊
Count of Intervals Containing Each Point - Watch the Algorithm Execute, Step by Step
Watching each event processed in order reveals how the line sweep algorithm efficiently counts intervals containing points without checking each interval individually.
Step 1/19
·Active fillAnswer cell
insert
(1, +1, -1)
0
i
(2, +1, -1)
1
Result: []
insert
(1, +1, -1)
0
(2, +1, -1)
1
(5, -1, -1)
2
i
(6, -1, -1)
3
Result: []
insert
(1, +1, -1)
0
(2, +1, -1)
1
(5, -1, -1)
2
(6, -1, -1)
3
(2, 0, 0)
4
(5, 0, 1)
5
i
(8, 0, 2)
6
Result: []
sort
i
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
Result: []
move_right
i
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
Result: []
move_right
(1, +1, -1)
0
i
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
Result: []
record
(1, +1, -1)
0
(2, +1, -1)
1
i
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
Result: [2,0,0]
record
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
i
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
Result: [2,2,0]
move_right
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
i
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
Result: [2,2,0]
move_right
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
i
(6, -1, -1)
5
(8, 0, 2)
6
Result: [2,2,0]
record
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
i
(8, 0, 2)
6
Result: [2,2,0]
insert
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
i
(7, +1, -1)
7
Result: [2,2,0]
insert
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(8, 0, 2)
6
(7, +1, -1)
7
i
(10, -1, -1)
8
Result: [2,2,0]
sort
i
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(7, +1, -1)
6
(8, 0, 2)
7
(10, -1, -1)
8
Result: [2,2,0]
move_right
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
i
(7, +1, -1)
6
(8, 0, 2)
7
(10, -1, -1)
8
Result: [2,2,0]
record
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(7, +1, -1)
6
i
(8, 0, 2)
7
(10, -1, -1)
8
Result: [2,2,1]
move_right
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(7, +1, -1)
6
(8, 0, 2)
7
i
(10, -1, -1)
8
Result: [2,2,1]
prune
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(7, +1, -1)
6
(8, 0, 2)
7
(10, -1, -1)
8
Result: [2,1,1]
record
(1, +1, -1)
0
(2, +1, -1)
1
(2, 0, 0)
2
(5, 0, 1)
3
(5, -1, -1)
4
(6, -1, -1)
5
(7, +1, -1)
6
(8, 0, 2)
7
(10, -1, -1)
8
Result: [2,1,1]

Key Takeaways

Transforming intervals and points into a unified sorted event list enables efficient counting.

This transformation is hard to grasp from code alone but becomes clear when watching events processed in order.

Processing interval starts before points and points before interval ends at the same coordinate ensures accurate active interval counts.

The tie-breaking in sorting is subtle but critical, and visualization shows why this order matters.

The active interval count changes only at event points, allowing O(n log n) counting instead of checking each interval per point.

Seeing the active count increment and decrement step-by-step reveals the efficiency gain over naive methods.

Practice

(1/5)
1. Consider the following code snippet implementing the sweep line approach for Employee Free Time. Given the input schedule = [[[1,2],[5,6]], [[1,3]], [[4,10]]], what is the final returned list of free time intervals?
easy
A. [[3,4],[6,10]]
B. [[3,4]]
C. [[3,4],[6,7]]
D. [[3,4],[6,10]]

Solution

  1. Step 1: Sort events and track active intervals

    Events: (1,1),(1,1),(2,-1),(3,-1),(4,1),(5,1),(6,-1),(10,-1). Active counts track busy employees.
  2. Step 2: Identify free intervals when active==0 between events

    Free time occurs between 3 and 4 only, as after 4 active increments again until 10.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Only [3,4] is free time interval [OK]
Hint: Free time only when no active intervals between events [OK]
Common Mistakes:
  • Including intervals after 6 incorrectly as free time
2. What is the time complexity of the optimal algorithm that determines if a person can attend all meetings given n intervals, where the algorithm sorts intervals by start time and then checks for overlaps in a single pass?
medium
A. O(n) because we only scan the intervals once after sorting
B. O(n^2) because each interval is compared with all others for overlaps
C. O(n log n) due to sorting the intervals by start time
D. O(log n) because sorting can be done in logarithmic time

Solution

  1. Step 1: Identify sorting cost

    Sorting n intervals by start time takes O(n log n) time.
  2. Step 2: Identify overlap check cost

    Single pass to check overlaps is O(n), which is dominated by sorting.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Sorting dominates total time complexity [OK]
Hint: Sorting dominates complexity -> O(n log n) [OK]
Common Mistakes:
  • Confusing single pass check as O(n log n)
  • Assuming nested loops cause O(n^2) here
3. Suppose the meeting intervals can be reused multiple times (i.e., a meeting can be attended multiple times if no overlaps occur). Which modification to the algorithm is necessary to correctly determine if a person can attend all meetings without overlap?
hard
A. Sort intervals by end time and greedily select intervals to maximize non-overlapping meetings.
B. No change needed; the original algorithm already handles reuse correctly.
C. Use a frequency map to count how many times each interval appears and check overlaps accordingly.
D. Sort intervals by start time and check overlaps as before, but also track counts of repeated intervals.

Solution

  1. Step 1: Understand reuse scenario

    Reusing intervals means attending multiple instances of the same meeting, so we want to maximize the number of non-overlapping meetings.
  2. Step 2: Identify correct approach

    Sorting by end time and greedily selecting earliest finishing meetings maximizes non-overlapping intervals, handling reuse correctly.
  3. Step 3: Why other options fail

    Original algorithm only checks if all intervals can be attended once; frequency maps or tracking counts do not solve scheduling conflicts optimally.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Greedy by end time is classic interval scheduling for maximum non-overlapping intervals [OK]
Hint: Reuse requires interval scheduling by end time [OK]
Common Mistakes:
  • Assuming original overlap check suffices for reuse
  • Trying to track counts without scheduling logic
4. Suppose intervals can be reused multiple times (i.e., you can merge overlapping intervals repeatedly, possibly reusing intervals). Which modification to the merge intervals algorithm correctly handles this scenario?
hard
A. Use the same sorting + one pass merge approach; repeated intervals will be merged naturally.
B. Sort intervals and repeatedly apply the merge pass until no changes occur, which may take multiple passes.
C. Use a brute force nested loop approach to merge intervals until no overlaps remain.
D. Use a graph-based approach to find connected components of overlapping intervals and merge each component.

Solution

  1. Step 1: Understand that reusing intervals means overlaps can form complex connected groups.

    Simple one-pass merge assumes intervals appear once and sorted order suffices.
  2. Step 2: Recognize that overlapping intervals form connected components in a graph.

    Building a graph where intervals are nodes and edges represent overlaps allows merging all connected intervals correctly.
  3. Step 3: Evaluate options.

    Use the same sorting + one pass merge approach; repeated intervals will be merged naturally. fails because one pass merge assumes no reuse. Sort intervals and repeatedly apply the merge pass until no changes occur, which may take multiple passes. is inefficient and may not terminate quickly. Use a brute force nested loop approach to merge intervals until no overlaps remain. is brute force and inefficient. Use a graph-based approach to find connected components of overlapping intervals and merge each component. correctly models the problem and merges all connected intervals.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Graph connected components capture all overlapping intervals including reuse. [OK]
Hint: Model reuse as graph connected components [OK]
Common Mistakes:
  • Assuming one pass merge works with reuse
  • Using repeated merges without termination guarantee
5. Suppose trains can arrive and depart multiple times (reusing platforms) and the schedule may include negative time values (e.g., early morning times). Which modification to the sweep line algorithm ensures correct minimum platform calculation?
hard
A. Use the same sweep line approach but ensure sorting handles negative times correctly and process events with departure before arrival on tie.
B. Use dynamic programming to handle multiple arrivals and departures per train.
C. Switch to nested loops checking overlaps since negative times break sorting assumptions.
D. Ignore negative times and treat them as zero to simplify sorting.

Solution

  1. Step 1: Recognize that negative times do not affect event sorting if handled correctly

    Sorting supports negative values naturally; no need to ignore or clamp them.
  2. Step 2: Maintain departure-before-arrival ordering on tie to avoid overcounting

    This ordering is critical regardless of time values or multiple arrivals/departures.
  3. Step 3: Continue using sweep line approach for efficiency and correctness

    Nested loops or DP are inefficient or unnecessary for this variant.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Sweep line with correct sorting handles negative and multiple events [OK]
Hint: Sorting handles negative times; keep departure before arrival on tie [OK]
Common Mistakes:
  • Ignoring negative times or clamping them
  • Switching to inefficient nested loops unnecessarily
  • Misordering events causing overcounting