Find a secret word via match feedback
Company: Moveworks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates algorithmic search, pattern-matching, and information-elimination skills in an interactive Mastermind-style word-guessing scenario, testing string comparison and combinatorial reasoning under constrained feedback.
Constraints
- 1 <= |wordlist| <= 1000
- 1 <= L <= 10 (all words share the same length)
- Words contain only lowercase letters and are unique
- The secret is guaranteed to be present in wordlist
- Maximum allowed guesses G (e.g. G = 10)
Examples
Input: (['abc', 'abd', 'aac', 'xyz'], 'abd')
Expected Output: 'abd'
Explanation: Words differ at one or two positions; elimination narrows to 'abd' within the guess budget.
Input: (['a'], 'a')
Expected Output: 'a'
Explanation: Singleton wordlist: the only candidate is the secret.
Input: (['cat', 'cot', 'cut', 'cog', 'dog'], 'dog')
Expected Output: 'dog'
Explanation: Several words share a common prefix/suffix; feedback buckets separate them.
Input: (['hello', 'jelly', 'jolly', 'belly', 'hilly'], 'jolly')
Expected Output: 'jolly'
Explanation: Length-5 words that pairwise differ in only a couple of positions, stressing the elimination logic.
Input: (['aaaa', 'aaab', 'aaba', 'abaa', 'baaa'], 'aaba')
Expected Output: 'aaba'
Explanation: All candidates are one position away from 'aaaa'; the minimax guess still isolates the secret.
Input: (['zz', 'zy', 'yz', 'yy'], 'yy')
Expected Output: 'yy'
Explanation: Length-2 boundary: every combination of two letters; feedback of 0 from 'zz' pins the answer.
Input: (['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy'], 'abcde')
Expected Output: 'abcde'
Explanation: Fully disjoint words: the secret is the only word giving a positive match against itself.
Hints
- After each guess you can throw away every word that would NOT have produced the same feedback as the secret did — only words consistent with all feedback can be the secret.
- Don't guess blindly: pick the guess whose feedback splits the remaining candidates most evenly. Concretely, for each candidate guess, bucket the other candidates by their match-score and choose the guess that minimizes the largest bucket (minimax).
- match(guess, candidate) is symmetric and cheap (compare characters position by position). Recompute it lazily while pruning.
- When match returns L (all positions match), the guess IS the secret — stop and return it.