Design sequence decoding with greedy and beam search

Quick Overview

This interview question evaluates core ML concepts, assumptions, math intuition, training/evaluation trade-offs, and practical failure modes in a realistic interview setting. A strong answer for Design sequence decoding with greedy and beam search states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Design sequence decoding with greedy and beam search

Company: Cresta

Role: Software Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Technical Screen

You are given a probabilistic next-token dictionary D that maps each token t to a dictionary of candidate next tokens {u: p(u|t)}. Use '<START>' as the start token and '.' as the end-of-sequence token. 1) Implement greedy decoding that, starting from '<START>', repeatedly selects the highest-probability next token until '.' is produced or no continuation exists. Provide both a recursive and an iterative version, and return the sequence of tokens (include '.' if produced). Analyze time and space complexity. 2) Implement beam search decoding with beam width k using a BFS-style expansion: at each decoding step, maintain up to k highest-scoring partial sequences ranked by cumulative log-probability, expand each by its top-k next tokens from D, and continue until all beams end with '.'. Return the best sequence and optionally the top-k sequences. Explain how you handle ties, tokens missing from D, potential cycles or dead-ends, score normalization (e.g., length normalization), and termination criteria. Compare greedy and beam search in terms of quality, complexity, and typical use cases, and demonstrate your implementations on a small example dictionary.

Quick Answer: This interview question evaluates core ML concepts, assumptions, math intuition, training/evaluation trade-offs, and practical failure modes in a realistic interview setting. A strong answer for Design sequence decoding with greedy and beam search states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

|Home/Machine Learning/Cresta
Cresta logo
Cresta
Aug 8, 2025, 12:00 AM
mediumSoftware EngineerTechnical ScreenMachine Learning
90
0

Context

You are given a probabilistic next-token dictionary D that maps each token t to a dictionary of candidate next tokens and their conditional probabilities: D[t] = {u: p(u | t)}. Use '<START>' as the start token and '.' as the end-of-sequence token. Assume probabilities in each D[t] sum to 1. If a token does not appear as a key in D, it has no continuations (dead-end).

Tasks

  1. Greedy Decoding
  • Starting from ' <START> ', repeatedly select the highest-probability next token until '.' is produced or no continuation exists.
  • Provide both a recursive and an iterative version.
  • Return the sequence of tokens after ' <START> ' (include '.' if produced).
  • Analyze time and space complexity.
  1. Beam Search Decoding (beam width k)
  • Use a BFS-style expansion: at each decoding step, maintain up to k highest-scoring partial sequences ranked by cumulative log-probability.
  • Expand each active beam by its top-k next tokens from D; continue until all beams end with '.'.
  • Return the best sequence and optionally the top-k sequences.
  • Explain how you handle:
    • Ties
    • Tokens missing from D (dead-ends)
    • Potential cycles
    • Score normalization (e.g., length normalization)
    • Termination criteria
  • Compare greedy vs. beam search in quality, complexity, and typical use cases.
  • Demonstrate your implementations on a small example dictionary.

Clarifying Questions to Ask Guidance

  • Clarify the task, data shape, labels, constraints, and evaluation metric.
  • State assumptions behind the math or modeling technique you choose.
  • Connect theory to practical training, debugging, and deployment implications.

What a Strong Answer Covers Guidance

  • Correct definitions and formulas where the prompt requires them.
  • A practical explanation of how the method behaves on real data.
  • Trade-offs, failure modes, diagnostics, and mitigation strategies.
  • Evaluation choices that match the product or modeling objective.

Follow-up Questions Guidance

  • How would noisy labels, class imbalance, or distribution shift affect the answer?
  • What would you monitor after deployment?
  • Which baseline would you compare against first?
Loading comments...