Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Letter Feedback Comparison

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Letter Feedback Comparison. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Character AI is testing a word-guessing interaction where each guess needs precise letter feedback. Implement a function that compares a guessed word with a secret word and classifies every guessed character.

Return a list with one status per character in guess:

  • 2 means the character is correct and in the correct position.
  • 1 means the character appears in the secret word but belongs in a different position.
  • 0 means the character does not have an unused matching occurrence in the secret word.

Formal Specification

Implement word_feedback(secret, guess), where both inputs are lowercase strings of equal length. Return a list of integers with the same length as guess. Each occurrence in the secret may be matched at most once. Exact matches must take priority over misplaced matches, which is important when either word contains repeated characters.

Constraints

  • 1 <= len(secret) == len(guess) <= 10^5
  • secret and guess contain only lowercase English letters
  • Each secret character occurrence can be matched at most once
  • Exact matches take priority over misplaced matches

Function Signature

def word_feedback(secret, guess):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output