Skip to the solution
EasyMachine Coding12 min read

Build a Stopwatch

455 views

The problem

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), where banked holds time from earlier runs.
  • performance.now() (monotonic) rather than Date.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.

  • React
  • Timers
  • State

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 Stopwatch from "./src/components/Stopwatch";

export default function App() {
  return (
    <main className="page">
      <Stopwatch />
    </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 19 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 nextEasy

Build a Mortgage Calculator

Continue