Implement greedy and beam decoding
Company: Cresta
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Technical Screen
Given a dictionary that maps each token to a dictionary of next-token probabilities (token -> {next_token: prob}) and a start token (e.g., "I" or "<START>"), implement two decoders for sequence generation:
(
1) Greedy search: at each step, pick the highest-probability next token and continue until you hit a terminal token '.' or a token with no outgoing options. Provide both recursive and iterative implementations and return the generated token sequence.
(
2) Beam search: using a BFS-style expansion with beam size k, from each partial sequence keep only the top-k continuations at each step (ranked by cumulative log-probability); stop when all beams end with '.' or no continuations exist. Return the highest-probability completed sequence, and optionally all completed sequences. Clearly specify function signatures, tie-breaking for equal probabilities, and analyze time and space complexity in terms of sequence length L, branching factor b, vocabulary size |V|, and beam size k.
Quick Answer: This question evaluates understanding of sequence decoding and search algorithms (greedy and beam search), probabilistic next-token handling, deterministic tie-breaking, and algorithmic analysis including time and space complexity.