← 返回博客列表
Bloomberg

Bloomberg Interview Question: Wordle Feedback Logic

2025-12-21

Problem

Implement the feedback generation for a Wordle-like game.

Given:

Return a feedback string of the same length:

Example

Target: staff
Guess:  world -> -----
Guess:  worms -> ----%
Guess:  fluff -> ---**
Guess:  staff -> *****

Key Point

You must handle duplicates correctly (e.g., staff has two f). The common pattern is a two-pass solution.

Approach (two-pass)

  1. First pass: mark exact matches (*) and remove those letters from remaining counts.
  2. Second pass: for the rest, mark % only if the letter still has remaining count.

Python (Counter-based)

from collections import Counter


def wordle_feedback(target: str, guess: str) -> str:
    n = len(target)
    res = ['-'] * n
    remaining = Counter(target)

    # exact matches
    for i in range(n):
        if guess[i] == target[i]:
            res[i] = '*'
            remaining[guess[i]] -= 1

    # present but wrong position
    for i in range(n):
        if res[i] == '-' and remaining[guess[i]] > 0:
            res[i] = '%'
            remaining[guess[i]] -= 1

    return ''.join(res)

Complexity


Need interview help? Contact OA VO Service

Need real interview questions? Contact WeChat: Coding0201 to get the questions.