PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Machine Learning/Cresta

Implement greedy and beam decoding

Last updated: Mar 29, 2026

Quick Overview

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.

  • medium
  • Cresta
  • Machine Learning
  • Machine Learning Engineer

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.

Related Interview Questions

  • Design sequence decoding with greedy and beam search - Cresta (medium)
|Home/Machine Learning/Cresta

Implement greedy and beam decoding

Cresta logo
Cresta
Sep 6, 2025, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenMachine Learning
53
0

Implement Greedy and Beam Search Decoders over Next-Token Probabilities

Context

You are given a directed token graph represented as a Python dictionary mapping each token to a dictionary of next-token probabilities:

  • transitions: token -> { next_token: probability }
  • A start token (e.g., "I" or " <START> ")
  • A terminal token '.' that ends the sequence, or a token with no outgoing options (missing key or empty dict)

Probabilities are non-negative and intended to be conditional next-token probabilities. You may assume they are normalized per node, but your implementation should be robust if they are not perfectly normalized.

Tasks

  1. Greedy Search
  • At each step, pick the highest-probability next token.
  • Stop when you reach '.' or a token with no outgoing options.
  • Provide both iterative and recursive implementations.
  • Return the generated token sequence (including the start token).
  1. Beam Search (BFS-style)
  • Use beam size k. From each partial sequence, expand to all next tokens, then keep only the top-k partial sequences ranked by cumulative log-probability.
  • Stop when all beams end with '.' or when no continuations exist.
  • Return the highest-probability completed sequence, and optionally all completed sequences.

Requirements

  • Provide clear function signatures.
  • Specify deterministic tie-breaking for equal probabilities/scores.
  • Include time and space complexity in terms of: sequence length L, branching factor b (max outgoing options per token), vocabulary size |V|, and beam size k.
Loading comments...

Browse More Questions

More Machine Learning•More Cresta•More Machine Learning Engineer•Cresta Machine Learning Engineer•Cresta Machine Learning•Machine Learning Engineer Machine Learning

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.