Generate Per-Position Guess Feedback
TeslaSoftware Engineer · Online AssessmentLast updated
This problem tests your ability to process strings and manage state (character frequency) to determine exact matches versus partial matches, commonly used in word-guessing games.
Problem Description
You are building the core engine for a word-guessing game. Given a secret word and a guess word of the same length, determine the feedback for each position in the guess.
The feedback for each position should be one of the following:
- 'G' (Green): The character at this position in
guessexactly matches the character at the same position insecret. - 'Y' (Yellow): The character at this position in
guessexists insecret, but not at the current position, and has not been "used up" by a 'G' or another 'Y' elsewhere. - 'X' (Gray): The character at this position in
guessdoes not exist insecret(or all instances of this character insecrethave already been accounted for).
Function Signature
def get_guess_feedback(secret: str, guess: str) -> str:
Examples
# Example 1
secret = "APPLE"
guess = "PLIER"
# Output: "XYYYX"
# P (idx 1) is Y (exists at index 1 in secret), L (idx 2) is Y, I (idx 3) is Y, E (idx 4) is Y.
# Example 2
secret = "ABCDE"
guess = "EDCBA"
# Output: "YYXYY"
# Example 3
secret = "SPEED"
guess = "DEEPS"
# Output: "YYXYY"
Constraints
len(secret) == len(guess)1 <= len(secret) <= 1000- Inputs consist of uppercase English letters.
Edge Cases
- The words are identical.
- All characters in
guessare absent fromsecret. - The words have repeating characters (e.g.,
secrethas two 'E's, butguesshas three).
Hints
Hint 1
First, perform a pass to identify all 'Green' matches and mark those characters as used.
Hint 2
Use a frequency map (or hash table) to count the remaining characters in the `secret` that are available to be matched.
Hint 3
Perform a second pass to identify 'Yellow' matches by checking if the current character exists in the remaining frequency map.
Follow-up questions
- How would you optimize this if you were validating thousands of guesses against the same secret word per second?
- What changes would you make if the guess word could be of a different length than the secret word?
- If the game allowed for case-insensitive matching, how would you handle the input normalization?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.