The interview problem

Practice prompt

A hidden Markov model describes two operating modes that emit observations. Compute a short sequence probability and explain why Viterbi decoding answers a different question.

Original practice, not a reported company interview. All scenarios and worked numbers are illustrative. Interview date: not applicable. Sources retrieved September 30, 2026.

1. Scope and assumptions

Assume two hidden states, a first-order Markov transition model and observations conditionally independent given the state sequence. Parameters are supplied; fitting them is outside this exercise.

2. Worked approach

The forward recurrence sums probability mass over all predecessor states, then multiplies by the current emission likelihood. The sequence likelihood sums the final forward values. Viterbi replaces the predecessor sum with a maximum and retains backpointers to recover one most-probable joint path.

Implement long sequences using scaled forward values or log-sum-exp to avoid underflow. Keep transition direction and initial-state conventions explicit. A collection of individually most probable marginal states need not be the same path as Viterbi's globally best sequence.

3. Calculate the example

Let initial state probabilities be (0.5,0.5), transitions stay with probability 0.8 and switch with 0.2, and probability of observation x be (0.9,0.2). For observations x,x, first forward values are (0.45,0.10).

Second values are ((0.45×0.8+0.10×0.2)×0.9, (0.45×0.2+0.10×0.8)×0.2)=(0.342,0.034).

Sequence probability is 0.376. The best path is state1,state1 with joint probability 0.5×0.9×0.8×0.9=0.324, smaller than the sum over all paths.

4. Failure cases and trade-offs

Using a max when computing likelihood discards valid paths. Multiplying many tiny values underflows to zero. State labels can permute without changing the model, so assigning a semantic name requires evidence beyond an arbitrary state index.

An HMM is interpretable and supports efficient dynamic programming but its duration and conditional-independence assumptions may be too restrictive. More states can model richer behavior while increasing ambiguity and fitting difficulty.

5. Validate the result

Enumerate all four two-state paths and verify their sum and maximum match the recurrences. Check transition rows sum to one and compare scaled and log implementations on long synthetic sequences.

6. Interview follow-ups and self-review

How would missing observations change the emission step? Why can the best path change after seeing future observations?

Reward sum-versus-max reasoning, path arithmetic, numerical stability and careful state interpretation.

Sources and related practice

Technical reference: hmmlearn.readthedocs.io — Official HMM algorithm background; two-state example is original.

Exact publication/update date not established in this review. Retrieved September 30, 2026. This reference supports the named technical concept; the proposed workflow and examples are original, not a documented vendor implementation or interview report.

Continue with Reason about stationary Markov distributions; Derive expectation maximization for a mixture.

Continue your preparation

Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.