Implement feedback for word guessing game
Problem Description
You are tasked with implementing the core logic for a simple word-guessing game. The game involves:
- A dictionary: A list of distinct lowercase strings, all of the same length
L, representing valid words. - A secret word: One of the words from the provided dictionary.
- A player's guess word.
For each guess, your function must return feedback consisting of two components:
exact_matches: The number of positionsiwhere the character in thesecretword at indexiis identical to the character in theguessword at indexi.misplaced_matches: The number of letters that appear in both thesecretandguesswords but in different positions, without double-counting any letter.
More formally for misplaced_matches:
After identifying and accounting for exact_matches, consider the remaining characters in both secret and guess (those not contributing to an exact match). For each distinct character c, count its occurrences in the remaining secret characters and its occurrences in the remaining guess characters. The contribution to misplaced_matches for character c is the minimum of these two counts. The total misplaced_matches is the sum of these minimums across all distinct characters.
If the guessed word is not present in the dictionary, the function should clearly indicate this invalid state.
Requirements
Implement a function with the following signature and behavior:
get_feedback(dictionary: List[str], secret: str, guess: str) -> (int exact_matches, int misplaced_matches)
- If
guessis not found within thedictionary, the function should signal an invalid guess. This can be achieved, for example, by raising an exception or returning a special sentinel value like(-1, -1). - Otherwise, return the pair
(exact_matches, misplaced_matches)as defined above.
Assumptions
You can assume the following constraints:
- The
dictionarywill contain between 1 and 10^5 words (1 <= len(dictionary) <= 10^5). - All words in the
dictionary, as well as thesecretandguesswords, will have the same lengthL, where1 <= L <= 20. - All words consist solely of lowercase English letters ('a' through 'z').
Example
Consider the following scenario:
dictionary = ["apple", "angle", "ample"]secret = "apple"guess = "alley"(Assume "alley" is a valid word in the dictionary for this example.)
Let's trace the feedback calculation:
secret:a p p l eguess:a l l e y
-
Exact Matches: Only the first character 'a' matches in the same position (
secret[0] == guess[0]).exact_matches = 1
-
Remaining Letters (after exact matches):
- From
secret:p, p, l, e(at indices 1, 2, 3, 4) - From
guess:l, l, e, y(at indices 1, 2, 3, 4)
- From
-
Misplaced Matches:
- Character 'l': Appears 1 time in remaining
secret, 2 times in remainingguess.min(1, 2) = 1. - Character 'e': Appears 1 time in remaining
secret, 1 time in remainingguess.min(1, 1) = 1. - Other characters ('p', 'y') do not appear in both remaining sets.
misplaced_matches = 1 ('l') + 1 ('e') = 2
- Character 'l': Appears 1 time in remaining
Therefore, for secret = "apple" and guess = "alley", the feedback is (1, 2).
Your task is to design and implement an efficient algorithm to compute this feedback for a single guess.
AI interview prep
Core dilemma
Efficiently and correctly calculating `misplaced_matches` after `exact_matches` while ensuring the dictionary lookup for guess validity scales for a large dictionary (N=10^5) under potential high-throughput scenarios.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement feedback for word guessing game
Problem Description
You are tasked with implementing the core logic for a simple word-guessing game. The game involves:
- A dictionary: A list of distinct lowercase strings, all of the same length
L, representing valid words. - A secret word: One of the words from the provided dictionary.
- A player's guess word.
For each guess, your function must return feedback consisting of two components:
exact_matches: The number of positionsiwhere the character in thesecretword at indexiis identical to the character in theguessword at indexi.misplaced_matches: The number of letters that appear in both thesecretandguesswords but in different positions, without double-counting any letter.
More formally for misplaced_matches:
After identifying and accounting for exact_matches, consider the remaining characters in both secret and guess (those not contributing to an exact match). For each distinct character c, count its occurrences in the remaining secret characters and its occurrences in the remaining guess characters. The contribution to misplaced_matches for character c is the minimum of these two counts. The total misplaced_matches is the sum of these minimums across all distinct characters.
If the guessed word is not present in the dictionary, the function should clearly indicate this invalid state.
Requirements
Implement a function with the following signature and behavior:
get_feedback(dictionary: List[str], secret: str, guess: str) -> (int exact_matches, int misplaced_matches)
- If
guessis not found within thedictionary, the function should signal an invalid guess. This can be achieved, for example, by raising an exception or returning a special sentinel value like(-1, -1). - Otherwise, return the pair
(exact_matches, misplaced_matches)as defined above.
Assumptions
You can assume the following constraints:
- The
dictionarywill contain between 1 and 10^5 words (1 <= len(dictionary) <= 10^5). - All words in the
dictionary, as well as thesecretandguesswords, will have the same lengthL, where1 <= L <= 20. - All words consist solely of lowercase English letters ('a' through 'z').
Example
Consider the following scenario:
dictionary = ["apple", "angle", "ample"]secret = "apple"guess = "alley"(Assume "alley" is a valid word in the dictionary for this example.)
Let's trace the feedback calculation:
secret:a p p l eguess:a l l e y
-
Exact Matches: Only the first character 'a' matches in the same position (
secret[0] == guess[0]).exact_matches = 1
-
Remaining Letters (after exact matches):
- From
secret:p, p, l, e(at indices 1, 2, 3, 4) - From
guess:l, l, e, y(at indices 1, 2, 3, 4)
- From
-
Misplaced Matches:
- Character 'l': Appears 1 time in remaining
secret, 2 times in remainingguess.min(1, 2) = 1. - Character 'e': Appears 1 time in remaining
secret, 1 time in remainingguess.min(1, 1) = 1. - Other characters ('p', 'y') do not appear in both remaining sets.
misplaced_matches = 1 ('l') + 1 ('e') = 2
- Character 'l': Appears 1 time in remaining
Therefore, for secret = "apple" and guess = "alley", the feedback is (1, 2).
Your task is to design and implement an efficient algorithm to compute this feedback for a single guess.