Generative AI and Large Language Models Questions
The capabilities and behavior of modern generative and large language models. Covers how LLMs are pretrained, in-context learning and few-shot prompting, generative model families (autoregressive, diffusion), context windows, and tokenization and sampling. Emphasizes understanding what generative models can and cannot do and how they differ from discriminative ML.
In RLHF, what is the purpose of applying a KL penalty relative to a reference policy? Explain how it guards against extreme policy shifts, preserves pre-trained behavior, and how you might tune the KL coefficient in practice.
Sample Answer
Direct answer: The KL (Kullback-Leibler divergence) penalty in RLHF measures how far the current policy has drifted from a fixed reference policy (usually the supervised-fine-tuned starting point) and subtracts a term proportional to that divergence from the training objective, which keeps the policy from making large, uncontrolled changes purely to chase reward.
Structured elaboration: Without a KL penalty, a policy-optimization algorithm like PPO can, in principle, keep increasing reward by drifting arbitrarily far from reasonable, fluent language, especially if the reward model has any exploitable blind spot, since nothing in the objective directly penalizes that drift. The KL penalty term, typically reward−βDKL(πθ∥πref), directly trades off reward against how different the new policy's output distribution is from the reference model's, so it preserves general language quality and previously-learned behavior even while the policy adapts to the reward signal. In practice the coefficient β is tuned adaptively in many implementations: if the measured KL divergence for a batch exceeds a target value, β is increased for the next update (pulling the policy back toward the reference), and if KL is comfortably below target, β is decreased slightly to allow more room for improvement.
Worked example: If a batch's rollouts show a KL divergence of 0.02 against a target of 0.01, a common adaptive scheme (roughly following the original PPO-KL adaptive-penalty approach) would increase β, for example multiplying it by 1.5, for the next update; if instead the observed KL were 0.002, well under target, β might be reduced, for example halved, to let the policy move more freely while it remains far from the drift limit.
Trade-offs and pitfalls: Setting β too high effectively freezes the policy close to its starting point, wasting the RLHF stage's ability to improve on the reward signal at all; setting it too low reintroduces the risk of unconstrained drift and reward hacking that the penalty exists to prevent. Because the reference policy is fixed throughout training, if the supervised-fine-tuned starting point itself has quality issues, the KL penalty will actively work against fixing them, since it specifically discourages moving away from that reference.
Explain the differences between zero-shot, one-shot, few-shot, and in-context learning in LLMs. Describe scenarios where each is preferred, and when you would reach for fine-tuning instead of relying on in-context capabilities.
Sample Answer
Direct answer
Zero-shot means asking the model to perform a task with only an instruction and no examples; one-shot gives exactly one example; few-shot gives a handful (typically 2 to a few dozen) of input-output examples in the prompt. In-context learning (ICL) is the umbrella capability that makes all three work: the model adapts its behavior to a new task purely from what is in the current prompt, with no weight updates at all. You would reach for fine-tuning instead of relying on in-context capability when the task needs to be applied at scale with tight latency/cost budgets, needs behavior more reliable than prompt-level steering can guarantee, or when the pattern is too complex or too far from the model's pre-training distribution for a handful of examples to convey.
Structured elaboration
How ICL actually works. The model was never explicitly trained to "learn from examples in a prompt" as a separate mechanism; the behavior emerges from next-token prediction at scale, where predicting the continuation of "input: X -> output: Y" patterns during pre-training exposed the model to enough structurally similar sequences that it generalizes to a novel task specified the same way at inference time, without any parameter update.
When each is preferred.
- Zero-shot is preferred when the task is common enough (or well enough described by an instruction) that the model likely saw very similar instructions during training or instruction tuning, e.g., "summarize this," "translate this to French."
- One-shot is useful mainly to pin down an output FORMAT (e.g., "here is the exact JSON shape I want") rather than to teach a genuinely new task.
- Few-shot is preferred when the task is more specific or the output format/style is unusual enough that one example is ambiguous but a handful of varied examples disambiguates it, e.g., classifying support tickets into a company-specific taxonomy.
When fine-tuning wins instead. Every example you put in a few-shot prompt costs tokens on every single request, forever, which adds real latency and dollar cost at scale; fine-tuning pays that cost once, up front, and then every inference call is short and fast. Fine-tuning is also the right call when you need the model's behavior to be reliably consistent (few-shot performance is famously sensitive to which examples you pick and what order you put them in) or when the task requires knowledge or a pattern too subtle to convey in a handful of in-prompt demonstrations.
Worked example
Suppose you're building a support-ticket triage classifier into 12 company-specific categories. Zero-shot with just category names in the instruction will likely confuse categories with overlapping language. Few-shot with 2 examples per category (24 examples) fixes most of the ambiguity, but now every classification call carries those 24 examples in the prompt, at, say, 60 tokens each, roughly 1,400 extra tokens per request purely for the examples. At 100,000 requests per day, that is 140 million extra prompt tokens per day, every day, indefinitely. Fine-tuning on a few thousand labeled tickets removes that per-request tax entirely: the fine-tuned model has "absorbed" the category boundaries into its weights, so inference goes back to a short prompt with the ticket text alone.
Trade-offs & pitfalls
The common mistake is treating fine-tuning and in-context learning as mutually exclusive; in practice teams often start with few-shot prompting to validate that the task is even learnable and to gather a labeled dataset from real usage, THEN fine-tune once volume justifies paying the one-time training cost to remove the recurring per-request example tax. Jumping straight to fine-tuning before validating the task with a cheap few-shot prototype risks spending real engineering and compute effort locking in a task definition that later turns out to be wrong or incomplete.
What are the practical limitations of few-shot in-context learning? For each limitation you identify, what would you do about it in production?
Sample Answer
Direct answer
Few-shot in-context learning has four well-documented practical limitations: it is bounded by the model's context length, it is sensitive to the order the examples are presented in, it can leak the wrong label pattern if examples are poorly chosen (label leakage / spurious correlation), and it can fail when the distribution of the demonstrations doesn't match the distribution of real test inputs. Each of these has a concrete production mitigation.
Structured elaboration
Context-length constraints. Every example consumes tokens, so there's a hard ceiling on how many demonstrations you can include, and long examples crowd out either more examples or the actual query and instructions. Mitigation: keep demonstrations short and information-dense, or retrieve only the most relevant few examples per query dynamically instead of stuffing a fixed large set into every prompt.
Sensitivity to example order. The same set of examples in a different order can meaningfully change model accuracy, sometimes by double-digit percentage points, because the model is still, fundamentally, predicting the next token conditioned on everything that came before, so recency and position matter more than they intuitively should. Mitigation: test multiple orderings during prompt development and pick (or ensemble across) the ones that are stable, and avoid ordering examples in a way that correlates trivially with the label (e.g., all "positive" examples first).
Label leakage. If the demonstrations have a spurious surface pattern that correlates with the label (e.g., all positive-sentiment examples happen to be longer sentences), the model can learn that surface shortcut instead of the actual task, and perform poorly the moment a real input breaks the pattern. Mitigation: deliberately balance and randomize examples so no superficial feature (length, punctuation, which examples appear first) correlates with the label.
Distributional mismatch between demonstrations and test inputs. If your example set doesn't represent the actual variety of inputs you'll see in production (different phrasing, edge cases, adversarial inputs), the model's in-context "understanding" of the task will be narrower than what production actually demands. Mitigation: continuously sample real production inputs (including near-misses and failures) back into your example pool, rather than freezing a hand-picked set once at launch.
Trade-offs & pitfalls
These four limitations compound: a team that picks a fixed, small, hand-curated example set and never revisits it is simultaneously exposed to order sensitivity (never re-tested), label leakage (never audited for spurious patterns), and distributional mismatch (never refreshed against real traffic). The practical discipline that addresses all four at once is treating your few-shot example set as a live artifact, versioned, periodically re-evaluated against a held-out sample of real queries, and refreshed as production traffic reveals gaps, rather than a one-time prompt-engineering decision made at launch and left alone.
What is chain-of-thought (CoT) prompting? Give a short example that elicits step-by-step reasoning, and explain when CoT usually improves accuracy versus when it may degrade performance or increase latency/cost.
Sample Answer
Direct answer
Chain-of-thought (CoT) prompting asks the model to produce intermediate reasoning steps before its final answer, rather than jumping straight to a conclusion. It reliably improves accuracy on multi-step problems (arithmetic, logic, multi-hop question answering) because it gives the model more forward computation and more opportunity to catch its own errors along the way, but it comes at a real latency and cost tax, and can occasionally hurt performance or expose incorrect intermediate steps to the user.
Structured elaboration
Example. For "A store has 12 packs of 4 pencils and sells 7 packs. How many pencils remain?", a direct-answer prompt might just output "20". A CoT prompt (e.g., appending "Let's think step by step") elicits something like: "Total pencils = 12 x 4 = 48. Packs sold = 7 packs x 4 pencils = 28 pencils sold. Remaining = 48 - 28 = 20." Both give the right final answer here, but the CoT version is far more likely to catch itself on a harder or more error-prone multi-step problem.
Why it usually improves accuracy. A transformer produces its output token by token, with a fixed amount of computation per token. Skipping straight to a final answer forces the model to do all the reasoning implicitly, in one shot, with no chance to condition later steps on earlier intermediate results. Writing the intermediate steps out gives the model a place to "show its work" that it can then condition on, effectively increasing the amount of sequential computation available for the problem, which especially helps with tasks that genuinely require several dependent steps (arithmetic, multi-hop reasoning).
When it may degrade performance or increase cost. For genuinely simple, single-step tasks, CoT adds tokens (and therefore latency and dollar cost) without adding accuracy, since there's no multi-step reasoning to unpack. It can also occasionally hurt: a model can talk itself into a wrong path during a long reasoning chain and then confidently continue down it, producing a worse answer than a terse, direct one would have. And every CoT token is a token the user (or a downstream system) can read, so an incorrect intermediate step is now visible where a direct wrong answer alone would not have exposed the flawed reasoning.
Trade-offs & pitfalls
The practical discipline is to reserve CoT for tasks that plausibly need multiple dependent reasoning steps, and skip it for tasks that don't, rather than applying it universally as a "make it smarter" default. In production, exposing the full chain of thought to end users also has a UX and trust cost: it lets users see reasoning that might be wrong, verbose, or reveal how the system arrived at a sensitive conclusion, so many products deliberately generate the reasoning but only display the final answer, or summarize the reasoning rather than showing it verbatim.
What is the difference between prompting an LLM and fine-tuning it to change its behavior? In what scenarios is prompting a sufficient first approach, and when does it become insufficient, requiring fine-tuning instead?
Sample Answer
Direct answer
Prompting an LLM means steering its behavior at inference time through instructions and examples in the input, with no change to the model's weights; fine-tuning changes the weights themselves through additional training. Prompting is the right first approach whenever the base model's existing knowledge and capabilities are already sufficient and you just need to steer HOW it uses them; fine-tuning becomes necessary when the required behavior can't reliably be specified through instructions alone, either because it needs knowledge the model doesn't have, or because it needs a level of consistency prompting can't guarantee.
Structured elaboration
What prompting can and can't do. A prompt can specify style, format, persona, and task framing, and can supply new information directly in context (few-shot examples, retrieved documents). What it cannot do is permanently change what the model "knows" or reliably guarantee a specific behavior across every possible input, since prompt-following is itself a learned, imperfect capability; a sufficiently unusual or adversarial input can still cause the model to ignore or misapply prompt instructions.
When prompting suffices. Style and format control ("respond in JSON," "use a formal tone"), task framing for capabilities the base model already broadly has (summarization, translation, general Q&A), and situations where you can supply the needed facts directly in the prompt (via retrieval) rather than needing them baked into the weights.
When it becomes insufficient. Three concrete triggers: (1) the task requires domain knowledge that's absent or too sparse in the model's pre-training data and too voluminous to supply per-request via retrieval or examples; (2) the required consistency is higher than prompting reliably delivers, e.g., a classification task that must NEVER misfire on adversarial inputs; (3) the per-request cost of achieving the behavior via prompting (long instructions, many few-shot examples) becomes larger than the one-time cost of fine-tuning would be, once volume is high enough.
Trade-offs & pitfalls
The common mistake is reaching for fine-tuning as a default "make the model better at X" move before actually testing whether a well-designed prompt (possibly with retrieval or few-shot examples) already solves the problem; fine-tuning has real fixed costs (data collection, training compute, evaluation, and a slower iteration loop since every behavior change requires retraining) that prompting doesn't. The inverse mistake also happens: sticking with an increasingly baroque, ever-longer prompt to force consistent behavior on a task that has clearly outgrown what prompting can reliably deliver, when a modest fine-tuning investment would produce a shorter, cheaper, more reliable system. The practical discipline is to start with prompting, measure where and how often it fails, and let that concrete failure pattern justify the fine-tuning investment rather than assuming it up front.
Unlock Full Question Bank
Get access to all 37 Generative AI and Large Language Models interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.