← cresta Interview Insights

cresta·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Cresta ML Engineer interview that was basically one big coding/design question about sequence generation. They wanted both greedy and beam search implementations, recursive and iterative, plus complexity analysis. Pretty dense for a single session.

Questions Asked (2)

Q1

You're given a token-to-next-token probability dictionary and a start token. Implement greedy decoding (both recursive and iterative) to generate a sequence, stopping at a terminal token or dead end.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The recursive version felt natural to write but I second-guessed myself on the base cases.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases first, then outline the greedy decoding algorithm before writing code. Implement both recursive and iterative versions, explaining the trade-offs between them. Test with examples including dead ends and terminal tokens.

Pro tip: Mention that greedy decoding is deterministic and can get stuck in loops or produce suboptimal sequences, so in practice you might use beam search or sampling. Also, highlight the importance of handling cycles to avoid infinite loops.

1. Clarify requirements and edge cases

Ask about the dictionary format, terminal token, dead end (no next token), and whether cycles are possible. Confirm if the sequence should include the start token and terminal token.

2. Design the algorithm

Explain greedy decoding: at each step, pick the next token with the highest probability. For recursion, define a base case (terminal or dead end) and recursive case. For iteration, use a loop with a visited set to detect cycles.

3. Implement recursive version

Write a recursive function that takes the current token and accumulated sequence. If current token is terminal or has no next tokens, return the sequence. Otherwise, select the highest-probability next token and recurse.

4. Implement iterative version

Use a while loop: start with the start token, repeatedly look up the next token with max probability, append to sequence, and update current token. Stop when terminal token is reached or no next token exists. Use a visited set to prevent infinite loops.

5. Analyze trade-offs and test

Compare recursion (elegant but risk of stack overflow) vs iteration (more control, better for long sequences). Discuss time complexity O(n) where n is sequence length, and space complexity. Walk through a small example.

Key Points to Mention

  • Greedy decoding selects the highest-probability next token at each step, which is locally optimal but not globally optimal.
  • Recursive implementation may hit recursion depth limits for long sequences; iterative is safer for production.
  • Cycle detection is crucial to avoid infinite loops when the graph has cycles.
  • Terminal token and dead end (no outgoing edges) both stop generation, but may be handled differently.
  • Time complexity is O(n) for n steps, assuming O(1) lookup for max probability (or O(k) if scanning k next tokens).
  • In practice, greedy decoding can produce repetitive or dull text; beam search or sampling are common alternatives.

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

Q2

Using the same probability dictionary, implement beam search with beam size k using BFS-style expansion, tracking cumulative log-probabilities. Return the best completed sequence and optionally all completed beams. Analyze time and space complexity in terms of sequence length, branching factor, vocabulary size, and beam size.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

This is where I started sweating.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the probability dictionary structure and beam search requirements, then outline a BFS-style expansion that maintains the top k beams at each step. Implement the algorithm with cumulative log-probabilities, track completed sequences, and finally analyze time and space complexity in terms of sequence length, branching factor, vocabulary size, and beam size.

Pro tip: Emphasize the importance of using log-probabilities to avoid numerical underflow and discuss how beam search balances exploration and exploitation, which is crucial for real-world ML systems like Cresta's conversational AI.

1. Clarify the problem and data structures

Confirm the format of the probability dictionary (e.g., mapping from prefix to next-token probabilities) and define beam size k, max sequence length, and end-of-sequence token. Decide whether to return all completed beams or just the best.

2. Initialize beams and tracking structures

Start with a single beam containing the start token with log-probability 0. Maintain a list of active beams (each with sequence and cumulative log-prob) and a list of completed beams.

3. Iteratively expand beams using BFS

For each step up to max length, expand each active beam by considering all possible next tokens from the probability dictionary. Compute new cumulative log-probabilities, then select the top k beams overall to keep. Move beams that hit the end token to completed beams.

4. Return the best completed sequence

After expansion, if completed beams exist, return the one with the highest cumulative log-probability (or all completed beams if requested). If no completed beams, return the best active beam or handle as needed.

5. Analyze time and space complexity

Time: O(T * k * b * V) where T is max length, k is beam size, b is branching factor (or V if all tokens considered), and V is vocabulary size. Space: O(k * T) for storing beams, plus O(k * V) for expansion if not careful. Discuss optimizations like pruning low-probability tokens.

Key Points to Mention

  • Use of log-probabilities to prevent underflow and enable addition instead of multiplication.
  • Beam search as a heuristic search algorithm that balances breadth and depth, commonly used in sequence generation tasks like machine translation and speech recognition.
  • Handling of end-of-sequence tokens and the distinction between active and completed beams.
  • Time complexity: O(T * k * b * V) where T is sequence length, k is beam size, b is branching factor (often V), and V is vocabulary size; space complexity: O(k * T) for beams.
  • Trade-offs: larger beam size improves quality but increases computation; smaller beam size is faster but may miss better sequences.
  • Potential optimizations: using a priority queue for top-k selection, pruning tokens with very low probability, and early stopping when all beams are completed.

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