KatInterview

Count Peak Hours Across Venue Time Slots

Greedy & IntervalsAuto-graded · 15 test cases
EasyAuto-gradedGreedy & Intervalsgreedyintervalssweep line

Last updated

Tests the ability to use a sweep line (difference array) to efficiently count how many time slots are simultaneously "active" and find the maximum overlap count.

Count Peak Hours Across Venue Time Slots

A venue logs a list of time slots when it is in use. Each time slot is represented as a half-open interval [start, end) measured in integer minutes from midnight (so a slot from minute 60 to minute 120 covers minutes 60, 61, …, 119 but not 120).

Given a list of such intervals, find the maximum number of intervals that are simultaneously active at any single minute, and also return the earliest minute at which that maximum is first achieved.

If the list of intervals is empty, return [0, -1].

Task

Implement peak_overlap(intervals: List[List[int]]) -> List[int]

  • Input: intervals — a list of [start, end] pairs where 0 <= start < end <= 1440.
  • Output: a list [max_count, earliest_minute] where
    • max_count is the maximum simultaneous overlap count, and
    • earliest_minute is the smallest minute index at which max_count intervals are active.

Constraints

  • 0 <= len(intervals) <= 2000
  • Each interval satisfies 0 <= start < end <= 1440
  • Multiple intervals may share the same start or end point.
  • Minutes are integers in [0, 1440).
  • If intervals is empty, return [0, -1].

Examples

Example 1

Input: peak_overlap([[60, 120], [90, 150], [100, 200]])

Output: [3, 100]

Example 2

Input: peak_overlap([[0, 1440]])

Output: [1, 0]

Example 3

Input: peak_overlap([])

Output: [0, -1]

Hints

Hint 1

Build a difference array of length 1441: add +1 at each start and -1 at each end, then take a prefix sum to get the active count at every minute.

Hint 2

Scan the prefix-sum array from left to right; the first index where the value equals the global maximum is your answer.

Hint 3

Be careful with the half-open interval semantics: minute `end` is NOT covered, so the -1 goes at index `end`, not `end-1`.