Choose the Next Hidden-Word Guess
Updated
Problem statement
A hidden target word is guaranteed to be one of the unique strings in words. Every word has the same length.
For two words a and b, their match score is the number of indices i for which a[i] == b[i]. For example, the score between aabbcc and abcdef is 1.
You have already made the guesses in guesses. The corresponding value scores[i] is the match score returned for guesses[i] against the hidden target.
First, keep only the words that are consistent with every previous guess and score. These are the remaining candidates.
Minimax selection
Choose the next guess from the remaining candidates using this rule:
- For a possible guess, group every remaining target candidate by the score it would return.
- Measure the guess by the size of its largest group.
- Choose the guess whose largest group is as small as possible.
- If several guesses tie, return the lexicographically smallest one.
Return the chosen next guess.
Notes
The target is in the supplied word list, guesses must come from that list, and feedback counts positions where both the character and index match. The goal is to identify the target with as few calls as possible; an entropy-style heuristic is a common alternative, but this problem uses the minimax rule above.
Function
chooseNextWordGuess(words: String[], guesses: String[], scores: int[]) → StringExamples
Example 1
words = ["acckzz","ccbazz","eiowzz","abcczz"]guesses = []scores = []return = "acckzz"Using acckzz would produce scores 6, 3, 2, and 4 for the four possible targets. Every score group has size 1, so its worst group is smaller than that of any other candidate.
Example 2
words = ["acckzz","ccbazz","eiowzz","abcczz"]guesses = ["acckzz"]scores = [3]return = "ccbazz"Among the listed words, only ccbazz has exactly three positional matches with acckzz. It is therefore the only remaining target candidate and must be returned.
Example 3
words = ["aaaa","aaab","aaba","abaa","baaa"]guesses = []scores = []return = "aaab"Guessing aaaa puts the other four words into one score-3 group, so its worst group has size 4. Each of the other four guesses has a worst group of size 3; aaab is lexicographically smallest among them.
Constraints
1 <= words.length <= 5001 <= words[i].length <= 20- All strings in
wordsare unique, contain only lowercase English letters, and have equal length. 0 <= guesses.length == scores.length < words.length- Every value in
guessesappears inwords. 0 <= scores[i] < words[i].length, so the target has not already been found.- At least one word is consistent with all previous guesses and scores.
More Flexport interview questions
- Container Ledger with TradesONSITE INTERVIEW · Seen Nov 2026
- Apply Operations to Make a String EmptyPHONE SCREEN · Seen Nov 2026
- Max Area of Island Without Changing the GridPHONE SCREEN · Seen Nov 2026
- Count Good SubsequencesOA · Seen Jun 2024
- Distinct MovesOA · Seen Jun 2024
- Hotspot ConnectionsOA · Seen Jun 2024
- Throw The BallOA · Seen Jun 2024
- Matrix TraversalOA · Seen Feb 2024
- Collect PointsOA · Seen Feb 2024
- Malware SpreadOA · Seen Feb 2024
Similar questions at other companies
- Choose the Next Hidden-Word GuessCharacter.AI · PHONE SCREEN
- 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
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Generate Word Guess FeedbackAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEWAnthropic interview questions