Question presented to candidate: "Build a stopwatch. It shows minutes, seconds and hundredths, has Start, Pause, Resume and Reset, and a Lap button that records split times in a list. It has to stay accurate: if I run it for ten minutes and pause and resume a few times, it should match a real stopwatch."
Requirements:
- Display
mm:ss.cc(minutes, seconds, centiseconds). - One primary button that reads Start, then Pause while running, then Resume while paused.
- Lap (only while running) adds a row with the lap number, the split for that lap and the running total.
- Reset (when there is anything to reset) clears the time and the laps.
- Accurate across pauses and when the page is busy; no timers left running.
What a strong answer should cover:
- Accuracy from timestamps, not tick counts:
elapsed = banked + (now - startedAt), wherebankedholds time from earlier runs. performance.now()(monotonic) rather thanDate.now()for measuring durations.- The redraw timer only triggers re-renders; its interval (here 25 ms) has nothing to do with accuracy.
- Pausing banks the current run; resuming only sets a new
startedAt. - Laps stored as cumulative totals, with splits derived as differences.
- Interval cleanup, and avoiding stale closures in the tick callback.
- Accessibility:
role="timer"(not announced every 25 ms), real buttons with disabled states, a polite live region for laps.
Clarifying questions expected:
- "What precision do you want displayed: tenths, hundredths or milliseconds?"
- "Should laps highlight the fastest and slowest?"
- "Does it need to survive a page reload?"
Code / implementation expected: Yes. A working stopwatch with the timestamp model, pause/resume, laps, reset and cleanup.