KatInterview

Track Segment Coverage by Expanding Intervals

Greedy & IntervalsAuto-graded · 24 test cases
MediumAuto-gradedGreedy & Intervalsintervalsmerge-intervalsdata-structures

Last updated

Tests interval merging and coverage detection with streaming data updates.

You are monitoring a one-dimensional segment from 0 to 50.

You will receive a sequence of floating-point positions. Each position x affects a region extending 0.5 units in both directions: specifically, the closed interval [x - 0.5, x + 0.5]. Once a portion of the segment is affected, it remains affected permanently. Effects from different positions can overlap and combine.

Write a function that processes each position in order and returns a list of boolean values. For each position, the corresponding boolean indicates whether the entire segment [0, 50] is completely affected after processing that position (and all previous positions).

Important details:

  • Intervals are closed (endpoints included). Two intervals that share an endpoint (like [1, 2] and [2, 3]) cover the shared point without gaps.
  • Only the portion of each interval that overlaps with [0, 50] matters. A position outside the range [-0.5, 50.5] contributes nothing.
  • Positions arrive in the order given and must be processed sequentially.
  • Once the segment is fully covered, it remains fully covered for all subsequent positions.

Constraints:

  • 1 <= len(points) <= 10^4
  • -10^6 <= points[i] <= 10^6
  • All inputs are finite floating-point numbers

Examples

Example 1

Input: track_segment_coverage([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50])

Output: [False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True]

Example 2

Input: track_segment_coverage([25, 0, 50, 1, 49])

Output: [False, False, False, False, False]

Example 3

Input: track_segment_coverage([100, -100, 25, 0, 50])

Output: [False, False, False, False, False]

Example 4

Input: track_segment_coverage([0.5, 49.5, 25, 10, 40])

Output: [False, False, False, False, False]

Hints

Hint 1

Maintain a collection of disjoint intervals representing affected regions, merging overlapping or touching intervals as new positions arrive.

Hint 2

Use binary search to efficiently find which existing intervals overlap or touch the new interval created by each position.

Hint 3

The segment is fully covered when exactly one interval remains and it spans from 0 to 50.