Generate Color Feedback for a Word Guess
Company: Ngrok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Generate Color Feedback for a Word Guess
Given an answer string and a guess of the same length, produce one color for each guessed character:
- `"green"` if the character matches the answer at the same position.
- `"yellow"` if it does not match at that position but appears anywhere in the answer.
- `"red"` if it does not appear anywhere in the answer.
Return the colors in guess order.
### Function Signature
```python
def color_guess(answer: str, guess: str) -> list[str]:
...
```
### Constraints
- `1 <= len(answer) == len(guess) <= 100_000`
- Inputs contain lowercase English letters.
- This task uses simple presence semantics: yellow matches are not consumed. If a letter appears anywhere in the answer, every misplaced occurrence of that letter in the guess is yellow.
### Examples
```text
Input: answer = "candy", guess = "dandy"
Output: ["yellow", "green", "green", "green", "green"]
Input: answer = "candy", guess = "zandy"
Output: ["red", "green", "green", "green", "green"]
Input: answer = "aba", guess = "bbb"
Output: ["yellow", "green", "yellow"]
```
### Clarifications
- Check positional equality before checking whether a character appears elsewhere.
- The duplicate-letter rule intentionally differs from count-limited Wordle scoring.
Quick Answer: Generate green, yellow, or red feedback for each character in a word guess based on positional equality and presence anywhere in the answer. Follow the stated presence semantics, where misplaced duplicate letters remain yellow rather than consuming a limited match count.
For equal-length lowercase strings, mark each guess position green for an exact match, yellow for a misplaced character present anywhere in the answer, or red when absent.
Constraints
- 1 <= len(answer) == len(guess) <= 100000
- Inputs contain lowercase English letters
- Yellow uses simple presence rather than count-limited consumption
Examples
Input: {'answer': 'candy', 'guess': 'dandy'}
Expected Output: ['yellow', 'green', 'green', 'green', 'green']
Explanation: The misplaced d is present elsewhere in the answer.
Input: {'answer': 'candy', 'guess': 'zandy'}
Expected Output: ['red', 'green', 'green', 'green', 'green']
Explanation: The unmatched z is absent from the answer.
Hints
- Check exact positional equality first.
- Precompute answer-character membership for misplaced guesses.