Problem, as an interviewer would state it: "Build a light/dark theme toggle. Before the user has ever made a choice, it should default to their operating system's preference. Once they explicitly pick a theme, that choice should persist and win over the system preference on every future visit — until they clear it, at which point it should go back to following the system preference again."
Examples:
// no stored choice, OS prefers dark -> theme is "dark"
// user clicks "light" -> theme is "light", persisted
// page reloads -> theme is STILL "light" (the stored choice, not the OS preference)
Clarifying questions expected:
- Should the theme also update live if the OS preference changes WHILE the page is open and the user hasn't made an explicit choice yet?
- Where should the choice persist —
localStorage, a cookie (for server-rendered pages to know the theme before first paint), or either is fine? - Is "system" itself a selectable third option (explicitly "follow the OS", distinct from "no choice made yet")?
Code / implementation expected: Yes — real, live proof against this session's own actual browser: the real OS-level preference is read correctly, an explicit choice correctly persists and survives a simulated reload, and clearing that choice correctly falls back to the system preference again.