KatInterview

Build a Wordle Clone Component in React

System DesignModel answer
MediumModel answerSystem Designreacttypescriptgame-logic

Last updated

Implement a 5-letter, 5-guess word puzzle in React with correct duplicate-letter tile scoring, virtual keyboard state, and clean game-over transitions.

Overview

You are building WordleGame, a self-contained React component that powers a word-guessing puzzle. The player has up to five attempts to identify a hidden five-letter word. After each guess the tiles and the on-screen keyboard update to reflect what the player has learned.


What to Build

1. The Game Board

  • Render a grid of 5 rows × 5 tiles.
  • The active row fills letter by letter as the player types.
  • Pressing Enter on a complete five-letter row submits the guess.
  • Pressing Backspace removes the last letter in the active row.
  • Both the physical keyboard and the on-screen virtual keyboard must work.
  • Once the game is over (win or loss), all input is ignored.

2. Tile Coloring Rules

After a guess is submitted, assign each tile one of three statuses:

StatusColorMeaning
"correct"GreenRight letter, right position
"present"YellowRight letter, wrong position
"absent"GrayLetter not in word (or used up)

Duplicate-letter rule: First, lock in every exact positional match as "correct". The remaining unmatched copies of each letter in the target form a budget. Hand out "present" status only while that budget lasts; any overflow copies are "absent". This ensures the total number of colored tiles for a given letter never exceeds how many times that letter appears in the target.

3. Virtual Keyboard & Game State

  • The on-screen keyboard tracks the best-known status for every letter across all submitted guesses (correct beats present beats absent).
  • When the player submits the exact target word, the game enters a won state.
  • When five guesses are exhausted without a match, the game enters a lost state and reveals the target word.

4. Component Interface

interface WordleGameProps {
  targetWord: string;    // Guaranteed uppercase, exactly 5 ASCII letters, e.g. "REACT"
  maxGuesses?: number;   // Defaults to 5
  onGameEnd?: (result: { won: boolean; guessCount: number }) => void;
}

export const WordleGame: React.FC<WordleGameProps> = (props) => {
  // your implementation
};

Worked Example

Target: "APPLE" | Guess: "PUPPY"

  1. Pass 1 — exact matches: Index 2 is P === P → correct. Remaining target letters available for matching: { A:1, P:1, L:1, E:1 }.
  2. Pass 2 — misplaced matches:
    • Index 0 P: budget for P is 1, use it → present (budget → 0).
    • Index 1 U: no U in remaining → absent.
    • Index 3 P: budget exhausted → absent.
    • Index 4 Y: no Y in remaining → absent.

Final: [present, absent, correct, absent, absent]


Edge Cases to Handle

  • Enter pressed with fewer than 5 letters typed → do nothing.
  • Typing a 6th letter → ignore it.
  • Non-alphabetic keys (digits, symbols, arrows) → ignore.
  • Keyboard shortcuts with Ctrl/Meta/Alt → skip, so browser shortcuts still work.
  • Lowercase physical key input → normalize to uppercase.
  • onGameEnd must fire exactly once even under React StrictMode.
  • To reset the puzzle with a new word, the parent should remount via key={targetWord}.

Hints

Hint 1

For the scoring function, two passes beat one: a first pass claims every exact match and tallies leftover target letters, then a second pass awards 'present' only while the tally for that letter is above zero. A single pass cannot handle duplicates correctly because you don't yet know which copies will be exact matches.

Hint 2

Store only the raw facts in state — the list of submitted guesses and the letters typed so far in the current row. Everything else (tile colors, keyboard colors, won/lost) is derived during render with useMemo, so it can never drift out of sync.

Hint 3

Use useReducer instead of multiple useState calls. The reducer becomes the single enforcement point for all input rules (full row before submit, no input after game over, letters only). It also solves the stale-closure problem: the keydown listener can dispatch without closing over state, so it only needs to be re-registered when targetWord or maxGuesses change, not on every keystroke.