KatInterview

Adaptive Word Guessing Minimax Strategy

Dynamic ProgrammingAuto-graded · 21 test cases
HardAuto-gradedDynamic Programmingdynamic-programmingbit-manipulationminimax

Last updated

Find the minimum number of worst-case letter guesses required to isolate a hidden word against an adversarial host.

You are given a list of unique lowercase English words, where all words have equal length.

A secret word is selected from this set. You interact with an adversarial opponent by making letter guesses one turn at a time. Each turn, you choose a single lowercase letter from 'a' to 'z' that has not been guessed yet.

The opponent responds by revealing all index positions where the guessed letter appears within a secret candidate word. However, the opponent is allowed to dynamically switch the active candidate word after any turn, provided the new word matches all feedback patterns revealed in previous turns. The opponent will always choose a response that maximizes the total number of guesses you must make.

Your goal is to uniquely isolate the secret word (i.e., reduce the set of possible consistent words to exactly one).

Assuming both you and the opponent play optimally, calculate the minimum number of letter guesses required to guarantee the secret word is identified in the worst-case scenario.

Constraints

  • 1 <= words.length <= 14
  • 1 <= words[i].length <= 10
  • All strings in words consist of lowercase English letters ('a' through 'z').
  • All strings in words have the exact same length.
  • All strings in words are unique.

Examples

Example 1

Input: worst_case_guesses(['cat', 'car', 'bar'])

Output: 2

Example 2

Input: worst_case_guesses(['a', 'b', 'c'])

Output: 2

Example 3

Input: worst_case_guesses(['aa', 'ab', 'ba', 'bb'])

Output: 1

Hints

Hint 1

Framing the opponent's strategy: whenever you guess a letter, the current candidate set is partitioned into disjoint subsets according to the revealed index pattern. The opponent will always direct you toward the largest remaining branch (in terms of worst-case future turns).

Hint 2

Because you want to minimize the worst-case cost, this problem can be modeled using a minimax recurrence relation on candidate subsets.

Hint 3

Represent candidate subsets using bitmasks for fast operations and efficient state memoization.