Dropbox logoDropbox
CodingFree preview

Hangman Word Game with an Adversarial Word Chooser

Implement Hangman-style hit/miss game logic (hits reveal every matching position, misses go into a misses array). The hard follow-up lets the chooser switch the secret word at any time while staying consistent with the board; given the list of possible words, compute the minimum number of guesses that guarantees a win. Depth ranges from discussion plus pseudocode to full implementation.

SWE
simulation
string
hashmap
hard
Frequency
Low
Last asked
2026-03-28
Stage
onsite-coding

Problem

A two-player word-guessing game in the Hangman style. One player chooses a secret word; the other guesses one letter at a time.

Part 1 — Game logic

  • If the guessed letter occurs in the secret word, it is a hit and every position holding that letter is revealed on the board.
  • If it does not occur, it is a miss and the letter is appended to a misses array.
  • Implement the state and feedback for each guess. The interviewer walks through an example up front to make sure the rules are understood before you code.
    • Minority variant: the round is framed with a dictionary, a secret word and a guessed word, and you return feedback for that guess rather than handling single letters — clarify before coding.

Part 2 — Adversarial chooser

  • The chooser may switch the secret word at any time, as long as the new word is still consistent with the current board (revealed letters and misses). You are given the list of possible words.
  • The guesser wants to minimize the number of guesses. Compute the minimum number of guesses needed to guarantee finding the word.
  • Depth varies by interviewer: in some loops this part is a strategy discussion plus pseudocode; in others it is the main coding task of the round.

Variants

Saboteur framing

Input is a dictionary, a masked string with hidden letters, and the lists of correctly and incorrectly guessed letters. One side guesses, the other sabotages by changing the answer before the next guess. Simulate both sides playing optimally and predict who wins. The adversarial chooser in Part 2 is the same game: if a guessed letter is absent from some still-possible word, the opponent may adopt that word as the answer and call the guess wrong, and the goal is to minimize wrong guesses.

Hints

Pitfalls

  • Part 1 is quick; the adversarial part is where candidates consistently find the round hard.
  • A word is only a legal switch if it matches every revealed position and contains none of the missed letters — the candidate set must be filtered on both after every guess.
  • Confirm early whether you are expected to write full code for Part 2 or only discuss it and sketch pseudocode, and budget time accordingly.
  • The wrong-guess budget must be part of the memo state; dropping it makes the cached results wrong as the budget shrinks.
  • The recursion returns the total number of guesses, hits included; decide the winner by comparing it with infinity, not with the wrong-guess budget.

Talking Points

  • Name the minimax structure up front: the guesser minimizes, the adversary maximizes, and the state is the set of consistent words.
  • Argue that the adversary's reply is determined by the pattern groups, which removes half of the game tree.
  • Memoize on frozensets of word indices; they are hashable and let different guess orders share results.
  • Be honest about the 2^|D| state space: fine for roughly 15–20 words, so propose branch-and-bound or heuristic pruning for larger dictionaries.
  • Point out that the sabotage framing and the "opponent picks the answer" framing are the same game.
  • Precompute each word's pattern per letter to speed up the grouping step.

Follow-ups

Preparation

  • Implement the Part 1 game state (secret word, revealed board, misses) including repeated letters, in under 15 minutes.
  • Write a function that, given a board pattern and the misses array, returns the words from a list that are still consistent.
  • Write the worst-case recurrence for "minimum guesses that guarantees a win" over a candidate word set, then add memoization and talk through its cost on a small word list.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...