Build a hit/miss word guessing game
Company: Dropbox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates implementation skills in managing stateful systems and data structures for pattern matching, as well as algorithmic and adversarial reasoning about information gain and search strategies when the secret can change.
Part 1: Hit/Miss Word Guessing Game Engine
Constraints
- 0 <= len(secret_word) <= 10^4
- 0 <= len(guesses) <= 10^4
- Each guess is a single lowercase English letter.
- secret_word contains only lowercase English letters.
- Repeated guesses are allowed but should not duplicate missed letters.
Examples
Input: ("banana", ["a", "x", "n", "b", "z"])
Expected Output: [["hit", "_a_a_a", ""], ["miss", "_a_a_a", "x"], ["hit", "_anana", "x"], ["hit", "banana", "x"], ["miss", "banana", "xz"]]
Explanation: The guesses reveal all occurrences of hit letters. Misses are tracked in insertion order.
Input: ("apple", ["p", "x", "p", "x", "e"])
Expected Output: [["hit", "_pp__", ""], ["miss", "_pp__", "x"], ["hit", "_pp__", "x"], ["miss", "_pp__", "x"], ["hit", "_pp_e", "x"]]
Explanation: Repeated guesses do not change the state or duplicate misses.
Input: ("", ["a", "b"])
Expected Output: [["miss", "", "a"], ["miss", "", "ab"]]
Explanation: An empty secret word contains no letters, so every guess is a miss.
Input: ("code", [])
Expected Output: []
Explanation: With no guesses, there are no states to return.
Input: ("a", ["b", "a"])
Expected Output: [["miss", "_", "b"], ["hit", "a", "b"]]
Explanation: A single-letter word starts fully hidden and is revealed after the correct guess.
Hints
- Precompute the positions where each letter appears in the secret word.
- Maintain a mutable pattern, such as a list of characters, and join it when producing each output state.
Part 2: Minimum Worst-Case Letter Guesses Against an Adaptive Secret Word
Constraints
- 0 <= len(candidate_words) <= 14
- 0 <= len(candidate_words[i]) <= 12
- All nonempty candidate words have the same length.
- Words contain only lowercase English letters.
- Only single-letter guesses are allowed; no full-word guesses are allowed.
Examples
Input: ([],)
Expected Output: -1
Explanation: There is no possible secret word to identify.
Input: (["planet"],)
Expected Output: 0
Explanation: With exactly one candidate word, the word is already determined.
Input: (["cat", "car", "dog"],)
Expected Output: 2
Explanation: One optimal first guess is 'c', separating dog from cat/car. If cat/car remain, one more guess distinguishes them.
Input: (["aa", "ab", "ba", "bb"],)
Expected Output: 1
Explanation: Guessing 'a' gives four different position masks, uniquely identifying all four words.
Input: (["abc", "abd", "abe"],)
Expected Output: 2
Explanation: Any useful guess can isolate at most one of the three words, so two guesses are needed in the worst case.
Hints
- A guessed letter partitions the current candidate set by the exact positions where that letter appears.
- Use minimax dynamic programming: Player B chooses the letter minimizing the worst remaining candidate group, while adaptive Player A chooses the worst group.