Debounce with a Maximum Wait
An autosave feature debounces edits but must never wait too long. Edits arrive at the times in events (ms, non-decreasing). Edits are collected into a batch; the first edit after a save starts a new batch.
A batch whose first edit was at start and latest edit at last saves at time min(last + waitMs, start + maxWaitMs). If an edit arrives at exactly the moment the pending batch would save, the save happens first and that edit starts the next batch.
Return the saves as { time, count } in order, where count is the number of edits in that batch.
Example 1
Input: events = [0,50,100,150,200,250,600], waitMs = 100, maxWaitMs = 200 Output: [{"time":200,"count":4},{"time":350,"count":2},{"time":700,"count":1}] Explanation: Continuous typing is cut off by maxWait at 200; the second batch saves the normal 100 ms after its last edit at 250.
Example 2
Input: events = [0], waitMs = 100, maxWaitMs = 300 Output: [{"time":100,"count":1}]
Constraints
- 0 <= events.length <= 10^5
- 1 <= waitMs <= maxWaitMs <= 10^6
Hint 1
Before adding an edit, check whether the pending batch is already due.
Hint 2
Remember to flush the final batch after the loop.
Discussion 0
Join free to post in the discussion.
Good practice for: Frontend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Game Developer
Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.