Implement Wordle-style word guessing solver
Company: Hudson River Trading
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Implement Wordle-style word guessing solver evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- All words in `dictionary` share the same length L.
- Words are lowercase a-z.
- `target` is the hidden word; for a solvable game it lies in `dictionary`.
- 1 <= max_attempts; an empty dictionary returns ('', 0).
- Repeated letters must be scored with exact Wordle semantics (two-pass count).
Examples
Input: (['crane', 'slate', 'trace', 'blink', 'ghost', 'lucky'], 'ghost', 6)
Expected Output: ('ghost', 2)
Explanation: Solver narrows to GHOST within the attempt limit.
Input: (['allee', 'apple', 'ample', 'amble', 'eagle', 'ladle'], 'apple', 6)
Expected Output: ('apple', 2)
Explanation: Target APPLE has duplicate P/L; repeated-letter scoring still converges.
Input: (['wordy'], 'wordy', 6)
Expected Output: ('wordy', 1)
Explanation: One candidate is guessed and solved on attempt 1.
Input: ([], 'xxxxx', 6)
Expected Output: ('', 0)
Explanation: Empty dictionary returns empty guess and zero attempts.
Input: (['bat', 'cat', 'hat', 'mat', 'rat', 'sat'], 'rat', 1)
Expected Output: ('bat', 1)
Explanation: With max_attempts=1 on near-identical words, returns best guess after one attempt.
Input: (['dog', 'cat', 'pig', 'owl', 'fox', 'bee'], 'fox', 6)
Expected Output: ('fox', 2)
Explanation: Distinct-letter 3-word set converges to FOX.
Hints
- Write the feedback/scoring function first. Use TWO passes: pass 1 marks Correct (==2) positions and tallies the remaining (unmatched) letters of the target; pass 2 marks Present (==1) only while a remaining count for that letter is left. This is what makes 'one copy Present, another Absent' come out right.
- Pruning is the key insight: after guessing G and seeing feedback F, the target must be SOME word W in the candidate set for which score(G, W) == F. Filter the candidates to exactly those words — no other constraint bookkeeping is needed.
- For next-guess selection, count how many candidates contain each distinct letter, then pick the candidate maximizing the sum of those counts over its distinct letters. Break ties deterministically (e.g. lexicographically) so the run is reproducible.