Dsa
Intervals
Easy
The First Maintenance Window
A cloud server is busy during N scheduled maintenance tasks, each represented by a [start, end) time interval. You need to find the earliest time, starting from time 0, where the server is free for a continuous duration of at least K units of time. All inputs are integers.
Input Format:
The first line contains two integers: N (the number of busy tasks) and K (the required free duration).
The next N lines each contain two integers: start and end.
Output Format:
Print the integer representing the earliest start time of the first available gap of size K.
Example:
Input:
3 10
15 20
0 2
5 12
Output:
20
Explanation: The busy intervals are [0,2), [5,12), and [15,20). Gaps are [2,5) (size 3), [12,15) (size 3), and [20, infinity). The first gap of at least size 10 starts at 20.
Key concepts
intervalssortinggreedy
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now