Question presented to candidate: "Build Whack-a-Mole. There is a 3 by 3 field of holes. After the player presses Start, moles pop up in random holes for a short time; clicking a mole scores a point. The game lasts 15 seconds, it should get harder as time runs out, and when time is up the board stops and shows the score. I also want to be able to test it without waiting for real randomness."
Requirements:
- A 3 x 3 field of holes, a score, a countdown and a Start / Play again button.
- One mole at a time appears in a random hole (never the same hole twice in a row), stays up for a limited time, then hides.
- Clicking the hole with the mole scores once and hides it; clicking an empty hole or the same mole twice does not add points.
- The time a mole stays up shrinks as the game goes on.
- At 0 seconds the game ends, no mole is shown, clicks do nothing, and every timer is stopped.
- The random source is injectable so a test can replay a game deterministically.
What a strong answer should cover:
- A state machine: idle, playing, over; and within playing, a gap between moles and a mole being up.
- Game rules as pure functions (
tick(state, dt, random),whack(state, hole)) with no timers orMath.randominside, so tests can drive them with a seeded generator. - A single game clock (one interval) instead of a pile of independent timeouts, and cleaning it up on game over and unmount.
- Avoiding stale state in timer callbacks, and why impure updates are dangerous where a framework may run update functions more than once.
- Preventing double scoring, and a difficulty curve expressed as data.
Clarifying questions expected:
- "One mole at a time, or several at once?"
- "Should missing a mole or clicking an empty hole cost points?"
- "Does the game need to pause when the tab is hidden?"
Code / implementation expected: Yes. A working game; expect to be asked how you tested it and how timers are cleaned up.