Back

Stopwatch

easy

Streak

0 days

Progress

0%

Submitted

0

Stopwatch

React25 mineasyFree

Prompt

Build a React stopwatch with Start, Pause, Resume, and Reset controls. Display elapsed time as MM:SS.cs, for example 01:05.27 for 65 seconds and 270 milliseconds.

  • Start begins at zero; after pausing, the same control resumes from the saved time.
  • Pause freezes elapsed time at the moment it is pressed, even between display updates. Time spent paused must not count.
  • Reset always stops the stopwatch and restores 00:00.00.
  • Repeated Start calls must not create multiple timers. Clear scheduled work on pause, reset, and unmount.

Measure elapsed time from timestamps rather than counting callbacks. A refresh roughly every 10–30ms is sufficient; browsers do not guarantee exact timer intervals. Minutes may exceed 99. Laps, refresh persistence, and tracking across device sleep are outside the core task.

Learn and continue

Prerequisite: Effect dependencies and cleanup. Work through the React Machine Coding Foundations path to practice related skills in order.

Requirements

  • →Display MM:SS.cs with two centisecond digits and unbounded minutes
  • →Start or Resume must create at most one active timer
  • →Pause samples elapsed time immediately and excludes subsequent paused time
  • →Reset stops and zeroes both saved and displayed time
  • →Clear interval on pause, reset, and unmount
  • →Measure timestamp differences; timer callbacks only refresh the display
  • →Laps, persistence across reloads, and device-sleep tracking are out of scope

Starter packet

What the interviewer handed you

The UI must stay accurate when the browser delays timers or the tab returns from the background. A counter incremented every tick is not sufficient.

Manual acceptance checks

  • Start, wait, pause: the displayed elapsed time stops increasing
  • Resume after a pause: time continues from the saved elapsed value, without adding the paused gap
  • Reset while running: the display returns to zero and old scheduled updates do not restart it
  • Unmount while running: scheduled work is cleaned up
  • Pause before the first display tick: elapsed still includes time up to the click
  • Call Start twice before a render: only one interval exists
  • Format 65270ms as 01:05.27 and 6000000ms as 100:00.00

Likely follow-ups

  • • What does requestAnimationFrame change?
  • • How would you persist an active timer across refresh?
Example
Loading preview...
For the best coding experience, we recommend using a desktop device.
Preparing Sandbox...
Premium interview report

What interviewers score in this build

Use this before reading the code. It tells you what to say, what to test, and where machine-coding candidates usually lose points.

Interview signals

  • Elapsed-time model: Computes elapsed time from a monotonic timestamp difference rather than tick counts
  • Refs and state: Stores timer bookkeeping in refs and visible display/running state in state
  • Pause and resume: Samples on Pause and preserves elapsed time while excluding the paused gap
  • Timer lifecycle: Guards duplicate Start calls and clears timers on Pause, Reset, and unmount

Time checkpoints

  1. 1

    0–3 min: Clarify pause, resume, reset, and display precision

  2. 2

    3–7 min: Define timestamp, saved elapsed, display, and timer ownership

  3. 3

    7–15 min: Implement guarded Start, exact Pause, Resume, and Reset

  4. 4

    15–20 min: Add formatting, labeled controls, and unmount cleanup

Edge-case checklist

Pause before the first tick
Resume after a long paused gap
Repeated Start before a render
Reset while running
Unmount while running
Two centisecond digits and minutes above 99

Common mistakes

  • Counting interval callbacks as time
  • Saving only the last painted value on Pause
  • Using only state to guard duplicate Start calls
  • Leaving an interval alive after Reset or unmount
  • Announcing every display tick in a live region
SolutionRead-only · Live Preview

Technical Explanation

Separate measuring time from refreshing the display

A timer callback is a reminder to repaint, not proof that exactly 20ms passed. If the browser is busy, callbacks can arrive late. Adding 20 on each callback undercounts elapsed time. A functional state updater fixes stale reads, but it does not fix this measurement error.

Use one elapsed-time equation

Keep the timer ID, start anchor, and saved elapsed milliseconds in refs. Keep the displayed milliseconds and running flag in state because both affect the UI. While running, compute elapsed = performance.now() - startAnchor. On resume, set startAnchor = performance.now() - savedElapsed.

For example, pause at 1,235ms, wait five seconds, then resume. Move the anchor back by 1,235ms from the new current time. After another 300ms of running, elapsed is 1,535ms: the five paused seconds are excluded.

Sample the clock when Pause is pressed

The last callback may have painted 1,220ms even though Pause is pressed at 1,235ms. Save a fresh timestamp difference in the pause handler before clearing the timer. Reusing only the last painted value loses 15ms in this example, and can lose much more when callbacks are throttled.

Prevent duplicate timers and define Reset

Use timerRef.current !== null as the synchronous guard in Start. State updates are scheduled, so a running flag alone may not guard multiple calls before the next render. Pause and Reset clear the interval and set its ref to null. Reset also zeroes saved and displayed time and sets running to false. Cleanup on unmount clears the same interval.

Choose the clock deliberately

performance.now() is monotonic and unaffected by changes to the system clock, making it useful for elapsed time within the current page. A delayed callback reads a fresh difference instead of replaying missed ticks. This does not promise continuous rendering in a background tab. Device-sleep behavior differs across browsers; persistence across reloads or sleep needs a separate product decision and clock strategy.

Format without changing the stored value

Store milliseconds. Derive centiseconds with Math.floor(ms / 10) % 100, seconds with Math.floor(ms / 1000) % 60, and total minutes with Math.floor(ms / 60000). Pad each part to at least two digits; do not wrap minutes at 60 or 100. Thus 65,270ms becomes 01:05.27.

Check the behavior

Test Pause before the first callback, a long delay between callbacks, repeated Start, pause/resume with a long paused gap, Reset while running, and unmount. Keep controls as native buttons. Give the elapsed display an accessible label without announcing every hundredth through a live region.

Interview Criteria

Elapsed-time model

Computes elapsed time from a monotonic timestamp difference rather than tick counts

Refs and state

Stores timer bookkeeping in refs and visible display/running state in state

Pause and resume

Samples on Pause and preserves elapsed time while excluding the paused gap

Timer lifecycle

Guards duplicate Start calls and clears timers on Pause, Reset, and unmount

Formatting and accessibility

Formats two centisecond digits, allows large minute counts, and avoids rapidly announcing time

Time Checkpoints

0–3 min

0–3 min: Clarify pause, resume, reset, and display precision

3–7 min

3–7 min: Define timestamp, saved elapsed, display, and timer ownership

7–15 min

7–15 min: Implement guarded Start, exact Pause, Resume, and Reset

15–20 min

15–20 min: Add formatting, labeled controls, and unmount cleanup

20–25 min

20–25 min: Check delayed ticks, rapid controls, and pause before the first tick

Streak

0 days

Last active: Sign in to track

Progress

0%

0/0 solved

Submitted

0

Solutions pushed to review history.

More Machine Coding

View all Machine Coding →
Machine CodingMedium

React Tic-Tac-Toe Engine

Machine CodingMedium

Performant Image Slider

Machine CodingMedium

API Search and Render Image

Machine CodingEasy

Accessible React Tabs

Machine CodingMedium

Recursive File Explorer

Machine CodingEasy

Tooltip

Related hubs

  • All machine coding
  • Frontend machine coding guide
  • React machine coding questions
  • UI coding interview questions
  • React UI interview questions
  • Apple interview
  • Netflix interview
  • Uber interview

FAQ

What skills does a Stopwatch interview question test?

Timer accuracy with setInterval/requestAnimationFrame, cleanup on unmount, start/pause/reset state transitions, and formatting elapsed time.

Common pitfalls?

Leaking intervals, drifting time, and messy state that cannot distinguish idle vs running vs paused.

Related practice?

Traffic light, progress bar, and image slider.