← Questions

Refactor a rock-paper-scissors simulation

0
AppleCodingLast asked Sep 2025

A coding interview question asked in Apple's coding round, last asked Sep 2025. It tests refactoring, object-oriented design and code reading.

You are given existing code that runs a rock-paper-scissors tournament between three robot players and prints how many games each one won. Change it so the number of rounds is configurable and draws are tracked too. The exercise is about reading unfamiliar code and making a clean change without breaking what works.

What to change

  • The number of rounds comes from configuration instead of a hard-coded constant.
  • Draws are counted: report each player's wins and the total number of draws.
  • The design should make it easy to add new strategies (or new gestures) and to test.

Interface

Unless you agree on a different interface with the interviewer, assume something like:

  • Config(rounds: int) passed into the tournament rather than read from a global.
  • Strategy.next_move(history) -> Move per player.
  • Tournament(players, config).run() -> Scoreboard, where the scoreboard holds wins per player and draws.

Example

config = Config(rounds=3)
players: "rocky" always plays rock, "papa" always plays paper, "cycler" plays rock, paper, scissors in turn
each round, every pair plays once
scoreboard after the run: wins and draws per pair, totals printed at the end

Clarifying questions

  • Does a "round" mean one game between each pair, or one game overall?
  • How are draws reported: in total, per player, or per pair?
  • Must the output format stay the same for existing consumers?
  • Are random strategies seeded so runs can be repeated?

What a strong answer covers

  • Reading the code first and stating its current behaviour before changing anything; adding a test that pins it down.
  • Small, safe steps: extract the round count into configuration and inject it; move scoring into a scoreboard class; keep the winner rule in one place.
  • Representing the "beats" relation as data so new gestures (lizard, Spock) don't need new if chains.
  • Injecting a random source so strategies are testable.
  • Tests: draws counted, configurable rounds honoured, a strategy that always wins.

Follow-ups

  • Add a fourth player without touching the tournament code.
  • Run tournaments in parallel and merge scoreboards.
  • Report results as JSON as well as text.

Where it's asked

  • Apple · Coding · Last asked Sep 2025

What it tests

refactoring · object-oriented design · code reading

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding