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
| Item | Detail |
|---|---|
| secret = acdz, guess = cxdz | ybgg |
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)Complexity
| Item | Detail |
|---|---|
| Brute force | O(n^2) |
| Optimized | O(n + alphabet) time, O(alphabet) space |
Edge Cases
- Duplicate letters
- All greens
- No matches
- Guess char appears as green elsewhere
Follow-ups
- Support Unicode letters
- Return counts of each color
Nearest Practice References
- Wordle
- Bulls and Cows
Common Mistakes
- Copying the nearest LeetCode solution without checking the changed rule.
- Skipping duplicate or boundary cases.
- Not stating the brute force before the optimized approach.