← Questions

Per-position feedback for a guess against a secret word

0
TeslaCodingLast asked May 2026

A coding interview question asked in Tesla's second technical round, last asked May 2026. It tests strings, hash maps and simulation.

Score a guess against a secret word, as in Wordle. Both words have the same length. For each position of the guess, return one mark:

  • G — the letter is in the secret at this position.
  • Y — the letter is in the secret but at a different position.
  • B — the letter is not in the secret, or every copy of it in the secret is already used by other marks.

Repeated letters must be counted correctly: a letter is marked G or Y at most as many times as it appears in the secret, and exact matches take priority.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • feedback(secret: str, guess: str) -> str — returns a string of G, Y and B, one per position.

Example

feedback("apple", "paper")  ->  "YYGYB"
feedback("crane", "eerie")  ->  "BBYBG"    # the last e is exact; crane has one e, so the first two e's get B
feedback("speed", "erase")  ->  "YBBYY"    # speed has two e's, so both e's in the guess get Y

Constraints

  • secret and guess have the same length and hold lowercase letters only.

Clarifying questions

  • When a letter appears more times in the guess than in the secret, which copies get Y? (Usual rule: left to right, after exact matches.)
  • Is the input always the same length and lowercase?
  • Must the guess be a valid word from a dictionary?

What a strong answer covers

  • Two passes: first mark exact matches and count the secret's unmatched letters; then, left to right, give Y while that letter's count is positive and decrement it.
  • Why a single pass gets repeated letters wrong (a Y can steal the copy a later G needs).
  • O(n) time, O(alphabet) space.
  • Tests with repeated letters in the guess, in the secret, and in both.

Follow-ups

  • Given a list of past guesses and their feedback, filter a word list to the words still possible.
  • Pick the next guess that best splits the remaining candidates.
  • Support Unicode letters or case-insensitive matching.

Where it's asked

What it tests

strings · hash maps · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding