FastPrepGuess the Word

Guess the Word

Google logoGoogle● HardNEW GRADOAPHONE SCREEN
Learn

Problem statement

You are given an array words of distinct six-letter lowercase words and a secret word that appears in words.

A guess receives the number of positions whose letters exactly match secret. Simulate the following deterministic solver for at most 10 guesses:

  1. Start with every word as a possible secret.
  2. For each current candidate guess, partition the current candidates by feedback value from 0 through 6.
  3. Choose the guess whose largest partition is smallest. Break ties by lexicographically smaller word.
  4. If all six positions match, return the one-based guess number. Otherwise retain only candidates with the observed feedback and continue.

Return -1 if this policy does not guess secret within ten attempts.

Function

guessesToFindSecret(words: String[], secret: String) → int

Examples

Example 1

words = ["acckzz","ccbazz","eiowzz","abcczz"]secret = "acckzz"return = 1

The minimax rule chooses acckzz first because every feedback bucket has size at most one.

Example 2

words = ["acckzz","ccbazz","eiowzz","abcczz"]secret = "eiowzz"return = 2

The first guess acckzz receives feedback 2, leaving only eiowzz, which is guessed next.

Example 3

words = ["aaaaaa","bbbbbb","cccccc"]secret = "cccccc"return = 3

Every distinct pair has feedback zero, so lexicographic tie-breaking guesses aaaaaa, then bbbbbb, then cccccc.

Constraints

  • 1 <= words.length <= 100.
  • Every word has exactly six lowercase English letters.
  • All words are distinct.
  • secret appears in words.

More Google problems

See Google hiring insights
public int guessesToFindSecret(String[] words, String secret) {
    // Write your code here.
}
words["acckzz","ccbazz","eiowzz","abcczz"]
secret"acckzz"
expected1
Checking account…