← Microsoft Interview Insights
Start by clarifying the function signature and constraints, then implement greedy decoding as a simple loop, followed by beam search with a priority queue or sorted list to maintain top-k beams. Discuss edge cases like early-finished beams and how to handle them, and analyze time/space complexity.
Pro tip: Mention that beam search can be optimized by only expanding beams that haven't finished, and that finished beams should be set aside but still considered for final output if they have high scores. Also, note that using log-probabilities avoids underflow and that cumulative log-prob is a monotonic sum.
Ask about the function signature, whether it returns a single log-prob or a distribution, and the expected input/output format. Confirm max_len and k values.
Write a loop that at each step picks the token with the highest log-prob, appends it to the sequence, and stops if EOS or max_len is reached. Return the sequence.
Initialize beams with the start prefix. At each step, expand each beam by considering all possible next tokens (or top candidates), compute cumulative log-probs, and keep the top-k sequences. Stop when all beams have finished or max_len is reached.
Address early-finished beams by either removing them from active beams but keeping them in a separate list for final selection, or by allowing them to continue with a padding token. Also handle ties, empty prefixes, and k larger than vocabulary.
Discuss time complexity: greedy is O(max_len * vocab_size), beam search is O(max_len * k * vocab_size) if expanding all tokens. Mention memory and potential optimizations like pruning low-probability tokens.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.