← Microsoft Interview Insights
I knew beam search conceptually but fumbled the implementation details under pressure.
Start by contrasting greedy decoding with beam search, emphasizing how beam search maintains k hypotheses to avoid local optima. Then walk through the algorithm step-by-step: initialization, expansion, scoring with log-probabilities, pruning, and finalization. Conclude with trade-offs and practical considerations like length normalization.
Pro tip: Mention that beam search is not guaranteed to find the global optimum but often improves results; also highlight the importance of length normalization to avoid favoring shorter sequences, and discuss how to handle end-of-sequence tokens.
Start with a single hypothesis containing the start token and a score of 0 (log-probability). Set beam width k.
For each hypothesis in the beam, generate all possible next tokens from the vocabulary. Compute the new score by adding the log-probability of the token to the hypothesis score.
Collect all candidate extensions, sort them by score (higher is better), and keep the top k hypotheses. If a hypothesis ends with an end-of-sequence token, mark it as complete but keep it in the beam for now.
Continue expanding and pruning until all hypotheses in the beam are complete or a maximum length is reached. Optionally, stop early if the top hypothesis is complete and its score cannot be beaten by any incomplete hypothesis.
Among complete hypotheses, select the one with the highest score, applying length normalization if needed. Return the corresponding sequence.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.