Implement Beam Search Decoding

Last updated: September 4, 2025

Quick Overview

Implement beam search over a vocabulary given log-probability distributions. Tests core LLM inference knowledge including token sequences, pruning, and heap operations.

Perplexity
Coding & Algorithms
Software Engineer
Perplexity
September 4, 2025
Software Engineer
Coding Round
Coding & Algorithms
Hard

8

4

1,853 solved


Implement beam search over a vocabulary given log-probability distributions. Tests core LLM inference knowledge including token sequences, pruning, and heap operations.

This AI-specific coding problem tests understanding of how LLM text generation works at a fundamental level. Common in Perplexity's ML-adjacent coding rounds.

What the Interviewer Expects
  • Implement beam search with configurable beam width
  • Handle log-probability arithmetic correctly
  • Use a heap for efficient beam management
  • Handle end-of-sequence tokens properly
  • Discuss time and space complexity
Key Topics to Cover
Beam search
LLM decoding
Priority queues
Token sequences
Log-probability arithmetic
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add length normalization?
  • What are the trade-offs between beam search and nucleus sampling?
  • How would you implement diverse beam search?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires implementing beam search decoding, a common algorithm in natural language processing (NLP) used for generating sequences with a focus on retaining the most probable tokens based o...

Approach
  1. Initialization: Start with the initial token (usually a start token) and its log-probability. Initialize a priority queue (min-heap) to store the sequences and their cumulative log-probabilitie...

Submit Your Answer
Markdown supported

Related Questions