Identify a Target Word with Repeated Minimax Guesses
FlexportSoftware Engineer · OnsiteLast updated
This problem tests advanced search heuristics and state-space pruning by requiring the implementation of a Wordle-style feedback system that uses a minimax strategy to identify a secret word from a dictionary.
Problem Statement
You are tasked with designing an engine to solve a word guessing game. You are given a dictionary of valid 5-letter words. When you guess a word, the system returns a 'score' representing the relationship between your guess and the secret word.
Each position in the guess is assigned a state:
- 2: The character is in the correct position (Green).
- 1: The character exists in the word but in a different position (Yellow).
- 0: The character does not exist in the word (Grey).
Your goal is to write a function get_best_guess(possible_words, dictionary) that returns the next optimal word to guess based on a minimax strategy. The strategy aims to minimize the maximum size of the candidate pool remaining after any potential feedback.
Function Signature
def get_best_guess(possible_words, dictionary):
# possible_words: list of words consistent with all previous feedback
# dictionary: full list of all valid words that can be guessed
pass
Scoring Logic
Given a guess and a secret, generate a 5-tuple representing the feedback. For duplicate letters in the guess, prioritize '2's, then '1's, then '0's based on their count in the secret word.
Example
- Secret:
APPLE, Guess:PAPER - Feedback:
(1, 2, 1, 0, 1) - Logic: 'P' (idx 0) is in 'APPLE' (1), 'A' (idx 1) is index 0 (2), 'P' (idx 2) is in 'APPLE' (1), 'E' (idx 3) not present (0), 'R' (idx 4) not present (0).
Constraints
dictionarysize up to 10,000 words.possible_wordswill reduce over time.- Time limit per guess: 2 seconds.
Hints
Hint 1
Instead of choosing the word that eliminates the most candidates on average, choose the word that minimizes the size of the largest remaining candidate group.
Hint 2
Precompute a score matrix where `matrix[guess][secret]` stores the feedback tuple to avoid redundant string comparisons.
Hint 3
If the current `possible_words` set is small, you can perform a deeper search; otherwise, limit your search to words in the current `possible_words` set to optimize pruning.
Follow-up questions
- How would your approach change if you were allowed to use words in your guesses that are not in the valid target dictionary?
- If this game were scaled to words of length N, how does the complexity of the feedback generation change?
- Discuss how you would optimize the performance if the dictionary size were to increase by 10x using bitwise operations for state comparison.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.