Question presented to candidate: "Build a countdown timer. The user enters minutes and seconds, presses Start, and can pause, resume and reset it. It must stop exactly at zero and tell the user, including screen-reader users, that time is up."
Requirements:
- Two number inputs: minutes (0-99) and seconds (0-59); out-of-range or junk input is clamped.
- Start is disabled when the total is zero; inputs are locked while the timer is running or paused.
- One button reads Start, Pause while running, Resume while paused; Reset returns to the entered time.
- The display is
mm:ssand reaches00:00exactly when the time is over, then shows a "Time is up!" alert once. - Correct even if the page is busy or the tab was in the background; no timers left running.
What a strong answer should cover:
- A deadline (
endAt = Date.now() + remaining) as the source of truth, with remaining time derived asendAt - now, instead of decrementing a counter. - Rounding the display up (
Math.ceil) so00:00appears only when time has really run out. - Pausing banks the remaining milliseconds; resuming creates a new deadline.
- A single timer, ideally a timeout aimed at the moment the shown second changes (
left % 1000), cleared on pause, reset and unmount. - Completion handled in exactly one place so it fires once.
- Input validation (clamping) and an accessible completion message with
role="alert".
Clarifying questions expected:
- "Should it keep counting if the user reloads the page?"
- "Any sound or browser notification at the end, or only an on-screen message?"
- "Is hours support needed, or are 99 minutes enough?"
Code / implementation expected: Yes. A working countdown with validated inputs, a deadline model, pause/resume/reset, single-fire completion and cleanup.