Google Wordle Color String | DSA Interview Patterns - Study Chapter | QuizMaker

Turn the Google Wordle Color String interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

Read
28m
Type
Chapter
Access
Free

Course

DSA Interview Patterns Roadmap

Topic

Company Asked Variants

Learning Outcome

Turn the Google Wordle Color String interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

Original Interview Statement

Given secret and guess of equal length, return b/y/g feedback with one secret character used at most once.

Examples

ItemDetail
secret = acdz, guess = cxdzybgg

Brute Force Approach

For every guess character, search secret for an unused same character. This is O(n^2) and easy to get wrong with duplicates.

Optimized Approach

First mark greens, then count remaining secret letters. Use those counts to assign yellows exactly once.

Exact Pseudocode

mark all greens
count non-green secret chars
for each non-green guess char:
  if count exists: mark yellow and decrement
  else mark black

Reference Code

from collections import Counter

def color(secret, guess):
    n = len(secret)
    ans = ['b'] * n
    left = Counter()
    for i in range(n):
        if guess[i] == secret[i]:
            ans[i] = 'g'
        else:
            left[secret[i]] += 1
    for i in range(n):
        if ans[i] == 'g':
            continue
        if left[guess[i]] > 0:
            ans[i] = 'y'
            left[guess[i]] -= 1
    return ''.join(ans)
string color(string secret, string guess) {
    int n = secret.size();
    string ans(n, 'b');
    vector<int> left(26, 0);
    for (int i = 0; i < n; i++) {
        if (secret[i] == guess[i]) ans[i] = 'g';
        else left[secret[i] - 'a']++;
    }
    for (int i = 0; i < n; i++) {
        if (ans[i] == 'g') continue;
        int c = guess[i] - 'a';
        if (left[c] > 0) {
            ans[i] = 'y';
            left[c]--;
        }
    }
    return ans;
}
public static String color(String secret, String guess) {
    int n = secret.length();
    char[] ans = new char[n];
    Arrays.fill(ans, 'b');
    int[] left = new int[26];
    for (int i = 0; i < n; i++) {
        if (secret.charAt(i) == guess.charAt(i)) ans[i] = 'g';
        else left[secret.charAt(i) - 'a']++;
    }
    for (int i = 0; i < n; i++) {
        if (ans[i] == 'g') continue;
        int c = guess.charAt(i) - 'a';
        if (left[c] > 0) {
            ans[i] = 'y';
            left[c]--;
        }
    }
    return new String(ans);
}

Complexity

ItemDetail
Brute forceO(n^2)
OptimizedO(n + alphabet) time, O(alphabet) space

Edge Cases

Follow-ups

Nearest Practice References

Common Mistakes

Tags

Open on QuizMaker