← Microsoft Interview Insights

Microsoft·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Apr 2026

Summary

Microsoft ML Engineer interview with a two-part question covering LLM decoding theory and a live Python implementation. The coding portion was more involved than I expected for what seemed like a conceptual topic.

Questions Asked (2)

Q1

Compare greedy decoding, top-k sampling, top-p (nucleus) sampling, and beam search for LLMs. What are the trade-offs in terms of determinism, output diversity, fluency, and handling low-probability tokens? When would you choose one over another?

Technical Trade-offsSystem Design
Author's notes

I felt okay here but rambled a bit on beam search.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Structure your answer by first defining each decoding method and its core mechanism, then compare them across the four dimensions (determinism, diversity, fluency, low-probability token handling). Finally, discuss practical scenarios and trade-offs, emphasizing that the choice depends on the application's requirements for creativity vs. reliability.

Pro tip: Mention that in production systems, a hybrid approach like top-p with temperature is often used, and that beam search, while powerful, can be computationally expensive and may produce bland outputs due to its focus on high-probability sequences.

1. Define each method

Briefly explain greedy decoding (always pick highest probability token), top-k sampling (sample from top k tokens), top-p sampling (sample from smallest set of tokens whose cumulative probability exceeds p), and beam search (keep top b sequences at each step).

2. Compare on determinism and diversity

Greedy and beam search are deterministic (given same input), while top-k and top-p are stochastic. Greedy and beam search produce low diversity; top-k and top-p can produce high diversity, with top-p often more coherent due to dynamic token set.

3. Compare on fluency and low-probability token handling

Greedy and beam search tend to produce fluent but possibly repetitive or generic text; they avoid low-probability tokens. Top-k and top-p can introduce low-probability tokens, but top-p adapts better by excluding unlikely tokens when the distribution is peaked, thus maintaining fluency.

4. Discuss trade-offs and use cases

Greedy: fast, deterministic, but dull. Beam search: better for tasks with a clear correct answer (e.g., translation), but computationally heavy and can be less diverse. Top-k: simple, but fixed k may be suboptimal. Top-p: often best for open-ended generation, balancing diversity and coherence. Choose based on need for creativity vs. precision, and computational budget.

Key Points to Mention

  • Determinism: greedy and beam search are deterministic; top-k and top-p are stochastic.
  • Diversity: top-k and top-p yield more diverse outputs; greedy and beam search are less diverse.
  • Fluency: beam search often produces the most fluent outputs but may lack diversity; top-p can maintain fluency while allowing diversity.
  • Low-probability tokens: greedy and beam search ignore them; top-k includes them if within top k; top-p dynamically excludes them based on cumulative probability.
  • Computational cost: beam search is more expensive; greedy is cheapest; sampling methods are in between.
  • Use cases: greedy for deterministic tasks; beam search for tasks like translation; top-p for creative writing and dialogue; top-k as a simpler alternative to top-p.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Implement greedy decoding in Python. You're given a function that takes the last token and returns a probability distribution over next tokens as a list of (token, probability) pairs. Generate a sequence by always picking the highest-probability next token, stopping at an end-of-sentence marker or max length, and return both the generated sentence and the product of the chosen probabilities.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Easier than I thought conceptually, but I got tripped up tracking the running probability.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then outline a step-by-step algorithm before coding. Implement the greedy loop, track the product of probabilities, and handle termination conditions. Finally, discuss potential issues like numerical underflow and alternatives like beam search.

Pro tip: Mention that the product of probabilities can underflow, so in practice you'd sum log-probabilities; this shows awareness of numerical stability. Also, note that greedy decoding is fast but can lead to repetitive or suboptimal sequences, so it's often used as a baseline.

1. Clarify requirements and edge cases

Ask about the input format, special tokens (e.g., end-of-sentence marker), maximum length, and whether the probability distribution is normalized. Confirm return types and handling of empty distributions.

2. Outline the algorithm

Describe the loop: start with an initial token, get the distribution, select the token with the highest probability, append it to the sequence, multiply the probability into a running product, and check for termination (EOS or max length).

3. Implement the code

Write clean Python code with clear variable names. Use a loop, track the product, and handle the case where the distribution is empty (e.g., break or raise an error).

4. Test with examples

Walk through a simple example to verify correctness, including edge cases like immediate EOS or reaching max length. Check that the product is computed correctly.

5. Discuss trade-offs and improvements

Mention limitations of greedy decoding (e.g., lack of diversity, suboptimal sequences) and alternatives like beam search or sampling. Also discuss numerical stability (log-probabilities) and efficiency.

Key Points to Mention

  • Greedy decoding selects the highest-probability token at each step, which is fast but may not yield the most probable overall sequence.
  • The product of probabilities can underflow for long sequences; using log-probabilities (summing logs) is more numerically stable.
  • Termination conditions: stop when the end-of-sentence token is generated or when the maximum length is reached.
  • Edge cases: empty probability distribution, ties in probabilities (choose any, but be consistent), and handling of special tokens.
  • Time complexity is O(n * m) where n is sequence length and m is the number of tokens in the distribution, assuming finding the max is O(m).
  • Greedy decoding is deterministic and often used as a baseline; beam search or sampling can produce more diverse or higher-quality outputs.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.