Question presented to candidate: "Build a memory card game. Show a 4 by 4 grid of face-down cards holding 8 pairs. The player flips two cards; if they match they stay face up, if not they flip back after a short delay. Count moves, detect the win and add a New game button. You have about 45 minutes."
Requirements:
- 16 cards (8 pairs), shuffled fairly, face down at the start.
- Clicking a card turns it face up. After the second card: a match stays up, a mismatch flips back after about 800 ms.
- While a mismatched pair is showing, further clicks are ignored (no third card).
- A move counter (one move per pair of flips), a pairs counter and a win message.
- New game reshuffles and resets everything, even in the middle of a flip-back delay.
What a strong answer should cover:
- Game rules as pure functions (
createGame,flip,hideUnmatched) separate from the UI, with the shuffle's randomness injected (a seeded RNG) so it can be tested. - A correct Fisher-Yates shuffle, and why
sort(() => Math.random() - 0.5)is biased. - The lock: deriving "two unmatched cards are open" from state instead of a separate boolean that can drift.
- The flip-back timer: started from state, cancelled on reset and unmount (effect cleanup), safe under React StrictMode.
- Accessibility: cards are real buttons, the label says face down or the symbol, a live region announces the result.
Clarifying questions expected:
- "How long should a mismatch stay visible, and can the player click during it?"
- "Should the grid size or number of pairs be configurable?"
- "Do you want a timer or a best score, or just moves?"
Code / implementation expected: Yes. A working game in the framework of your choice, with the rules in plain functions and a short explanation of the timer cleanup.