Implement Beam Search for Language Model Decoding

Asked at NVIDIA · LLMs & Prompt Engineering · Difficulty: Medium · Last asked Jul 12, 2025

A language model generates text one token at a time. Greedy decoding always takes the single most likely next token, which can miss a sequence that is more likely overall. Beam search keeps the `beam_width` best partial sequences at every step instead of one. Implement beam search over a given language model.

Loading the problem…