AlgoMaster Logo

Guess the Word

hardFrequency8 min readUpdated June 23, 2026

Understanding the Problem

This is Wordle with a fixed word list. You guess a word from the list, the system tells you how many characters are in the exact right position, and you use that feedback to narrow down which word is the secret. You have a limited number of guesses, so the order of guesses matters.

One fact drives every approach. After guessing word W and receiving a match count of k from the Master, the secret has exactly k positional matches with W. Any word in the list that does not have exactly k positional matches with W cannot be the secret, so we eliminate it. This guess-and-filter loop is the foundation. The remaining question is which word to guess next to eliminate the most candidates.

Key Constraints:

  • words.length <= 100 -> The word list is small. Even O(n^2) work per guess stays under 10,000 comparisons.
  • words[i].length == 6 -> Comparing two words is a fixed 6-character loop, so a single comparison is O(1).
  • 10 <= allowedGuesses <= 30 -> With at least 10 guesses for up to 100 words, eliminating one word per guess is not enough. The strategy has to shrink the candidate list faster than that.
  • secret exists in words -> The answer is always in the candidate list, so we never construct words from scratch.

Approach 1: Naive Guess with Filtering

Intuition

The simplest strategy is to pick any word from the candidate list, guess it, and use the Master's feedback to eliminate impossible candidates. No selection logic is needed to get started; picking the first word in the list works.

Filtering works because the match count is symmetric and exact. If we guess word W and the Master returns k, the secret has exactly k characters matching W in the same positions. A word with 2 positional matches against W cannot be the secret when the Master reported 3, so we drop it. The secret always survives this filter because it has exactly k matches with W by definition.

After filtering, we repeat with the smaller candidate list. Each round guesses one word and removes it along with every incompatible word, until only the secret remains.

Algorithm

  1. Copy all words into a candidates list
  2. Pick the first word from candidates as the guess
  3. Call master.guess(guess) and store the match count
  4. If the match count is 6, we found the secret, return
  5. Filter candidates: keep only words where match(word, guess) == matchCount
  6. Repeat from step 2 with the filtered list

Example Walkthrough

1Round 1: Pick first candidate "eiowzz" to guess
0
eiowzz
guess
1
ccbazz
2
acckzz
3
abcczz
1/6

Code

The weakness here is the selection strategy. Picking the first word wastes a guess when that word has the same match count with every other candidate, since the filter then removes only the guessed word itself. The next approach picks words that split the candidates into smaller groups.

Approach 2: Minimize Zero-Match Candidates

Intuition

A simple probability argument leads to a heuristic that works well before reaching for full game theory. For two random 6-letter words, each position has a 1/26 chance of matching, so the probability of zero total matches is (25/26)^6, about 79%. Roughly four out of five random word pairs share no character in any position.

That skew matters for filtering. When we guess a word and get 0 matches back, we keep only words with 0 matches against the guess. Because most word pairs have 0 matches, this group is usually the largest, and a large surviving group means weak elimination.

The heuristic falls out of this: for each candidate, count how many other candidates have 0 matches with it, then guess the candidate with the fewest zero-match partners. That word shares at least one positional character with the most other candidates. Guessing it keeps the dangerous 0-match group small, and the non-zero outcomes are smaller still.

Algorithm

  1. Copy all words into a candidates list
  2. For each candidate, count how many other candidates have 0 positional matches with it
  3. Pick the candidate with the smallest zero-match count
  4. Call master.guess() with the selected word
  5. If the match count is 6, return
  6. Filter candidates: keep only words where match(word, guess) == matchCount
  7. Repeat from step 2

Example Walkthrough

candidates
1Round 1: count zero-match partners for each word
0
0m=2
abcdef
1
0m=4
ghijkl
2
0m=2
abcxyz
3
0m=4
mnopqr
4
0m=2
abcghi
zeroMatchCounts
1Count zero-match partners: words sharing the "abc" prefix overlap in 3 positions
abcdef
:
2
ghijkl
:
4
abcxyz
:
2
mnopqr
:
4
abcghi
:
2
1/7

Code

The zero-match heuristic only looks at one of the seven possible match outcomes. A word can have few zero-match partners and still leave a large 3-match group. The next approach scores every outcome for each candidate and picks the word that minimizes the worst case.

Approach 3: Minimax Selection

Intuition

Treat the secret as an adversary. We pick a guess, and the adversary effectively chooses the outcome by deciding which match count comes back. The goal is to minimize our worst case across every outcome the adversary could force.

For each candidate word W, consider guessing it. The other candidates split into groups by their match count with W: the 0-match group, the 1-match group, and so on up to the 5-match group (a 6-match means W is the secret). After we guess W and receive a match count, the candidate list shrinks to exactly the group with that count.

The worst case for guessing W is the size of its largest group, because the adversary can always report the count that leaves the most candidates. If W splits candidates into groups of sizes [15, 3, 2, 1], the worst case is 15 survivors. If word X splits them into [6, 5, 5, 4], its worst case is 6.

The minimax strategy picks the word whose largest group is smallest, minimizing the most candidates that can survive a single guess. For n up to 100, this converges in roughly 5 to 6 guesses, well inside the allowed budget.

Algorithm

  1. Copy all words into a candidates list
  2. For each candidate, compute the match count with every other candidate and group them by match count (0 through 6)
  3. For each candidate, find the size of its largest group (the worst-case outcome)
  4. Pick the candidate whose largest group is smallest (minimax choice)
  5. Call master.guess() with the selected word
  6. If the match count is 6, return
  7. Filter candidates: keep only words where match(word, guess) == matchCount
  8. Repeat from step 2

Example Walkthrough

candidates
1Compute match counts between all pairs of candidates
0
eiowzz
1
ccbazz
2
acckzz
3
abcczz
minimaxScores
1Evaluate each word: group other candidates by match count
1/6

Code