Dsa
Intervals
Easy
The Heartbeat Buffer
A monitoring system tracks 'active' intervals of a server. To save power, the server only shuts down its buffer if it remains idle for at least G consecutive seconds. An idle period is the gap between two active intervals. If the gap is less than G, the server remains 'warm'. How many times does the server eventually cool down and clear its buffer? Note: The buffer always clears after the final active interval.
Input Format:
- An integer G (the gap threshold).
- An integer N (number of active intervals).
- N lines, each containing two integers S and E (start and end times, inclusive).
Output Format:
- A single integer representing the number of buffer clears.
Example:
Input:
5 3
10 20
22 30
40 50
Output:
2
Explanation: Gap between [10,20] and [22,30] is 2. Since 2 < 5, it stays warm. The merged block is [10,30]. Gap between [30,40] is 10. Since 10 >= 5, it clears at time 30. It clears again after 50.
Key concepts
intervalsmerginggreedy
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