Katkat

Generate Per-Position Guess Feedback

TeslaSoftware Engineer · Online Assessment

Last 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 guess exactly matches the character at the same position in secret.
  • 'Y' (Yellow): The character at this position in guess exists in secret, 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 guess does not exist in secret (or all instances of this character in secret have 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 guess are absent from secret.
  • The words have repeating characters (e.g., secret has two 'E's, but guess has 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?