Skip to main content

Guess the Word - Solution & Explanation

HardArrayMathStringInteractive12 min readAsked at: Amazon, Microsoft, Apple +7
Practice this problem

Problem Statement

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, or
  • an integer representing the number of exact matches (value and position) of your guess to the secret word.

There 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 <= 100
  • words[i].length == 6
  • words[i] consist of lowercase English letters.
  • All the strings of wordlist are unique.
  • secret exists in words.
  • 10 <= allowedGuesses <= 30

Approach Overview

Problem 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 1: Approach 1: Minimax Strategy

In this approach, we utilize a minimax strategy to minimize the possible size of the word list with each guess. For every guess, we divide the list based on the number of matches it has with other words in the list. The goal is to eliminate as many potential candidates as possible with each guess to find the secret word more efficiently.

We first count matches between each pair of words and use a heuristic to maximize the minimum size of the elimination set. The strategy involves selecting the word which, when guessed, minimizes the worst-case number of words remaining in the list.

Code

Python

JavaScript

C++

Java

C

C#

Complexity

Time Complexity: O(N^2) where N is the number of words. Space Complexity: O(N).

Try this approach in the editor →

Approach 2: Approach 2: Randomized Guessing with Strategy

An alternative approach involves a randomized method that guesses based on the frequency of word intersections. Utilize a strategy that considers how often words share the same characters positionally, choosing those guesses that maximize intersection frequencies to steadily isolate the secret word.

This Python solution randomly selects a word and guesses it. It only keeps words that have the same matching character count as reported by the guess to continuously narrow down the possibilities.

Code

Python

JavaScript

C++

Java

C

C#

Complexity

Time Complexity: O(N) for each guess where N is the length of words. Space Complexity: O(N) for maintaining words.

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Minimax Strategy

Time Complexity: O(N^2) where N is the number of words. Space Complexity: O(N).

Approach 2: Randomized Guessing with Strategy

Time Complexity: O(N) for each guess where N is the length of words. Space Complexity: O(N) for maintaining words.

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Randomized Guessing with FilteringO(n^2 * L) expectedO(n)Quick implementation when constraints are small and worst-case guarantees are not critical
Minimax StrategyO(n^2 * L)O(n)Interview-preferred solution that minimizes worst-case candidate set after each guess

Video Solution

LeetCode 843. Guess the Word • Happy Coding • 11,005 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Guess the Word easy or hard?
Guess the Word is classified as a Hard problem on LeetCode. The difficulty comes from designing a strategy that consistently finds the secret within 10 guesses. Implementing the minimax partition logic and reasoning about worst-case reductions makes it significantly more complex than typical string problems.
How to solve Guess the Word efficiently?
Use a minimax-style selection strategy. For every candidate word, simulate how it partitions the remaining words by match count. Pick the word whose worst-case partition is smallest, then filter the candidate list using the feedback from the interactive guess. This ensures the search space shrinks quickly.
What is the best approach for Guess the Word?
The minimax strategy is the most reliable approach. For each possible guess, group the remaining candidate words by how many characters match. Choose the word that minimizes the largest group in the worst case. This keeps the candidate space shrinking efficiently and guarantees strong performance within the 10-guess limit.
What data structure is used in Guess the Word?
The solution mainly uses arrays or lists to maintain candidate words and frequency arrays to track match-count partitions. String comparison functions compute positional matches between two words. The overall strategy relies on filtering and grouping operations on these collections.
What is the time complexity of Guess the Word?
Most optimized solutions run in O(n^2 * L) time, where n is the number of candidate words and L is the word length (6). The algorithm compares pairs of words to calculate positional matches and repeatedly filters the candidate list after each guess. Space complexity is O(n) for storing remaining candidates.
Is Guess the Word asked at Google, Amazon, or Meta?
Guess the Word is commonly associated with Google-style interview problems because it combines interactive APIs, elimination strategies, and adversarial reasoning. Similar guessing and information-reduction problems appear in interviews at major tech companies including Google and Meta.
Is there a Python or Java solution for Guess the Word?
Solutions are commonly implemented in Python, Java, C++, and JavaScript. Each implementation follows the same pattern: compute match counts between words, select the next guess using either minimax or random strategy, and filter the remaining candidate list based on the interactive response.

Ready to solve this problem?

Practice Guess the Word with our built-in code editor and test cases.

Practice on FleetCode