Guess the Word - Problem

Imagine you're playing a word guessing game similar to Mastermind, but with 6-letter words! You have an array of unique words words where each word is exactly 6 letters long. One of these words has been secretly chosen, and your job is to figure out which one it is.

You have access to a special Master object that can help you:

  • Call Master.guess(word) with any 6-letter word from the array
  • It returns -1 if the word isn't in the array
  • It returns the number of exact matches (same letter in the same position) between your guess and the secret word

The challenge? You have a limited number of guesses (allowedGuesses) to find the secret word. You need to be strategic - random guessing won't work!

Example: If the secret word is "acckzz" and you guess "ccbazz", you get 3 matches (positions 2, 4, and 5-6 match).

Your goal is to implement an intelligent strategy that can reliably find the secret word within the allowed number of guesses.

Input & Output

example_1.py — Python
$ Input: words = ["acckzz", "ccbazz", "eiowzz", "abcczz"], secret = "acckzz", allowedGuesses = 10
Output: You guessed the secret word correctly.
💡 Note: Using minimax strategy, we might first guess "ccbazz" which gives 3 matches, then filter to words with 3 matches against "ccbazz", and eventually find "acckzz".
example_2.py — Python
$ Input: words = ["hamada", "khaled", "mahmud", "aaaaaa"], secret = "aaaaaa", allowedGuesses = 10
Output: You guessed the secret word correctly.
💡 Note: The optimal strategy would recognize that "aaaaaa" is quite different from other words and can be identified efficiently through strategic guessing.
example_3.py — Python
$ Input: words = ["gaxckt", "trlccr", "jxwhkz", "ycbfps", "peayuf", "yiejjw"], secret = "gaxckt", allowedGuesses = 10
Output: You guessed the secret word correctly.
💡 Note: Even with diverse words, the minimax approach systematically narrows down possibilities by choosing guesses that provide maximum information.

Constraints

  • 1 ≤ words.length ≤ 100
  • words[i].length == 6
  • words[i] consists of lowercase English letters
  • All the strings of words are unique
  • secret exists in words
  • 10 ≤ allowedGuesses ≤ 30

Visualization

Tap to expand
Guess the Word - Optimal Solution INPUT words[] array: "acckzz" SECRET "ccbazz" "eiowzz" "abcczz" Parameters: secret = "acckzz" allowedGuesses = 10 Master.guess(word) Returns # of exact matches guess("acckzz") --> 6 (all 6 letters match!) ALGORITHM STEPS 1 Pick a Candidate Select word that eliminates most possibilities 2 Query Master Call Master.guess(word) Get match count 3 Filter Candidates Keep only words with same match count to guess 4 Repeat Until Found Continue narrowing down until secret found Match Count Example: Guess: "ccbazz" Secret: "acckzz" Position: 0 1 2 3 4 5 Guess: c c b a z z Secret: a c c k z z Matches: 3 (pos 1,4,5) FINAL RESULT OK Secret Found! Secret Word: "acckzz" Guesses Used: 1-3 (of 10 allowed) Output: "You guessed the secret word correctly." Minimax Strategy O(n^2) per guess ~10 guesses max Key Insight: Use MINIMAX strategy: Pick the word that minimizes the worst-case remaining candidates. After each guess, filter words to only those with the SAME match count to the guessed word. This eliminates maximum words per guess, guaranteeing solution within allowed attempts. TutorialsPoint - Guess the Word | Minimax Optimal Approach
Asked in
Google 45 Microsoft 32 Amazon 28 Meta 15
42.0K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen