Problem · String

Choose the Next Hidden-Word Guess

Learn this problem
MediumFlexportFULLTIMEONSITE INTERVIEW

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:

  1. For a possible guess, group every remaining target candidate by the score it would return.
  2. Measure the guess by the size of its largest group.
  3. Choose the guess whose largest group is as small as possible.
  4. If several guesses tie, return the lexicographically smallest one.

Return the chosen next guess.

What the interview report shared

The report described an onsite word-guessing variant. 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, and the report mentioned an entropy-style idea. It did not provide an exact function signature, constraints, or tie-breaking rule.

Function

chooseNextWordGuess(words: String[], guesses: String[], scores: int[]) → String

Examples

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 <= 500
  • 1 <= words[i].length <= 20
  • All strings in words are unique, contain only lowercase English letters, and have equal length.
  • 0 <= guesses.length == scores.length < words.length
  • Every value in guesses appears in words.
  • 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 problems

drafts saved locally
public String chooseNextWordGuess(String[] words, String[] guesses, int[] scores) {
  // write your code here
}
words["acckzz","ccbazz","eiowzz","abcczz"]
guesses[]
scores[]
expected"acckzz"
checking account