Design a Wordle-style word guessing game with per-letter feedback
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement a word-guessing game similar to Wordle. The game selects a secret word and the player must guess it within a limited number of attempts.
Each guess must be a valid word from a provided dictionary and must match the length of the secret word. After each valid guess, the game provides per-letter feedback: `correct` if the letter is in the right position, `misplaced` if the letter exists in the secret word but at a different position, and `wrong` if the letter does not appear in the secret word.
Duplicate letters are handled carefully: a letter in the guess is only marked as `misplaced` if there is a corresponding unmatched occurrence of that letter in the secret word. The game ends when the player guesses correctly or exhausts all attempts.
Implement the following:
- `WordGuessingGame(secretWord: str, dictionary: list[str], maxAttempts: int)`: Start a game with the given secret word, dictionary and number of attempts.
- `guess(word: str) -> str`: Submit a guess. Returns comma-separated feedback per letter (`correct`, `misplaced`, `wrong`), or an error string: `game_over` if the game has ended, `invalid_length` if the word length does not match, `not_in_dictionary` if the word is not in the dictionary.
- `getGameState() -> str`: Returns the current game state: `active`, `won`, or `lost`.
- `getAttemptsLeft() -> int`: Returns the number of remaining attempts.
- `getGuessHistory() -> str`: Returns a semicolon-separated string of all past guesses with their feedback in the format `word:feedback`. Returns `empty` if no guesses have been made.
`guess` runs its checks in the order listed (game over, then length, then dictionary) and returns the first error that applies. A rejected guess does not use an attempt and is not added to the history.
```hint Repeated letters
Score the guess "alley" against the secret "apple" by hand. The secret has one "l" and the guess has two. Which "l" should be marked misplaced, and what must you know about the rest of the word before you can decide?
```
```hint Keeping track of what is left
For each guess letter, searching the secret for an unused match works but gets fiddly. Think about what a per-letter count of the secret letters that are still unmatched would give you.
```
### Examples
Example 1:
```python
game = WordGuessingGame("apple", ["apple", "alley", "paper", "lapel", "happy"], 4)
game.guess("ap") # -> "invalid_length" (no attempt used)
game.guess("zzzzz") # -> "not_in_dictionary" (no attempt used)
game.guess("alley") # -> "correct,misplaced,wrong,misplaced,wrong"
game.guess("paper") # -> "misplaced,misplaced,correct,misplaced,wrong"
game.guess("APPLE") # -> "correct,correct,correct,correct,correct"
game.getGameState() # -> "won"
game.getAttemptsLeft() # -> 1
game.guess("happy") # -> "game_over"
game.getGuessHistory()
# -> "alley:correct,misplaced,wrong,misplaced,wrong;paper:misplaced,misplaced,correct,misplaced,wrong;apple:correct,correct,correct,correct,correct"
```
In "alley", the first "l" takes the only unmatched "l" of "apple", so the second "l" is wrong. The "e" is misplaced because the secret's "e" sits at the last position, where the guess has "y". In "paper", the "p" at index 2 is an exact match, which leaves the secret's other "p" (index 1) for the "p" at index 0. "APPLE" is lowercased before any check, so it wins and is stored in the history as "apple". Three valid guesses out of four leave one attempt.
Example 2:
```text
Input:
["WordGuessingGame", "guess", "guess", "getGameState", "getAttemptsLeft", "guess", "getGameState"]
[["cat", ["cat", "bat", "hat", "mat", "sat", "rat", "car", "cap", "can", "cab"], 2], ["bat"], ["hat"], [], [], ["cat"], []]
Output: [null, "wrong,correct,correct", "wrong,correct,correct", "lost", 0, "game_over", "lost"]
```
```python
game = WordGuessingGame("cat", [...], 2) # secret word is "cat", 2 attempts
game.guess("bat") # -> "wrong,correct,correct" ("a" and "t" correct, "b" wrong)
game.guess("hat") # -> "wrong,correct,correct" ("a" and "t" correct, "h" wrong)
game.getGameState() # -> "lost" (2 attempts used, none left)
game.getAttemptsLeft() # -> 0
game.guess("cat") # -> "game_over" (the game already ended)
game.getGameState() # -> "lost"
```
### Constraints & Assumptions
- `1 <= maxAttempts <= 10`
- `3 <= len(secretWord) <= 8`
- All words consist of lowercase English letters.
- The dictionary always contains the secret word.
- Guesses are converted to lowercase before any check.
### Clarifying Questions to Ask
- Can the player submit the same valid word twice, and does a repeat use an attempt?
- Are rule changes likely later, such as a hard mode where revealed letters must be reused, so that scoring should sit apart from the game state?
- Does one object serve one player's single game, or does the design need to cover many games at once?
### What a Strong Answer Covers
- Correct feedback when letters repeat in the guess, the secret, or both, with exact matches resolved before misplaced ones.
- Validation in the stated order, with rejected guesses leaving the attempt count and history unchanged.
- State transitions that count a correct guess on the last attempt as a win and accept no guesses once the game is over.
- A split between the scoring logic and the game state, so scoring can be tested on its own.
- The cost of each guess and the memory held by the dictionary and the history.
### Follow-up Questions
- How would you add a hard mode where every later guess must use the letters already revealed?
- How would you write a solver that picks the guess that narrows the remaining candidate words the most?
- How would you run this for many players at once, with game state kept on a server?
Overview: Design and implement a Wordle-style word guessing game that validates guesses against a dictionary, scores each letter as correct, misplaced or wrong, and tracks attempts, game state and guess history. It tests careful handling of repeated letters, the order of input checks and clean object design.