KatInterview

Analyze Schedule Capacity and Overlaps

Greedy & IntervalsAuto-graded · 21 test cases
MediumAuto-gradedGreedy & Intervalsintervalssortingheap

Last updated

Evaluate interval concurrency to determine if single-resource scheduling is possible and calculate peak concurrent demand.

You are given a collection of time reservations for a shared resource. Each reservation is represented as a half-open interval [start, end), where start is the beginning timestamp and end is the completion timestamp. A reservation releasing the resource at time t allows another reservation to claim it starting at time t without collision.

You need to analyze the scheduling requirements and answer two questions:

  1. Is it possible to fulfill every reservation using a single resource (i.e., no two reservations overlap in time)?
  2. What is the minimum number of resources needed to serve all reservations simultaneously?

Input / Output

  • Input: a, a list of integer pairs [start, end] representing half-open time intervals where start < end.
  • Output: A list [can_use_one, min_resources] where:
    • can_use_one is a boolean (True if single resource is sufficient, False otherwise).
    • min_resources is an integer specifying the peak number of concurrent resources needed.

Constraints

  • 0 <= len(a) <= 10^5
  • Each interval satisfies start < end.
  • Timestamps fit within standard integer limits.

Examples

Example 1

Input: analyze_meetings([[0, 30], [5, 10], [15, 20]])

Output: [False, 2]

Hints

Hint 1

Try sorting the intervals by their starting times to process events chronologically.

Hint 2

How can a min-heap help maintain the active reservations ending earliest?

Hint 3

Remember that intervals are half-open: an event ending at time `t` does not conflict with one starting at time `t`.