Watch 10 video solutions for Guess the Word, a hard level problem involving Array, Math, String. This walkthrough by Happy Coding has 10,818 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array of unique strings words where words[i] is six letters long. One word of words was chosen as a secret word.
You are also given the helper object Master. You may call Master.guess(word) where word is a six-letter-long string, and it must be from words. Master.guess(word) returns:
-1 if word is not from words, orThere is a parameter allowedGuesses for each test case where allowedGuesses is the maximum number of times you can call Master.guess(word).
For each test case, you should call Master.guess with the secret word without exceeding the maximum number of allowed guesses. You will get:
"Either you took too many guesses, or you did not find the secret word." if you called Master.guess more than allowedGuesses times or if you did not call Master.guess with the secret word, or"You guessed the secret word correctly." if you called Master.guess with the secret word with the number of calls to Master.guess less than or equal to allowedGuesses.The test cases are generated such that you can guess the secret word with a reasonable strategy (other than using the bruteforce method).
Example 1:
Input: secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10
Output: You guessed the secret word correctly.
Explanation:
master.guess("aaaaaa") returns -1, because "aaaaaa" is not in wordlist.
master.guess("acckzz") returns 6, because "acckzz" is secret and has all 6 matches.
master.guess("ccbazz") returns 3, because "ccbazz" has 3 matches.
master.guess("eiowzz") returns 2, because "eiowzz" has 2 matches.
master.guess("abcczz") returns 4, because "abcczz" has 4 matches.
We made 5 calls to master.guess, and one of them was the secret, so we pass the test case.
Example 2:
Input: secret = "hamada", words = ["hamada","khaled"], allowedGuesses = 10 Output: You guessed the secret word correctly. Explanation: Since there are two words, you can guess both.
Constraints:
1 <= words.length <= 100words[i].length == 6words[i] consist of lowercase English letters.wordlist are unique.secret exists in words.10 <= allowedGuesses <= 30Problem Overview: You receive a list of possible 6-letter words. One of them is the secret. Each guess returns the number of characters that match the secret word at the same position. The goal is to identify the secret word within 10 guesses by strategically choosing which word to query.
Approach 1: Minimax Strategy (O(n^2 * L) time, O(n) space)
This approach treats the problem like an adversarial game. For every candidate word in the list, compute how it partitions the remaining words based on the number of positional matches (0–6). The worst-case bucket size after a guess represents how much uncertainty remains. The minimax rule picks the word whose largest bucket is smallest, minimizing the worst-case remaining candidates. Implementation typically builds a frequency array for match counts and chooses the guess with the smallest maximum partition. After each guess, filter the word list to keep only words that produce the same match count with the guess as reported by Master.guess(). This process continues until the secret is found or guesses are exhausted. Time complexity is O(n^2 * L) because each pair of words may be compared to count matches (with L = 6), and space complexity is O(n) for maintaining candidate lists. This approach relies heavily on efficient comparison logic and careful pruning of the candidate set.
Approach 2: Randomized Guessing with Strategy (O(n^2 * L) expected time, O(n) space)
A simpler alternative randomly selects a word from the remaining candidates and uses the returned match count to eliminate inconsistent words. After a guess, iterate through the word list and keep only those words whose positional match count with the guessed word equals the response from the master API. The filtering step ensures the candidate pool shrinks every round. Although the selection is random, repeated filtering often converges quickly because the search space drops sharply after each guess. Expected complexity remains around O(n^2 * L) due to repeated comparisons during filtering. This method is easier to implement and often passes constraints, but it lacks the worst-case guarantees provided by the minimax approach.
Both approaches rely on repeatedly comparing words character-by-character and maintaining a shrinking candidate set, which makes the problem closely related to techniques from array processing and string comparison. The decision strategy—especially in the minimax version—mirrors concepts from game theory, where you minimize the opponent's best outcome.
Recommended for interviews: The minimax strategy is the expected solution. It demonstrates the ability to reason about worst-case partitions and adversarial search spaces. A randomized filtering approach can show initial understanding, but the minimax strategy signals stronger algorithmic thinking and better control over worst-case behavior.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Randomized Guessing with Filtering | O(n^2 * L) expected | O(n) | Quick implementation when constraints are small and worst-case guarantees are not critical |
| Minimax Strategy | O(n^2 * L) | O(n) | Interview-preferred solution that minimizes worst-case candidate set after each guess |