Home
← All questions

Stopwatch | Frontend Machine Coding

EasyPremiumMachine Coding

Frontend Machine Coding interview practice: Build a stopwatch with start, stop, and pause controls.

Problem

Build a stopwatch component with Start, Pause, and Stop controls.

Requirements

  1. Render three buttons: Start, Pause, and Stop.
  2. Start — begins counting up. On the first start the timer runs from zero.
  3. Pause — freezes the timer at the current time. Clicking Start again resumes from where it was paused.
  4. Stop — stops the timer and resets the display back to zero.

Hints / Approach

  • Keep elapsed time in state as milliseconds so pause/resume just keeps accumulating; format it as mm:ss (or mm:ss:cs for centiseconds) for display.
  • Drive ticks with setInterval; clear the interval on pause and on stop.
  • To avoid drift, store startTimestamp = Date.now() - elapsed on (re)start and set elapsed = Date.now() - startTimestamp on each tick — don't just do elapsed += interval.
  • Stop clears the interval and sets elapsed back to 0; guard against starting an already-running timer.

No reference solution provided.

Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium