Question presented to candidate: "Build an analog clock: a round face with hour, minute and second hands that always show the current time. I would like the second hand to sweep smoothly, but give me a way to switch it to a classic one-tick-per-second movement. Walk me through the angle maths."
Requirements:
- A clock face with 60 tick marks and the numbers 1 to 12, drawn with SVG.
- Hour, minute and second hands rotated to the current time; the hour hand moves gradually between numbers, not in jumps.
- A "Smooth second hand" switch: on means a continuous sweep, off means one step per second.
- Drift-free (angles always come from the real time) and nothing left running after unmount.
- An accessible text equivalent of the time.
What a strong answer should cover:
- The angle formulas: 6 degrees per second, 6 degrees per minute, 30 degrees per hour, each including the fraction of the smaller unit (
hour = 30 * (h % 12 + m / 60)). - Rotating around the centre with SVG
transform="rotate(deg cx cy)"(or CSStransform-origin). requestAnimationFramefor the smooth sweep versus a timeout aimed at the next second for tick mode, switching cleanly with cancellation (cancelAnimationFrame/clearTimeout).- Reading the time every frame or tick instead of adding to an angle, so the hands cannot drift.
- The 360-to-0 wrap at 12 o clock, and why a CSS transition on
rotatemakes the hand spin backwards there. - Rendering cost of 60 updates per second and when to bypass the framework with direct DOM writes.
Clarifying questions expected:
- "Is a smooth sweep required, or is one tick per second enough?"
- "SVG or CSS-rotated divs? Should it scale to any size?"
- "Local time only, or a configurable time zone?"
Code / implementation expected: Yes. A working SVG clock with correct hand angles, both movement modes, a clean switch between them and cleanup.