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.

Visualization

Tap to expand
๐Ÿ•ต๏ธ The Secret Word DetectiveWord1Word2Word3Word4Word5Word6Step 1: All suspects equally likely๐Ÿ” Question: Word3?Answer: 2 matches with secretโœ—โœ“โœ—โœ“โœ—โœ“Step 2: Eliminate suspects that don't match clueRemaining: Word2, Word4, Word6๐ŸŽฏ Continue until only one suspect remains!Smart questioning eliminates maximum suspects each time
Understanding the Visualization
1
Gather Suspects
Start with all possible words as suspects
2
Strategic Questioning
Choose the suspect to question that will give you the most information
3
Analyze Clues
Use the similarity score to eliminate impossible suspects
4
Narrow Down
Repeat with remaining suspects until you find the culprit
Key Takeaway
๐ŸŽฏ Key Insight: The minimax strategy ensures we always make the most informative guess possible, minimizing the worst-case number of remaining suspects after each question. This mathematical approach from game theory guarantees we'll solve the mystery efficiently!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each guess, we need to evaluate all candidates and calculate partitions

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Store candidates and partition information

n
2n
โšก Linearithmic Space

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
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