Wordle Guess Checker
Updated
Problem statement
You are checking guesses in a word game. The secret word is target, and the player submits the words in guesses one after another. Every word has 5 lowercase letters.
For each guess, give every position one mark:
'G'(green): the guess has the same letter astargetat this position.'Y'(yellow): the letters at this position differ, buttargetstill has an unused copy of the guessed letter somewhere else.'W'(white): the letters at this position differ, andtargethas no unused copy of the guessed letter left.
Repeated letters are counted, because one copy of a letter in target can account for only one position of the guess. First mark every green position; each green uses up the copy of its letter in target. Then visit the other positions from left to right: a position is yellow if target still has an unused copy of that letter, and that copy is now used up; otherwise the position is white.
The game also remembers earlier guesses. A guess that contains a ruled-out letter is rejected: its entry is the string "invalid_input" and it gets no marks.
Assumption: a letter is ruled out only when an accepted guess marks every copy of it white, which means the letter does not occur in target. A letter that is white at one position but green or yellow at another is not ruled out, and a rejected guess rules out nothing.
Return an array with one entry per guess, in order: the 5 marks of an accepted guess as one string, such as "WGWGG", or "invalid_input" for a rejected guess. Every guess is handled this way, even after a guess that is all green.
Function
scoreGuesses(target: String, guesses: String[]) → String[]Examples
Example 1
target = "maple"guesses = ["apple"]return = ["YWGGG"]Positions 2, 3 and 4 hold the same letters in both words, so they are green. "maple" has one 'p', and the green at position 2 uses it, so the 'p' at position 1 is white. The 'a' at position 0 is not in its place, but the 'a' of "maple" at position 1 is still unused, so it is yellow.
Example 2
target = "maple"guesses = ["cable", "cazle", "paple"]return = ["WGWGG", "invalid_input", "WGGGG"]In "cable", positions 1, 3 and 4 are green. 'c' and 'b' do not occur in "maple", so both are white and both are now ruled out. "cazle" contains 'c', so it is rejected; its 'z' is not ruled out, because a rejected guess rules out nothing. In "paple", positions 1 to 4 are green; the green 'p' at position 2 uses the only 'p' of "maple", so the 'p' at position 0 is white.
Example 3
target = "robot"guesses = ["error", "eager", "gator", "robot"]return = ["WYWGW", "invalid_input", "WWYGY", "GGGGG"]In "error", the 'o' at position 3 is green. 'e' does not occur in "robot", so it is white and ruled out. "robot" has one 'r' and no green uses it, so the first 'r' of the guess, at position 1, is yellow and the ones at positions 2 and 4 are white; 'r' got a yellow, so it stays allowed. "eager" contains 'e' and is rejected. In "gator", 'g' and 'a' are white, the 'o' is green, and 't' and 'r' are yellow because "robot" has an unused copy of each. The last guess matches at every position.
Constraints
target.length == 51 <= guesses.length <= 100guesses[i].length == 5targetand everyguesses[i]contain only lowercase English letters.
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- JSON ParserPHONE SCREEN · Seen Jun 2021
Similar questions at other companies
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- String Interpolation with a Missing-Value ModeMongoDB · PHONE SCREENMongoDB interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Fixed-Order Task Run Time With CooldownWaymo · PHONE SCREENWaymo interview questions
- Character Counts with Retried Remote WorkersCoalition · PHONE SCREEN