KatInterview

Minimize Expected Candidates with Position-Match Feedback

Math & Bit ManipulationAuto-graded · 27 test cases
MediumAuto-gradedMath & Bit Manipulationgreedybitmaskhashing

Last updated

Tests ability to model a greedy partitioning strategy using bitmask bucketing to minimize expected remaining candidates under green-only feedback.

Problem Description

You are building a solver for a restricted word-guessing game. The game works like this: there is a secret word drawn uniformly at random from a fixed word list. You pick a guess from that same list, and for each position you learn whether your guess matches the secret exactly at that position (a "hit"). Positions that do not match give you no information at all — you cannot tell whether the letter is absent or simply in the wrong place.

After seeing the hit pattern, you eliminate every word that could not have produced it. Your goal is to choose the guess that leaves the fewest candidates on average.

Write a function optimal_word_guess(words) that accepts the word list and returns the single best guess according to this criterion.

Feedback and Filtering Rules

  • Given a guess g and a secret s, position i is a hit when g[i] == s[i].
  • The pattern for a (guess, secret) pair is the set of hit positions.
  • After a guess, only words whose pattern against the guess matches the secret's pattern remain in play.
  • Every word in the list is equally likely to be the secret.

Scoring a Guess

Because every word is equally likely, the expected number of candidates remaining after guess g is:

E[remaining | g] = (1 / n) * sum over all secrets s of |bucket(g, s)|

where bucket(g, s) is the set of words that share the same hit pattern as s against g, and n is the total number of words.

Equivalently, if the hit patterns partition the word list into groups of sizes k_1, k_2, …, then:

E[remaining | g] = (k_1² + k_2² + … ) / n

Since n is the same for every guess, minimizing the expected remaining count is the same as minimizing the sum of squared group sizes.

What to Return

Return the word from words that produces the smallest sum of squared group sizes. If two or more words tie on this score, return whichever comes first in lexicographic order.

Constraints

  • 1 <= len(words) <= 2000
  • All words have the same length L, where 1 <= L <= 10.
  • Every word consists of lowercase English letters.
  • The returned word must itself be an element of words.

Examples

Example 1

Input: optimal_word_guess(['aba', 'aca', 'ada'])

Output: 'aba'

Example 2

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

Output: 'a'

Example 3

Input: optimal_word_guess(['all', 'ill', 'ell', 'add'])

Output: 'add'

Hints

Hint 1

For a fixed guess, every secret maps to a hit pattern — the bitmask of positions where they agree. Words with the same bitmask are indistinguishable after that guess, so they form a bucket. The expected remaining count depends only on the sizes of these buckets.

Hint 2

You don't need floating-point division to compare expected values across guesses. Because n is constant, minimizing E[remaining] is the same as minimizing the integer sum of squared bucket sizes — compute that integer score directly.

Hint 3

To break ties in favor of the lexicographically smallest word, iterate over guesses in sorted order and update the best candidate only on a strictly smaller score (not ≤).