KatInterview

Shared High-Activity Windows Across Sensor Feeds

Greedy & IntervalsAuto-graded · 21 test cases
HardAuto-gradedGreedy & Intervalssweep-lineintervalsstep-function

Last updated

Merge step-function sensor feeds with a sweep over status-change events to find every maximal inclusive window in which all feeds are at or above a level.

Shared High-Activity Windows Across Sensor Feeds

You are given several sensor feeds. Each feed is a list of [timestamp, level] readings, sorted by strictly increasing integer timestamp. Levels are integers in 0..1000000, standing for fixed-point values 0.000000 to 1.000000. The cutoff min_level uses the same scale.

How a feed behaves over time

  • Time is made of integer timestamps only.
  • A reading at timestamp t stays in effect for every integer timestamp from t up to, but not including, the next reading's timestamp.
  • A feed's last reading stays in effect through last_time, inclusive.
  • A feed has no defined level before its first reading. Analysis therefore begins at the latest first-reading timestamp across all feeds. Earlier timestamps are never reported.

Task

Return every maximal inclusive window [start, end] in which, at every integer timestamp, every feed has an effective level that is at least min_level.

  • Windows must be sorted by start.
  • Windows must not overlap or touch. If two qualifying stretches are adjacent (one ends at x and the next starts at x + 1), they form a single window.
  • A window may be a single timestamp, such as [8, 8].
  • If no timestamp qualifies, return an empty list.
  • With one feed, the task reduces to finding that feed's own qualifying windows.

Function

shared_activity_windows(feeds: List[List[List[int]]], min_level: int, last_time: int) -> List[List[int]]

  • feeds: the list of feeds, each a list of [timestamp, level] readings.
  • min_level: the cutoff level.
  • last_time: the final timestamp, inclusive, through which each feed's last reading stays in effect.

Constraints

  • 1 <= len(feeds) <= 1000
  • Every feed has at least one reading, and its timestamps are strictly increasing.
  • Every reading timestamp lies in [0, last_time].
  • 0 <= min_level <= 1000000
  • 0 <= last_time <= 10^12
  • The total number of readings across all feeds is at most 200000.

Examples

Example 1

Input: shared_activity_windows([[[1, 200000], [3, 700000], [6, 800000], [8, 100000]]], 600000, 10)

Output: [[3, 7]]

Example 2

Input: shared_activity_windows([[[0, 800000], [5, 300000], [8, 900000]], [[2, 700000], [6, 900000], [9, 200000]]], 600000, 10)

Output: [[2, 4], [8, 8]]

Hints

Hint 1

Only whether a feed is at or above the cutoff matters, not its exact level. Turn each feed into a yes/no step function and look only at the moments where the answer flips.

Hint 2

Do not walk through every timestamp, since last_time can reach 10^12. Record +1 or -1 at each timestamp where a feed's status flips, then sweep over the sorted event timestamps while counting how many feeds currently qualify.

Hint 3

At the analysis start, use the reading in effect there (found by binary search), not the first reading. A window is open exactly while the count equals the number of feeds. It closes at event_time - 1 on a drop and, if still open at the end, at last_time. Sum the changes at equal timestamps before acting on them.