Problem, as an interviewer would state it:
"setInterval(fn, 1000) is not actually guaranteed to fire exactly every 1000ms — explain why, and implement a version that stays accurate over a long-running session."
Examples:
const clock = createDriftCorrectedInterval(tick, 1000);
// stays close to real wall-clock 1000ms boundaries even if tick() itself
// occasionally takes 20-30ms to run, unlike plain setInterval
Clarifying questions expected:
- Is the drift caused by the EVENT LOOP being busy, or specifically by the handler's OWN execution time?
- Does "accurate" mean each individual gap is exactly
wait, or that the TOTAL elapsed time after N ticks matches N × wait? - Is pausing/resuming (e.g., a background browser tab) in scope, or just steady-state drift?
Code / implementation expected: Yes — real, direct, measured proof comparing accumulated drift between a naive setInterval and a corrected version under identical conditions.