Implement a Word Guessing Game
Company: Shopify
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a developer's proficiency in string processing, input validation, state management, Wordle-style matching semantics (including duplicate-letter handling), and API design for game logic and edge cases.
Constraints
- 1 <= len(dictionary_words) <= 100000
- 0 <= len(guesses) <= 100000
- Each dictionary word and the target consists of exactly 4 lowercase English letters
- The target is guaranteed to appear in dictionary_words
- Only valid guesses count toward the 5-attempt limit
Examples
Input: (["code", "coda", "play"], "code", [1234, "coda", "code"])
Expected Output: (True, 2, ['INVALID', '111-', 'CORRECT'])
Explanation: 1234 is not a string, so it is invalid and does not use an attempt. 'coda' matches the first three positions exactly, giving '111-'. 'code' is then guessed correctly on the second valid attempt.
Input: (["bend", "ebnx", "xend", "ally"], "bend", ["ebnx", "xend", "bend"])
Expected Output: (True, 3, ['001-', '-111', 'CORRECT'])
Explanation: 'ebnx' has 'e' and 'b' misplaced, 'n' exact, and 'x' absent, so the hint is '001-'. 'xend' has only the first letter wrong, giving '-111'. The third valid guess is correct.
Input: (["lamp"], "lamp", [])
Expected Output: (False, 0, [])
Explanation: No guesses are processed, so the player does not win and uses no valid attempts.
Input: (["noon", "mono"], "noon", ["mono", "noon"])
Expected Output: (True, 2, ['-100', 'CORRECT'])
Explanation: Against target 'noon', 'mono' gets 'o' exact in position 1, 'n' misplaced in position 2, and the final 'o' misplaced in position 3 after exact matches are handled, so the hint is '-100'. The next guess is correct.
Input: (["play", "clay", "slay", "pray", "gray", "stay"], "play", ["tool", "clay", "slay", "pray", "gray", "stay", "play"])
Expected Output: (False, 5, ['INVALID', '-111', '-111', '1-11', '--11', '--11'])
Explanation: 'tool' is invalid because it is not in the dictionary. The next five guesses are valid and consume all 5 allowed attempts without finding the target, so the final guess 'play' is never processed.
Hints
- Use a set for the dictionary so you can validate guesses in O(1) average time.
- To handle duplicate letters correctly, build the hint in two passes: exact matches first, then misplaced matches using counts of the remaining unmatched target letters.