KatInterview

Minimum Bandwidth to Stream All Files Within Deadline

Heaps, Sorting & SearchAuto-graded · 17 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary searchgreedysorting

Last updated

Tests binary search on the answer to find the minimum integer bandwidth that allows streaming a list of files (each with a size and deadline) without missing any deadline using a greedy scheduler.

Minimum Bandwidth to Stream All Files Within Deadline

You are building a media streaming system. There are n files to stream. Each file i has:

  • a size sizes[i] (in megabytes, MB), which is the total data that must be delivered, and
  • a deadline deadlines[i] (in seconds from now), by which the file must be fully delivered.

The system streams at a constant bandwidth of B MB/s, and can only stream one file at a time. You must schedule all files in some order, starting at time 0. File i is delivered at the moment its last byte is sent, which equals the sum of the sizes of all files scheduled before it plus its own size, divided by B.

A schedule is feasible for bandwidth B if there exists an ordering of the files such that every file is fully delivered by its deadline.

Return the minimum integer bandwidth B ≥ 1 (in MB/s) such that a feasible schedule exists.

Scheduling Rule

For a given bandwidth B, to check feasibility use the Earliest Deadline First (EDF) policy: sort files by deadline in ascending order, breaking ties by index (smaller index first). Greedily deliver them in that order. A schedule is feasible if and only if no file misses its deadline under EDF.

Constraints

  • 1 ≤ n ≤ 2000
  • 1 ≤ sizes[i] ≤ 10^6
  • 1 ≤ deadlines[i] ≤ 10^9
  • The answer always exists and is at most 10^9.
  • All values are integers.

Examples

Example 1

Input: min_bandwidth([10, 20, 30], [5, 4, 9])

Output: 7

Example 2

Input: min_bandwidth([100], [10])

Output: 10

Example 3

Input: min_bandwidth([5, 5, 5, 5], [4, 8, 12, 16])

Output: 2

Hints

Hint 1

The feasibility check for a fixed bandwidth B is straightforward: sort files by (deadline, index) and simulate delivery times. Think about what property feasibility has as B increases.

Hint 2

If bandwidth B is feasible, then any bandwidth B' > B is also feasible. This monotonicity means you can binary search on B.

Hint 3

For the binary search bounds: the minimum possible answer is 1, and an upper bound is ceil(max(sizes[i]) / min(deadlines[i])) * n or simply sum(sizes) — think about a safe upper bound that is guaranteed to be feasible.