Loading
Loading
A monitoring agent sends a heartbeat timestamp at a fixed interval. The collector stored them in order, but exactly one heartbeat was lost somewhere in the middle — never the first or the last.
Given the received timestamps in beats (strictly increasing), return the timestamp of the missing heartbeat. With n received beats there were n + 1 sent, so the interval is (last − first) / n.
beats = [10, 20, 40, 50, 60]interval = (60 − 10) / 5 = 10index i should hold 10 + 10·imid = 2: 40 vs 30already shifted — the gap is at or before index 2mid = 1: 20 vs 20still on schedule — the gap is after index 1first shifted index = 2the missing beat is 10 + 10·2 = 3030Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(log n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Readings are 10 apart; 30 should sit between 20 and 40.
Example 2
The step is (3 − (−6)) / 3 = 3, so −6, −3, 0, 3 — the 0 is missing.