Choose optimal guesses for green-only Wordle
GoogleSoftware Engineer · Technical ScreenLast updated
This problem tests your ability to model uncertainty and implement a greedy strategy to minimize the expected search space size when provided with restricted (green-only) feedback.
Problem Statement
In a variant of the Wordle game, you are provided with a dictionary of words. When you make a guess, you receive a 'green' feedback only for letters that are in the correct position. If a letter is not in the correct position, you receive no information about it (i.e., you do not know if it is absent or misplaced).
Your goal is to write a function find_best_guess(words) that returns the word from the provided list that minimizes the expected number of remaining possible words for the next turn.
Rules
- A guess is 'green' at index
iifguess[i] == secret[i]. - After a guess
g, the set of possible candidates is filtered: only words that share the same green-feedback pattern as the secret word relative togremain valid. - Assume every word in the provided dictionary is equally likely to be the secret word.
Function Signature
def find_best_guess(words: List[str]) -> str:
Example
If words = ['aba', 'aca', 'ada']:
- If you guess 'aba':
- If secret is 'aba', feedback is (T, T, T). Remaining: {'aba'}.
- If secret is 'aca', feedback is (T, F, T). Remaining: {'aca'}.
- If secret is 'ada', feedback is (T, F, T). Remaining: {'ada'}.
- The expected number of remaining words is (1 + 1 + 1) / 3 = 1.
Constraints
len(words)is between 1 and 2,000.- All words have the same length
L(1 <= L <= 10). - You should return the word that results in the smallest average size of the resulting candidate set.
Edge Cases
- If multiple words result in the same minimum expected value, return the one that appears first lexicographically.
Hints
Hint 1
For a fixed guess, group all words in the dictionary by the 'green pattern' they produce relative to that guess.
Hint 2
The expected number of remaining candidates for a specific guess is the sum of the squares of the sizes of these groups, divided by the total number of words.
Hint 3
Pre-calculating the green-pattern for every pair of words can significantly speed up the grouping process.
Follow-up questions
- How would the complexity change if the feedback included 'yellow' hints (letter present but wrong position)?
- What data structures would you use to optimize the pattern grouping if the dictionary size increased to 100,000 words?
- Could you implement a heuristic to prune the search space if we only wanted a 'good enough' guess instead of the mathematically optimal one?
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.