Evaluate Word-Guess Feedback: Label Each Letter Correct, Misplaced or Wrong

Quick Overview

Given a secret word and a guess of the same length, label each letter of the guess as correct, misplaced or wrong. It tests letter counting with a frequency map, careful handling of repeated letters, and why exact matches have to be resolved before misplaced ones.

Evaluate Word-Guess Feedback: Label Each Letter Correct, Misplaced or Wrong

Company: Patreon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two strings `secret` and `guess` of equal length, return a feedback list that labels each position of `guess` by how its character compares to `secret`. Each label is one of: - `"correct"` when the character in `guess` matches the character in `secret` at the same position. - `"misplaced"` when the character appears somewhere in `secret` but not at this position. - `"wrong"` when the character does not appear in `secret` at all, or all of its occurrences have already been accounted for. Duplicate letters must be handled carefully: no letter in `secret` may be credited (as `"correct"` or `"misplaced"`) more times than it actually appears in `secret`. Exact matches (`"correct"`) take priority over partial matches (`"misplaced"`). ### Function Signature ```python def evaluate_guess(secret: str, guess: str) -> list[str]: ``` ### Rules - Return a list of length `len(secret)` whose entries are exactly the strings `"correct"`, `"misplaced"` or `"wrong"`. - Every position where `guess[i] == secret[i]` is `"correct"`, wherever it sits in the string. - Once the exact matches are counted, each letter has some number of occurrences in `secret` left uncredited. The other positions of `guess` are labeled from left to right: a position is `"misplaced"` if its letter still has an uncredited occurrence (which that position then uses up), and `"wrong"` otherwise. When a letter appears in `guess` more often than it can be credited, the leftmost of those positions get `"misplaced"`. ### Constraints - `1 <= len(secret) <= 10^4` - `len(guess) == len(secret)` - `secret` and `guess` consist of lowercase English letters only. ### Examples ```text Input: secret = "aabbc", guess = "aacde" Output: ["correct", "correct", "misplaced", "wrong", "wrong"] ``` Positions 0 and 1 are exact matches for `a`. The `c` at position 2 is not at that spot in `secret`, but `secret` has a `c` at position 4, so it is misplaced. `d` and `e` do not appear in `secret`. ```text Input: secret = "hello", guess = "world" Output: ["wrong", "misplaced", "wrong", "correct", "wrong"] ``` The `l` at position 3 matches `secret` exactly. The `o` at position 1 is credited with the only `o` in `secret` (position 4), so it is misplaced. `w`, `r` and `d` do not appear in `secret`. ```text Input: secret = "abbey", guess = "bobby" Output: ["misplaced", "wrong", "correct", "wrong", "correct"] ``` Positions 2 (`b`) and 4 (`y`) are exact matches. `secret` has two `b`s and the exact match uses one, so only one more `b` can be credited. It goes to the leftmost remaining `b` in `guess` at position 0, and the `b` at position 3 is wrong. `o` does not appear in `secret`.

Overview: Given a secret word and a guess of the same length, label each letter of the guess as correct, misplaced or wrong. It tests letter counting with a frequency map, careful handling of repeated letters, and why exact matches have to be resolved before misplaced ones.

Patreon logo
Patreon
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two strings secret and guess of equal length, return a feedback list that labels each position of guess by how its character compares to secret.

Each label is one of:

  • "correct" when the character in guess matches the character in secret at the same position.
  • "misplaced" when the character appears somewhere in secret but not at this position.
  • "wrong" when the character does not appear in secret at all, or all of its occurrences have already been accounted for.

Duplicate letters must be handled carefully: no letter in secret may be credited (as "correct" or "misplaced") more times than it actually appears in secret. Exact matches ("correct") take priority over partial matches ("misplaced").

Function Signature

def evaluate_guess(secret: str, guess: str) -> list[str]:

Rules

  • Return a list of length len(secret) whose entries are exactly the strings "correct" , "misplaced" or "wrong" .
  • Every position where guess[i] == secret[i] is "correct" , wherever it sits in the string.
  • Once the exact matches are counted, each letter has some number of occurrences in secret left uncredited. The other positions of guess are labeled from left to right: a position is "misplaced" if its letter still has an uncredited occurrence (which that position then uses up), and "wrong" otherwise. When a letter appears in guess more often than it can be credited, the leftmost of those positions get "misplaced" .

Constraints

  • 1 <= len(secret) <= 10^4
  • len(guess) == len(secret)
  • secret and guess consist of lowercase English letters only.

Examples

Input: secret = "aabbc", guess = "aacde"
Output: ["correct", "correct", "misplaced", "wrong", "wrong"]

Positions 0 and 1 are exact matches for a. The c at position 2 is not at that spot in secret, but secret has a c at position 4, so it is misplaced. d and e do not appear in secret.

Input: secret = "hello", guess = "world"
Output: ["wrong", "misplaced", "wrong", "correct", "wrong"]

The l at position 3 matches secret exactly. The o at position 1 is credited with the only o in secret (position 4), so it is misplaced. w, r and d do not appear in secret.

Input: secret = "abbey", guess = "bobby"
Output: ["misplaced", "wrong", "correct", "wrong", "correct"]

Positions 2 (b) and 4 (y) are exact matches. secret has two bs and the exact match uses one, so only one more b can be credited. It goes to the leftmost remaining b in guess at position 0, and the b at position 3 is wrong. o does not appear in secret.