AO
Back

Beam Search Implementation

Phone ScreenPhone ScreenLast reported August 2025Low Frequency

Problem Overview

Part 1 — Beam search on the given signature

Perplexity phone screen with a Chinese interviewer, from one first-hand report (August 2025). The candidate was asked to write beam search: the signature was given, the job was to complete the code, and the unit tests had to pass. The report gives the signature and nothing else: no tests, no examples, no follow-ups, no outcome. Everything below beyond the signature is standard beam search and our setup.

Given — the signature (as reported)

def beam_search(
    input_seq: List[int],
    next_token_fn: Callable[[List[int]], List[float]],
    max_token: int,
    beam_size: int,
    stop_word_id: int
) -> List[List[int]]:
    ...

Implement beam_search(input_seq, next_token_fn, max_token, beam_size, stop_word_id).

Our setup (not from the report) — ask your interviewer

The report states none of the rules below. They are standard beam search, and they are what the tests grade:

  • Start from input_seq. next_token_fn, called on a sequence, gives a score for every possible next token, indexed by token id.
  • At each step, extend every unfinished beam by every token, pool the expansions from all the beams, and keep the beam_size best by cumulative score.
  • A beam that emits stop_word_id is finished and is never extended again. It keeps competing on its score.
  • Stop when every beam has finished, or when max_token is reached.
  • Return the best sequences you kept: at most beam_size of them. beam_size is at least 1.

Conventions the signature leaves open:

  • next_token_fn returns probabilities. The signature says only List[float], which could also be log-probabilities. A sequence's score is the product of its probabilities (or the sum of their logs, the same order).
  • Left open, and every reading is accepted: whether the returned sequences include input_seq, whether they end with the stop token, their order, whether next_token_fn is called with the whole sequence or only the generated tokens, whether max_token counts generated tokens or the whole length, whether a finished beam keeps its slot or moves to a separate list, and whether scores are length-normalised. The tests strip the input prefix and a trailing stop token and ignore order; their inputs make the other readings give the same answer, with no ties, except where the max_token reading changes the result, and there each reading's answer is accepted.
  • If max_token ends the search before every beam has stopped, return the beams you have, or only the ones that stopped. Both are accepted.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 3
Trade-off discussion

Our practice question: why sum log-probabilities instead of multiplying probabilities on long sequences?

Our practice question: how would you add length normalisation, and how does it change which finished beams win?

Our practice question: what does one step cost, and how would you avoid sorting all beam_size x vocabulary candidates?

What Reports Emphasize

What the report says: a phone screen with a Chinese interviewer. The signature was given, the task was to complete the code, and the unit tests had to pass. Nothing else was posted: no tests, examples, follow-ups or outcome, so this page rests on one report.

Our advice (not from the report): before coding, say which conventions you assume: probabilities or log-probabilities, what max_token counts, and whether the output keeps the input prefix and the stop token. Pool every beam's expansions and keep the global best beam_size; choosing per beam is greedy decoding. Keep a beam that emits the stop token as a result and never extend it, and make sure max_token always ends the loop.

Practice

Write your own against 5 test cases, or read the worked solution — approach, complexity, and code that runs.
Perplexity · Phone Screen · Reported 1× across candidate reports
Is this helpful?