Skip to content
Tech Interview Prep
Technical interview guide

LLM Fundamentals

The core mechanics of how LLMs work and the practical parameters engineers actually tune: attention, tokenization, sampling, structured output, and cost/latency trade-offs.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v5
Editorial status
Reviewed

Scope: Transformer, BPE, SentencePiece, GPT-3, scaling laws, Chinchilla, nucleus sampling, RoPE, FlashAttention, GQA, PagedAttention, speculative decoding, GPTQ, Model Cards, and NIST GenAI references reviewed 2026-09-06.

Overview

Curated: · Written: · Reviewed:

LLM Fundamentals

In one line

A decoder-only LLM maps a token sequence to a next-token distribution, and generation is a sampling loop around it; reach for this model when you need to explain what sets cost, latency, context limits and what determinism you can actually promise. Everything outside the loop (validation, authorization, retrieval, tools) carries the guarantees the model cannot.

Key definitions

  • Token: an integer ID from a fixed subword vocabulary; cost, latency and context limits are denominated in tokens.
  • Logits and softmax: raw scores over the vocabulary, converted to a probability distribution for the next token.
  • Causal mask: position i attends only to positions at or before i, so one forward pass trains every next-token prediction at once.
  • KV cache: stored keys and values for prior positions, so each decode step computes only its own q/k/v.
  • Nucleus (top-p) sampling: keep the smallest set of tokens whose cumulative probability reaches p, then renormalise and sample.
  • Constrained decoding: restricts sampled tokens so output stays valid against a schema; guarantees syntax, not semantics.

Formulas and complexity

KV cache per sequence:

KV bytes=2×nlayers×nkv_heads×dhead×nseq×bytes per element\text{KV bytes} = 2 \times n_{layers} \times n_{kv\_heads} \times d_{head} \times n_{seq} \times \text{bytes per element}

GPT-3-class geometry, full multi-head cache, 4k context, fp16:

2×96×96×128×4096×2≈19.3 GB2 \times 96 \times 96 \times 128 \times 4096 \times 2 \approx 19.3\ \text{GB}

Attention score matrix is n×n, so doubling context from 4k to 8k quadruples score computation. Temperature divides logits: pi=ezi/T∑jezj/Tp_i = \frac{e^{z_i/T}}{\sum_j e^{z_j/T}}

How it works

no, append token and reuse KV cache yes Text Tokenizer BPE or SentencePiece Prefill: all input tokens in parallel, builds KV cache Logits over vocabulary Temperature, top-k or top-p, sample one token Stop condition? Detokenize, validate schema, check authority, bill tokens

Prefill sets TTFT and grows with input length. Each decode step attends over the cache, which sets inter-token latency. Seeds fix only the sampling stream; batching, floating-point order and model revision still vary output.

When to use and when not to

situationuse an LLM stepprefer deterministic code or a tool
open-ended synthesis or summarising textyes, with schema-checked outputno
arithmetic, totals, pricesno, unless verifiedyes
permissions, refunds, side effectsproposes onlyauthorises and executes
facts that change weeklyonly with retrieval and citationslookup against source of truth
stable format or toneprompt first, tune only if prompts failn/a

Traps interviewers probe

  • Trusting temperature for accuracy. Lowering temperature narrows the sampled set; it does not make the top token correct. Fix: pin decoding for extraction, then validate against ground truth.
  • Treating a seed as determinism. Batching and floating-point order still vary output. Fix: pin the revision and decoding, replay fixed inputs, and make the downstream contract deterministic.
  • Validating syntax and calling it correct. JSON mode forces parseable output, not the right total. Fix: server-side range, enum and cross-field checks against the authoritative record.
  • Estimating cost from English word counts. Code, CJK and emoji cost 1.5 to 4 times more tokens per unit of meaning. Fix: count with the model's exact tokenizer and chat template.
  • Putting authorization in the prompt. Retrieved text can carry instructions. Fix: bind identity and limits outside the model and check every tool argument against them.

Interview tips

Open by naming the loop: tokenise, prefill, sample one token at a time, validate outside the model. Then say which component owns each guarantee.

Expect these follow-ups: why long context is expensive (quadratic scores in prefill, linear KV memory per sequence, the 19.3 GB arithmetic); how you make output deterministic (you don't; you make the contract deterministic); fine-tune, prompt or retrieve (facts outside weights, behaviour in prompts, stable format in tuning); and whether the product hallucinates (yes, by mechanism; here is grounding, abstention and review).

30-second outline: "Text becomes BPE tokens, which set cost and context. Prefill builds the KV cache in one parallel pass, which sets TTFT. Each step samples one token from temperature-scaled logits, using top-p for variety or greedy for extraction. The loop stops, and the output is schema-validated, checked against authoritative state, then billed by tokens. The model proposes; deterministic code decides."

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.