Skip to the solution
EasyFrontend12 min read

Build a Countdown Timer

79 views

The problem

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:ss and reaches 00:00 exactly 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 as endAt - now, instead of decrementing a counter.
  • Rounding the display up (Math.ceil) so 00:00 appears 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.

  • Timers
  • State
  • Effects

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

12 min read ยท 9 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import CountdownTimer from "./src/components/CountdownTimer";

export default function App() {
  return (
    <main className="page">
      <CountdownTimer />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 25 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build a File Explorer Toolbar โ€” Breadcrumbs, Search & Context Menu

Continue