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 <= 100
words[i].length == 6
words[i]
consist of lowercase English letters.wordlist
are unique.secret
exists in words
.10 <= allowedGuesses <= 30
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to "Guess the Word" involves trying every possible word from the list as the secret word. We make a guess, receive feedback, and then use that feedback to narrow down the remaining possible secret words. We repeat this process until we correctly guess the secret word.
Here's how the algorithm would work step-by-step:
def guess_the_word_brute_force(word_list, secret): possible_words = word_list[:]
while len(possible_words) > 0:
guess_word = possible_words[0]
matches = get_matches(guess_word, secret)
# Eliminate words that don't have the same number of matches
possible_words = [word for word in possible_words if get_matches(word, guess_word) == matches]
if guess_word == secret:
return guess_word
def get_matches(word1, word2):
# Determines the number of matching characters between two words
matches = 0
for index in range(len(word1)):
if word1[index] == word2[index]:
matches += 1
return matches
The goal is to guess a secret word by making strategic guesses. The most efficient strategy involves picking guesses that maximize the information gained about the secret word, quickly narrowing down the possibilities. We'll choose guesses that are different from all previous guesses.
Here's how the algorithm would work step-by-step:
import random
def guess_the_word(word_list, master):
def get_matches(word1, word2):
matches = 0
for i in range(len(word1)):
if word1[i] == word2[i]:
matches += 1
return matches
possible_words = word_list[:]
for _ in range(10):
#Pick a random word for the first guess.
if len(possible_words) > 1:
guess = random.choice(possible_words)
else:
guess = possible_words[0]
match_count = master.guess(guess)
if match_count == len(guess):
return
#Filter the list to keep words that match our guess
possible_words = [
word
for word in possible_words
if get_matches(guess, word) == match_count
]
# Use minmax strategy to reduce worst case outcome.
if len(possible_words) > 1:
best_guess = ""
min_max_score = float('inf')
for potential_guess in possible_words:
group_counts = {}
# Count groups based on pattern matches.
for other_word in possible_words:
matches = get_matches(potential_guess, other_word)
if matches not in group_counts:
group_counts[matches] = 0
group_counts[matches] += 1
max_group_size = max(group_counts.values())
# Pick the guess that minimizes the largest group.
if max_group_size < min_max_score:
min_max_score = max_group_size
best_guess = potential_guess
guess = best_guess
match_count = master.guess(guess)
if match_count == len(guess):
return
#Filter the list to keep words that match our guess.
possible_words = [
word
for word in possible_words
if get_matches(guess, word) == match_count
]
Case | How to Handle |
---|---|
wordlist is null or empty | Return an empty string or null to indicate invalid input and prevent null pointer exceptions. |
wordlist contains words of different lengths | Filter out words that have different lengths from the initial secret word length. |
secret is null or empty | Return an error or handle gracefully by returning a default/empty value to prevent runtime exceptions. |
No word in wordlist matches the secret | Handle the case where the algorithm fails to converge by returning a special value (e.g., an empty string) or raising an exception after a maximum number of guesses. |
wordlist contains duplicate words | The algorithm should still work correctly as the duplicate words will be treated as separate guesses. |
All words in wordlist are identical | The algorithm might get stuck if all words are identical; add a check to handle this special scenario by returning or by randomly picking another word. |
Very large wordlist (performance) | Ensure that the number of guesses is bounded to avoid infinite loops and/or excessive processing time. |
secret word is not in the wordlist | Handle the case where the secret word is not in the list by returning a specific error or exiting after a maximum number of attempts. |