DenCT/test-storage / aiml-interview-prep.md
DenCT's picture
|
download
raw
57.1 kB
# AI/ML Interview Prep — Theory, Models, and Production Deep-Dive > Companion to the System Design roadmap. > Covers what interviewers actually ask in ML/DL/LLM/RAG/Agentic/VLM/Inferencing rounds. > Format per topic: **concept → likely questions → what to cover in your answer → gotchas / follow-ups**. --- ## How to use this guide - Each section has questions ordered roughly easy → hard. - For each question, the "Cover" bullets are the **bare minimum** to say. Add detail/examples on top. - Interviewers probe for: (1) correctness, (2) intuition, (3) trade-offs, (4) when it fails. Hit all four. - When you don't know: say "I'd reason from first principles" and do exactly that. Never bluff specifics. - Always tie answers to *production*. You have 3 yrs experience — your edge is "I've seen X break this way." --- # PART 1 — CLASSICAL ML THEORY ## 1.1 Fundamentals ### Q: Explain the bias-variance trade-off. **Cover**: - Total expected error = bias² + variance + irreducible noise (decompose for squared loss). - Bias = error from wrong assumptions (underfitting). Variance = sensitivity to training set (overfitting). - High capacity model = low bias, high variance. Regularization, more data, simpler model reduce variance. - Show curve: training error decreases monotonically; validation error U-shape. - **Modern twist**: in over-parameterized deep nets we see *double descent* — error drops, rises near interpolation threshold, then drops again. Mention if asked about DL. **Gotchas**: They may ask "how do you know if you're in bias or variance regime?" → Look at train vs val gap. Small gap + high error = bias. Large gap = variance. ### Q: Generative vs discriminative models — examples and trade-offs? **Cover**: - Discriminative models P(y|x) directly (logistic regression, SVM, most NNs). - Generative models P(x, y) or P(x|y) and use Bayes (Naive Bayes, GMM, HMM, LDA, diffusion, GANs, LLMs technically). - Discriminative usually beats generative on classification with enough data; generative can be sampled from and handles missing data. - Naive Bayes converges faster with little data (asymptotic argument from Ng & Jordan paper). ### Q: How do you handle imbalanced datasets? **Cover** (give a *menu*, then say which you'd pick): - Re-sampling: random oversampling, SMOTE / ADASYN, random undersampling, NearMiss, Tomek links. - Class weights in the loss (often best first move — cheap and effective). - Threshold tuning (don't use 0.5). - Anomaly-detection framing if extremely rare (<0.1%). - Two-stage models: cheap recall stage + heavy precision stage. - Right metric: PR-AUC > ROC-AUC for rare-positive problems; F-beta if recall vs precision asymmetry matters; MCC if both classes matter. - Stratified k-fold CV. **Gotcha**: SMOTE in high-dim sparse data (NLP) is usually bad — interpolation isn't meaningful. ### Q: Why is feature scaling important? When can you skip it? **Cover**: - Required: distance-based (KNN, K-means, SVM with RBF), gradient-based (NNs, log reg), regularized models (Ridge/Lasso punish large coefficients unfairly). - Not required: tree-based (RF, XGBoost) — splits are scale-invariant. - Methods: standardization (z-score), min-max, robust (median/IQR), log/power transforms (Box-Cox, Yeo-Johnson), quantile transform. - Fit scaler on train only; apply to val/test. Else: data leakage. ### Q: Curse of dimensionality? **Cover**: - Volume grows exponentially → data becomes sparse → distances concentrate (all points roughly equidistant) → KNN/clustering degrade. - Need exponentially more data to maintain density. - Mitigation: dimensionality reduction (PCA, autoencoders), feature selection, manifold assumption (real data lives on lower-dim manifold — why DL works). ## 1.2 Linear models ### Q: Derive logistic regression. **Cover**: - Linear combo: z = w·x + b. - Sigmoid: σ(z) = 1/(1+e^(-z)). - Bernoulli likelihood → log-loss / binary cross-entropy. - No closed form → gradient descent. Gradient: (σ(z) − y) · x. (Be ready to derive on whiteboard.) - L1/L2 regularization → MAP with Laplace/Gaussian prior on weights. - Multi-class: softmax + categorical CE. **Follow-ups**: Why log-loss not MSE? → Non-convex with sigmoid+MSE, slow gradients in saturation, log-loss is convex. ### Q: L1 vs L2 regularization? **Cover**: - L1 (Lasso): |w| — induces sparsity (corners of diamond constraint touch axes), feature selection. - L2 (Ridge): w² — shrinks all weights smoothly, handles multicollinearity, has closed form. - Elastic Net combines both. - Bayesian view: L2 = Gaussian prior; L1 = Laplace prior. - L1 not differentiable at 0 → subgradient or proximal methods (coordinate descent in glmnet). ### Q: How does multicollinearity affect linear regression? **Cover**: - Coefficient estimates unstable / high variance, p-values inflated, signs flip. - Detect: VIF > 5–10, correlation matrix, condition number. - Fix: drop a feature, PCA, Ridge regression, partial least squares. ## 1.3 Tree-based models ### Q: How does a decision tree decide a split? **Cover**: - Classification: minimize Gini impurity or entropy / maximize information gain. - Regression: minimize variance / MSE. - Greedy; one feature, one threshold at a time. - Stopping: max depth, min samples leaf, min impurity decrease. - Prone to overfitting — pruning (cost-complexity α). ### Q: Random Forest vs Gradient Boosting? **Cover**: - **RF**: bagging (bootstrap + feature subsampling per split). Trees independent, parallel. Reduces variance. - **GBM**: boosting — trees sequential, each fits residuals (or gradient of loss). Reduces bias. Stronger but easier to overfit. - XGBoost/LightGBM/CatBoost are GBM with engineering improvements. - RF for quick baseline; GBM for max accuracy on tabular. ### Q: Differences XGBoost vs LightGBM vs CatBoost? **Cover**: - **XGBoost**: level-wise growth, exact + approximate split finding, regularization in objective, robust default. - **LightGBM**: leaf-wise (best-first) growth → faster, deeper unbalanced trees, can overfit small data. Uses histogram binning, GOSS (gradient-based one-side sampling), EFB (exclusive feature bundling). - **CatBoost**: ordered boosting (avoids target leakage from categorical encoding), native categorical handling (target stats per ordering), symmetric trees (fast inference). - All three: GBDT, regularization, missing-value handling. **Production tip**: LightGBM usually fastest training; CatBoost best on heavy-categorical; XGBoost most ubiquitous tooling. ### Q: Why do tree ensembles dominate tabular? **Cover**: - Handle mixed types, missing values, non-linear interactions, no scaling. - Robust to irrelevant features. - Strong inductive bias matches tabular structure (axis-aligned splits, hierarchical interactions). - Recent NN tabular attempts (TabNet, FT-Transformer, SAINT) only match GBDT on average, win in some niches. ## 1.4 Other classical models ### Q: How does SVM work? Kernel trick? **Cover**: - Maximize margin between classes. Hard-margin (linearly separable) → soft-margin with slack ξ and C parameter. - Dual formulation depends only on inner products → kernel trick: replace ⟨x, x'⟩ with K(x, x') = ⟨φ(x), φ(x')⟩ implicitly mapping to high-dim space. - Common kernels: linear, polynomial, RBF (Gaussian), sigmoid. - Doesn't scale beyond ~10⁵ samples (O(n²) or worse). ### Q: KNN — strengths, weaknesses, choice of k? **Cover**: - Lazy learning, no training; prediction is O(n) per query (mitigate with KD-tree, Ball tree, ANN like HNSW for large n). - k small → high variance; k large → high bias. Use CV. - Distance metric matters (Euclidean, Manhattan, cosine, Mahalanobis). - Needs scaling; suffers curse of dimensionality. ### Q: K-means details. When does it fail? **Cover**: - EM-style: assign points to nearest centroid → update centroids → repeat. - Minimizes within-cluster sum of squares; converges to local optimum. - Sensitive to init (use K-means++); to outliers; assumes spherical, equal-size clusters. - Choose k: elbow method, silhouette, gap statistic. - Alternatives: GMM (soft, ellipsoidal), DBSCAN (density-based, finds arbitrary shapes, handles noise; key params ε and minPts), HDBSCAN (varying density), agglomerative (hierarchical, dendrogram). ### Q: PCA vs t-SNE vs UMAP? **Cover**: - **PCA**: linear, preserves global variance, fast, deterministic, axes interpretable. - **t-SNE**: non-linear, preserves local neighborhoods, great for visualization, no out-of-sample mapping, sensitive to perplexity, slow on large data. - **UMAP**: non-linear, faster than t-SNE, preserves more global structure, supports new-point transform. - For downstream ML use PCA / autoencoder; for viz only use t-SNE or UMAP. ## 1.5 Metrics ### Q: Walk me through classification metrics. **Cover**: - Confusion matrix → derive: precision = TP/(TP+FP), recall/sensitivity = TP/(TP+FN), specificity = TN/(TN+FP), F1 = harmonic mean. - Accuracy is misleading on imbalance. - ROC-AUC: TPR vs FPR across thresholds; class-balance-invariant; can over-optimistically rank when negatives dominate. - PR-AUC: more honest on rare-positive problems. - Log-loss / Brier: scoring rule for probabilistic predictions; sensitive to calibration. - Cohen's kappa, MCC: chance-adjusted. ### Q: How do you evaluate a regression model? **Cover**: - MSE, RMSE (same units), MAE (robust to outliers), MAPE/SMAPE (scale-free, blows up near 0), Huber loss (hybrid). - R² (coefficient of determination) — proportion of variance explained; can be negative on test. - Adjusted R² penalizes feature count. - Always plot residuals vs predictions — heteroskedasticity, non-linearity, bias regions. ### Q: When ROC-AUC misleads? **Cover**: Heavy class imbalance (lots of TN inflate FPR denominator → AUC stays high while precision is terrible). Use PR-AUC instead. Also AUC doesn't reflect calibration or threshold. ### Q: What's calibration? **Cover**: - Predicted P(y=1) matches empirical frequency. Diagnose: reliability diagram, expected calibration error (ECE). - Many models miscalibrated: SVMs (no probability), boosted trees (over-confident), modern NNs (very over-confident). - Fixes: Platt scaling (logistic), isotonic regression (non-parametric, needs more data), temperature scaling (single param, popular for NNs). ## 1.6 Cross-validation & validation strategy ### Q: How do you set up validation for time-series? IID? Grouped data? **Cover**: - **IID**: random k-fold, stratified k-fold (preserves class ratio). - **Time-series**: rolling/expanding window forward-chained; never include future in training. - **Grouped** (multiple rows per user/patient): GroupKFold so same group not in both train and val — else leakage and overestimated performance. - Nested CV when tuning hyperparameters and reporting performance simultaneously. ### Q: What is data leakage and how have you found it? **Cover** (lead with a *story*): - Target leakage: using a feature derived (directly or indirectly) from the label. - Train-test contamination: same entity in both; preprocessing on full data. - Temporal leakage: using future info. - Detection: too-good-to-be-true scores, feature importances dominated by one feature, drop-out test, holdout in time. - Prevention: fit transformers on train only inside CV pipelines, careful feature engineering review. --- # PART 2 — DEEP LEARNING THEORY ## 2.1 Foundations ### Q: Walk through backpropagation. **Cover**: - Forward pass: compute activations layer by layer. - Loss at output. - Backward: chain rule of partial derivatives — compute ∂L/∂w for each weight. - Reuse intermediate gradients (dynamic programming). - For a layer y = Wx + b followed by activation σ: dL/dW = (dL/dy ⊙ σ'(z)) · xᵀ. - Computational graph; reverse-mode autodiff. **Be ready to**: derive backprop for a 2-layer MLP on paper. ### Q: Vanishing & exploding gradients? **Cover**: - Causes: deep nets, saturating activations (sigmoid/tanh), poor init, RNN unrolling. - Effects: early layers don't learn (vanish) or training diverges (explode). - Mitigations: ReLU/GELU activations; He/Xavier init; residual connections; normalization (BN/LN); gradient clipping; gating (LSTM/GRU). - For transformers, residuals + LayerNorm + careful init handle it. ### Q: Activations — sigmoid, tanh, ReLU, Leaky ReLU, GELU, SwiGLU? **Cover**: - **Sigmoid**: 0–1; vanishing gradient, non-zero centered. - **Tanh**: −1 to 1; zero-centered but still saturates. - **ReLU**: max(0, x); cheap, sparse activations; dying ReLU problem. - **Leaky / Parametric ReLU**: small slope for negatives. - **GELU**: x · Φ(x); smooth, used in transformers (BERT, GPT-2). - **SwiGLU**: gated variant in modern LLMs (LLaMA, PaLM) — empirically better, slightly more params. - **Softmax**: output normalization, multi-class. ### Q: BatchNorm vs LayerNorm vs RMSNorm vs GroupNorm? **Cover**: - **BatchNorm**: normalize across batch per feature; trains faster, regularizes; requires large batches; awkward at inference (running stats) and in sequence/RL/small-batch settings. - **LayerNorm**: normalize across features per sample; batch-size-independent; default in transformers. - **RMSNorm**: drops mean subtraction, just RMS — used in LLaMA, faster, similar quality. - **GroupNorm**: groups of channels; works for vision with small batches. - **InstanceNorm**: per-sample per-channel; style transfer. - **Pre-norm vs Post-norm**: pre-norm transformers (LN before sub-layer) train more stably at depth; original transformer was post-norm. ### Q: Optimizers — SGD, momentum, Adam, AdamW, Lion? **Cover**: - **SGD**: noisy, slow but generalizes well; needs careful LR. - **Momentum**: accelerates in consistent directions, dampens oscillations. - **Nesterov**: lookahead variant. - **Adagrad/RMSprop**: per-parameter adaptive LR. - **Adam**: momentum + adaptive LR via 1st & 2nd moment estimates. Fast convergence; sometimes worse generalization than SGD. - **AdamW**: decouples weight decay from gradient update — current default for transformers. - **Lion**: sign-only, less memory, competitive. - **Shampoo / Sophia**: second-order-ish, used in some LLM trainings. ### Q: Learning rate schedules? **Cover**: - Step decay, exponential, polynomial. - Cosine annealing (with warm restarts). - Warmup (linear) + cosine decay — standard for transformers. - One-cycle (Smith). - Adaptive: ReduceLROnPlateau. - Why warmup? Early gradients in Adam have noisy second-moment estimates; large LR explodes. ### Q: Dropout — how does it work, why does it help? **Cover**: - Randomly zero activations with prob p at train time; scale by 1/(1-p) (inverted dropout) so expected output unchanged. - Acts as ensemble of subnetworks; prevents co-adaptation. - At inference: no dropout. - Doesn't combine well with BatchNorm (variance shift). Use one. - Variants: spatial dropout (CNNs), DropPath / stochastic depth (ResNet, ViT), attention dropout. ## 2.2 Loss functions ### Q: When to use cross-entropy vs MSE vs others? **Cover**: - **CE**: classification; matches log-likelihood of categorical; sharp gradients when wrong. - **MSE**: regression; assumes Gaussian noise; sensitive to outliers. - **MAE / Huber**: robust regression. - **Focal loss**: down-weights easy examples; for class imbalance / dense detection (RetinaNet). - **Triplet / Contrastive / InfoNCE**: representation learning; pulls positive pairs together, pushes negatives apart. - **Dice / IoU**: segmentation, imbalanced foreground/background. - **CTC**: sequence with unknown alignment (ASR). ### Q: What is label smoothing? **Cover**: Replace one-hot target [0,…,1,…,0] with [ε/(K-1),…,1-ε,…,ε/(K-1)]. Prevents over-confidence, improves calibration, marginal accuracy boost. Standard in transformer training. ## 2.3 CNNs ### Q: Walk through a convolution. **Cover**: - Kernel slides over input, computes dot product → feature map. - Hyperparams: kernel size, stride, padding, dilation, channels. - Output size: ⌊(W + 2p − k)/s⌋ + 1. - Parameter sharing — translation equivariant. - Pooling: max/avg; downsamples, broader receptive field. - Receptive field grows with depth & dilation. ### Q: ResNet — why do residual connections work? **Cover**: - Skip connection: y = F(x) + x. - Network learns residual F instead of full mapping → easier optimization. - Gradient highway: ∂L/∂x has direct path → mitigates vanishing. - Enables very deep nets (>100 layers). - Variants: pre-activation ResNet, ResNeXt, DenseNet (concat instead of add), Wide ResNet. ### Q: Modern vision architectures? **Cover**: - **VGG**: stack of 3×3 conv; simple, large. - **Inception/GoogLeNet**: multi-scale (1×1, 3×3, 5×5 in parallel). - **ResNet**: residuals. - **MobileNet**: depthwise separable conv → efficient. - **EfficientNet**: compound scaling (depth, width, resolution). - **ConvNeXt**: modernized CNN matching ViT. - **ViT**: pure transformer on image patches. - **Swin**: hierarchical ViT with shifted windows. - **DETR**: transformer for detection. - **SAM**: segment anything (promptable seg). ## 2.4 Sequence models pre-transformer ### Q: LSTM internals? **Cover**: - Gates: forget, input, output (sigmoid); candidate cell (tanh). - Cell state carries long-term info; selectively updated. - Solves vanishing gradient via additive cell update path. - GRU: simpler (reset, update gates), fewer params, similar performance. - Bi-directional variants for non-causal sequence labeling. ### Q: Why did transformers replace RNNs? **Cover**: - RNN inherently sequential → can't parallelize across time. Transformer parallelizes across positions. - Long-range dependencies easier (constant path length via attention vs O(n) for RNN). - Better scaling on compute. - Cost: O(n²) attention. Mitigated by FlashAttention and architectural variants. ## 2.5 Generative DL (non-LLM) ### Q: Autoencoder vs VAE vs GAN vs Diffusion? **Cover**: - **AE**: encode → bottleneck → decode; reconstruction loss; no generative density. - **VAE**: encoder outputs μ, σ of latent; reparam trick z = μ + σ⊙ε; loss = reconstruction + KL(q(z|x) || prior). Probabilistic, samples blurry. - **GAN**: generator vs discriminator minimax; sharp samples; unstable, mode collapse. Variants: DCGAN, WGAN(-GP), StyleGAN. - **Diffusion**: forward process adds noise; learn reverse (denoising score matching). Stable, SOTA for images (DDPM, Stable Diffusion, Imagen). Latent diffusion = diffusion in VAE latent (Stable Diffusion). Classifier-free guidance for conditional. DDIM/DPM-Solver for fewer steps. - **Flow models**: invertible nets, exact likelihood (RealNVP, Glow). - **Score-based**: continuous-time SDE view of diffusion. ### Q: Why does diffusion beat GAN now? **Cover**: Stable training (no minimax), better mode coverage, controllability via classifier-free guidance and conditioning, scales with compute. GAN advantage was speed (single forward pass) — closing via distillation (LCM, SDXL Turbo). ## 2.6 Self-supervised learning ### Q: Pretext tasks? **Cover**: - Vision: rotation prediction, jigsaw, colorization, MAE (masked autoencoder), BYOL/SimCLR/MoCo (contrastive), DINO (self-distillation). - NLP: MLM (BERT), next-token (GPT), span corruption (T5), next-sentence (orig BERT, found weak). - Audio: wav2vec, HuBERT. - Multimodal: CLIP (contrastive image-text). - Goal: learn transferable representations from cheap unlabeled data. ### Q: Contrastive learning — InfoNCE loss? **Cover**: - Pull positive pair embeddings close, push negatives apart. - InfoNCE = −log( exp(sim(q,k+)/τ) / Σ exp(sim(q,kᵢ)/τ) ) over negatives. - Temperature τ controls hardness. - Negative quality matters: SimCLR uses in-batch; MoCo uses momentum queue; supervised contrastive uses class labels. ## 2.7 Transfer learning & fine-tuning ### Q: When fine-tune vs feature extract vs train from scratch? **Cover**: - Small data + similar domain → freeze most layers, train head. - Medium data → unfreeze top layers, fine-tune with smaller LR. - Large data + different domain → full fine-tune, maybe initialize from pretrained. - LR for fine-tuning: 1e-4 to 1e-5; use discriminative LR (lower for earlier layers). - Catastrophic forgetting: keep some original task data, EWC, LoRA (covered in LLM section). --- # PART 3 — TRANSFORMERS IN DETAIL This is where senior interviews live. Memorize the math. ## 3.1 Attention ### Q: Derive scaled dot-product attention. **Cover** (draw it): - Inputs: Q (n × d_k), K (n × d_k), V (n × d_v). - Scores: S = QKᵀ (n × n). - Scale by √d_k (else softmax saturates as d_k grows; variance argument). - Softmax row-wise → attention weights A. - Output: A V (n × d_v). - Complexity: O(n² · d) time, O(n²) memory (bottleneck for long context). **Be ready to**: write this in NumPy/PyTorch in <2 min. ### Q: Multi-head attention — why? **Cover**: - Split d_model into h heads of d_model/h. - Each head learns different attention pattern (syntax, coreference, position). - Concat → linear projection. - Parameter count similar to single head with d_model; better representation. ### Q: Self-attention vs cross-attention vs causal attention? **Cover**: - **Self**: Q, K, V from same sequence. - **Cross**: Q from decoder, K & V from encoder (translation, image captioning). - **Causal/masked**: zero out future positions (mask with -∞ before softmax) — for autoregressive decoding. ### Q: Efficient attention variants? **Cover**: - **Multi-Query Attention (MQA)**: one K, V shared across heads (Q per head). Saves KV-cache memory, mild quality drop. - **Grouped-Query Attention (GQA)**: groups of heads share KV; sweet spot, used in LLaMA-2/3. - **Sliding window attention**: each token attends to last W tokens; linear time (Mistral, Longformer). - **Sparse attention**: BigBird (random + window + global). - **Linear attention**: kernel feature maps to avoid n² (Performer, Linear Transformer); usually quality lag. - **FlashAttention**: not different math, just IO-aware kernel — tiles over SRAM, avoids materializing n×n matrix. Massive speedup. V2/V3 add more tricks. - **PagedAttention**: KV-cache memory management (vLLM). - **Mamba / SSMs**: alternative architecture, linear time, selective state; competitive recently. ## 3.2 Positional encoding ### Q: Walk through positional encoding variants. **Cover**: - Transformers are permutation-equivariant → need to inject position. - **Sinusoidal (original)**: fixed sin/cos at multiple frequencies; extrapolates somewhat. - **Learned absolute**: add a learnable vector per position; doesn't extrapolate. - **Relative**: encode i-j (T5 bias, Shaw, Transformer-XL). - **RoPE (Rotary)**: rotate Q and K vectors by an angle proportional to position; encodes relative position via dot-product; used in LLaMA, GPT-NeoX, most modern LLMs. Extends with NTK-aware scaling, YaRN for context extension. - **ALiBi**: add linear bias to attention scores by distance; extrapolates well. ### Q: How do you extend context length of an LLM? **Cover**: - Position interpolation (RoPE scaling — linear). - NTK-aware scaling (different scaling per frequency band). - YaRN (improved NTK). - LongRoPE. - Continued pretraining on long sequences. - Architectural: sliding window + global tokens (Mistral, Longformer), Mamba. - Trade-off: attention quadratic cost still applies for full attention; serving 1M context is expensive. ## 3.3 Architecture family ### Q: Encoder-only vs decoder-only vs encoder-decoder? **Cover**: - **Encoder-only (BERT family)**: bidirectional self-attention; MLM objective; good for classification, NER, embeddings. RoBERTa, DeBERTa. - **Decoder-only (GPT family)**: causal self-attention; CLM objective; generation, in-context learning. Dominant for LLMs (GPT, LLaMA, Mistral, Qwen, DeepSeek). - **Encoder-decoder (T5, BART)**: encoder bidirectional + decoder causal with cross-attention; good for translation, summarization, structured outputs. - Why decoder-only won: scaling laws favored simple decoder + scale; one objective; emergent in-context learning. ### Q: Pre-norm vs post-norm transformer? **Cover**: - Original: x → sublayer → add → LN. Post-norm. - Modern: LN → sublayer → add. Pre-norm. - Pre-norm trains stably at depth without elaborate warmup; slight quality cost. - LLaMA-style: RMSNorm + pre-norm. ### Q: Walk through what happens to a token from input to output in GPT. **Cover**: 1. Tokenize text → IDs. 2. Token embedding lookup + (RoPE applied later in attention). 3. N transformer blocks: RMSNorm → multi-head causal attention (with KV cache) → residual → RMSNorm → SwiGLU FFN → residual. 4. Final LayerNorm → lm_head (often tied with input embedding) → logits. 5. Softmax / sampling → next token. ## 3.4 Tokenization ### Q: BPE vs WordPiece vs SentencePiece vs Unigram? **Cover**: - All produce subword tokens; trade vocab size vs sequence length. - **BPE**: start with chars, iteratively merge most frequent pair. Used by GPT-2, RoBERTa. - **WordPiece**: similar but merges by likelihood gain. BERT. - **SentencePiece**: language-agnostic (handles raw text including whitespace as token); used by T5, LLaMA, mT5. - **Unigram LM**: probabilistic; remove tokens that minimize loss. ALBERT, XLNet. - Byte-level BPE (GPT-2): works on bytes — never out-of-vocab. tiktoken does this. ### Q: Implications of tokenization? **Cover**: - Cost & latency = token count, not char count. - Numbers: weird splits (1234 → "12" "34") → arithmetic struggles. - Non-English/code: more tokens per word → more expensive, lower effective context. - Glitch tokens (SolidGoldMagikarp): rare training-data tokens cause weird behavior. --- # PART 4 — LARGE LANGUAGE MODELS ## 4.1 Pre-training ### Q: How are LLMs pre-trained at high level? **Cover**: - Objective: next-token prediction (cross-entropy on shifted sequence). - Data: trillions of tokens — web crawl (CommonCrawl, FineWeb), books, code (GitHub, StackOverflow), Wikipedia, papers (ArXiv, PubMed). Heavy dedup + quality filtering. - Training: thousands of GPUs/TPUs, weeks–months, 3D parallelism (data + tensor + pipeline + FSDP). - Stability: BF16, AdamW, gradient clipping, cosine schedule with warmup, careful init. - Curriculum: sometimes data-mix-aware (more code/math later). ### Q: Chinchilla scaling laws? **Cover**: - Hoffmann et al. — compute-optimal training: model size and data size should scale roughly equally with compute (≈20 tokens per parameter). - Before Chinchilla (GPT-3): models too big, trained too little. - After: smaller models, more data (LLaMA-1 7B trained on 1T tokens). - More recent: "Beyond Chinchilla optimal" — over-training small models is justified when inference cost dominates (LLaMA-3 trained 15T tokens on 8B). - Emergent abilities: some capabilities only appear past scale thresholds (controversial — may be metric artifacts). ## 4.2 Post-training ### Q: What is SFT (supervised fine-tuning)? **Cover**: - Curated (instruction, response) pairs. - Same next-token loss but on instruction-formatted data. - Often loss-masked to only score response tokens. - Data quality > quantity at this stage (LIMA paper: 1k examples can suffice). ### Q: RLHF — full pipeline. **Cover**: 1. Pretrain base LLM. 2. SFT on instructions. 3. Train reward model: pairs of responses ranked by humans → preference loss (Bradley-Terry). 4. RL fine-tune policy against RM using PPO; KL penalty to SFT to prevent reward hacking. - Issues: reward hacking, distribution shift, expensive, sensitive. ### Q: DPO vs PPO? **Cover**: - **PPO**: online RL with separate reward model. - **DPO** (Direct Preference Optimization): closed-form derivation that lets you optimize policy directly on preference pairs — no separate reward model, no rollouts. Simpler, more stable, faster, often comparable. - Variants: IPO (fixes overfitting in DPO), KTO (works on unpaired data with binary labels), ORPO (combines SFT + preference, no ref model), SimPO (simpler, length-normalized). ### Q: What is RLAIF / Constitutional AI? **Cover**: - RLAIF: AI feedback replaces human (use stronger LLM as judge to label preferences). - Constitutional AI (Anthropic): self-critique using natural-language principles, then RL on AI-generated preferences. Reduces harmful outputs. ### Q: Reasoning RL (o1 / R1 style)? **Cover**: - Scale "test-time compute" — model produces long chain-of-thought before final answer. - DeepSeek-R1 paper introduced GRPO (Group Relative Policy Optimization): cheaper PPO variant for reasoning, no value model. - Reward = verifiable correctness (math, code unit tests). - Emergent reflection, self-correction. ## 4.3 Parameter-efficient fine-tuning (PEFT) ### Q: Explain LoRA. **Cover**: - Freeze base weights W ∈ ℝ^(d×k). Add low-rank update ΔW = BA, where B ∈ ℝ^(d×r), A ∈ ℝ^(r×k), r ≪ d. - Train only A, B (and optionally biases). Trainable params drop by ~100×. - At inference: merge ΔW into W (zero overhead) or keep separate (multi-LoRA serving). - Hyperparams: rank r (8–64), α (scaling, effective LR), target modules (q_proj, v_proj minimally; all linear ideal). - Where it lands: SFT new domains, style adaptation. Rarely matches full FT on hard novel capabilities. ### Q: QLoRA? **Cover**: - Base model quantized to 4-bit (NF4 — normal-float-4); LoRA adapters in BF16. - Double quantization (quantize the quantization constants). - Paged optimizers to handle memory spikes. - Enables fine-tuning 65B on a single 48GB GPU. Quality near full LoRA at higher precision. ### Q: Other PEFT methods? **Cover**: - **Prefix tuning / Prompt tuning**: prepend learnable vectors to keys/values or input embeddings. - **Adapters**: small bottleneck MLPs inside each block. - **IA³**: rescale activations with learned vectors. - **DoRA**: decompose weight into magnitude + direction; LoRA on direction. Slight gains over LoRA. - **BitFit**: tune only biases. ## 4.4 Prompting & in-context learning ### Q: Prompting techniques? **Cover**: - Zero-shot, few-shot (examples in context). - Chain-of-Thought (CoT): "let's think step by step." - Self-consistency: sample multiple CoTs, majority vote. - Tree of Thoughts: branch & explore. - ReAct: thought + action + observation interleaved. - Self-ask, least-to-most, plan-and-solve. - Role / persona prompting. - Output format: JSON mode, structured grammars. - Bonus: "show, don't tell" — examples > instructions. ### Q: Why does in-context learning work? **Cover** (honest answer): - Empirically: large models can pattern-match examples → apply to new query. - Theoretical hypotheses: implicit Bayesian inference; gradient descent in the forward pass (Akyürek et al., von Oswald et al. — showed attention can implement gradient steps on linear regression). - Not fully understood — say so. ## 4.5 Hallucinations & faithfulness ### Q: Why do LLMs hallucinate? **Cover**: - Trained to be fluent, not truthful. Loss penalizes "I don't know" if training data confidently asserted something. - Knowledge cutoff + lack of retrieval. - Long-tail facts under-represented. - Sycophancy from RLHF — reward models prefer confident answers. - Compression: model "remembers" gist not exact facts. ### Q: How to reduce hallucinations in production? **Cover** (menu): - RAG with strict citations. - Constrained decoding (JSON schema, grammars). - Self-consistency, self-verification. - Tool use for facts (calculator, search, DB). - Confidence estimation (logprobs, sampling agreement, "I don't know" calibration). - Fine-tune on "answer only when grounded." - Guardrails / output filters. - Smaller, focused models often less hallucinate-y than bigger generalists in narrow tasks. ## 4.6 Evaluation ### Q: How do you evaluate LLMs? **Cover**: - **Capability benchmarks**: MMLU (knowledge), HumanEval/MBPP (code), GSM8K/MATH (math), BBH, ARC, HellaSwag, TruthfulQA, GPQA, MMMU (multimodal). Saturating — be skeptical. - **Instruction-following**: IFEval, MT-Bench, Arena-Hard, AlpacaEval. - **Reasoning**: ARC-AGI, MATH, AIME. - **Long-context**: needle-in-haystack, RULER, LongBench. - **Chatbot Arena**: human pairwise preferences — gold standard. - **Domain-specific**: your eval set is what matters in production. - **LLM-as-judge**: with caveats (position bias, verbosity bias, self-preference). Use pairwise + randomize. - **Reference-based**: BLEU, ROUGE, METEOR — outdated for open-ended gen. ### Q: Perplexity? **Cover**: exp(average negative log-likelihood per token). Lower is better. Only comparable for same tokenizer. Tracks pre-training loss; weak proxy for downstream performance. ## 4.7 Decoding strategies ### Q: Walk through decoding methods. **Cover**: - **Greedy**: argmax. Deterministic, repetitive. - **Beam search**: keep top-k partial sequences. Good for translation, bad for open-ended (mode collapse on generic outputs). - **Sampling**: temperature τ rescales logits (τ → 0 greedy; τ > 1 wilder). - **Top-k**: sample from k highest probs. - **Top-p (nucleus)**: smallest set with cumulative prob ≥ p. Adapts to distribution sharpness. - **Min-p**: tokens with prob ≥ p × max_prob. Newer, robust. - **Typical sampling**, **mirostat**: less common. - **Repetition penalty**, frequency/presence penalties. - **Speculative decoding**: small draft model proposes, large verifies. - **Constrained decoding**: mask logits to enforce grammar/JSON/regex. --- # PART 5 — VLM / MULTIMODAL ### Q: How does CLIP work? **Cover**: - Two encoders: image (ViT or ResNet), text (transformer). - Contrastive InfoNCE loss on (image, caption) pairs — pull matching pairs together in shared embedding space. - Inference: zero-shot classification by similarity to text prompts ("a photo of a {class}"). - Limitations: no generation; fails on counting, compositionality, spatial relations. - Improvements: SigLIP (sigmoid loss, no large batch needed), EVA-CLIP, LiT (locked image tuning). ### Q: How does LLaVA-style VLM work? **Cover** (architecture sketch): - Vision encoder (CLIP-ViT-L) extracts patch embeddings. - Projector (MLP or Q-Former) maps to LLM token space. - LLM (Vicuna, LLaMA) consumes projected tokens + text tokens; generates response. - **Training stages**: 1. Feature alignment: train only projector on image-caption pairs. 2. Instruction tuning: train projector + LLM on multimodal instructions (often GPT-4 generated). - Trade-offs vs Q-Former (BLIP-2): Q-Former compresses image to fewer tokens (efficient) but loses fine detail. ### Q: How do you handle high-resolution images / documents? **Cover**: - Naive resize → loses detail. - Tiling / dynamic resolution: split into patches/tiles, encode each, concatenate (LLaVA-Next, InternVL). - Native any-resolution ViTs (NaViT, Qwen2-VL). - Document-specific: ColPali (ColBERT-style late interaction over patch embeddings) for retrieval without OCR. - Image-token budget vs cost trade-off — image tokens often dominate. ### Q: Open VLMs you'd consider in production? **Cover** (be current — names shift): - LLaVA-Next, Qwen2-VL / Qwen2.5-VL, InternVL2, Pixtral, Molmo, Llama 3.2 Vision, Phi-3.5-Vision, Idefics3, MiniCPM-V. - Document/OCR: GOT-OCR2, Nougat, ColPali. - Closed: GPT-4o, Claude (vision), Gemini. - Choose by: license, latency, image-resolution support, languages, doc-vs-natural-image strength. ### Q: Multimodal evaluation? **Cover**: MMMU, MathVista, DocVQA, ChartQA, OCR-bench, MMBench, RealWorldQA, MME. Pick task-relevant set. --- # PART 6 — RAG INTERVIEW QUESTIONS ### Q: When should you use RAG vs fine-tuning vs long-context? **Cover**: - **RAG**: dynamic / large / private knowledge; needs citations; cheap updates. - **Fine-tuning**: style/format/task patterns; not for adding factual knowledge cheaply (poor recall, expensive updates). - **Long context**: small fixed corpus; cost scales linearly per query; no retrieval infra. Loses precision over very long contexts ("lost in the middle"). - Often combine: fine-tune for style + RAG for facts. ### Q: How do you chunk documents? **Cover**: - Fixed-size (token, char) with overlap (10–20%). - Recursive character splitter (paragraph → sentence → word). - Sentence-aware (spaCy/NLTK). - Semantic chunking (split at embedding similarity drops). - Structure-aware (Markdown headers, HTML, code blocks). - Hierarchical / parent-child (small chunks for retrieval, larger for context). - Late chunking — embed whole doc, then chunk embeddings. - *No universal best* — depends on content. Evaluate empirically. ### Q: Walk through hybrid retrieval. **Cover**: - Sparse (BM25) catches keyword/rare terms; dense (embeddings) catches semantic. - Run both; fuse via Reciprocal Rank Fusion (RRF) — score = Σ 1/(k + rank_i), k≈60. - Or learn weighted combination. - Often +10–20% recall over either alone. ### Q: How does a vector DB perform ANN search? **Cover**: - Brute force O(N·d) — fine up to ~10⁵. - **HNSW**: hierarchical small-world graph — log(N) search, high memory. Most common. - **IVF**: partition into clusters (k-means); search nearest clusters. - **PQ (Product Quantization)**: split vector into subvectors, quantize each → compressed search. - **IVF-PQ**: combine — disk-friendly, billion-scale (FAISS). - **DiskANN**: SSD-backed graph index, billion-scale on a single node. - Trade-offs: recall@k vs latency vs memory vs build time. Tune `efSearch` / `nprobe`. ### Q: How do you evaluate a RAG system? **Cover** (component + end-to-end): - **Retrieval**: Recall@k (does the right chunk appear?), MRR, NDCG, hit rate. - **Generation given context**: faithfulness (no claims unsupported by context), answer relevance, completeness. - **End-to-end**: correctness vs ground truth. - Frameworks: Ragas, TruLens, DeepEval, Arize Phoenix. - LLM-as-judge with rubric; calibrate against human labels. - Build a golden eval set first. ### Q: Common RAG failure modes? **Cover**: - Retrieval miss (wrong chunk). - Chunk too small (missing context) / too large (irrelevant noise). - Ambiguous query → retrieves wrong topic (fix: query rewriting, HyDE, multi-query). - Multi-hop questions (fix: agentic / multi-step retrieval). - Conflicting sources (fix: deduplicate, rerank, summarize). - Outdated chunks (fix: timestamp metadata, prefer recent). - Lost-in-the-middle (fix: rerank + reorder to put best at edges). - LLM ignores retrieved context (fix: prompt engineering, citation requirement, instruction-tuned model). - ACL leakage (fix: filter at retrieval, not post-hoc). ### Q: How do you reduce RAG latency? **Cover**: - Cache embeddings. - Smaller / quantized embedding model. - Reduce top-k via better embeddings or rerank-prune. - Avoid heavy reranker on every query (skip when confidence high). - Stream LLM output. - Prefix-cache common system prompts. - Faster vector DB / lower recall target. - Smaller LLM for simple queries (model routing). --- # PART 7 — AGENTIC AI ### Q: What is an agent vs a workflow? **Cover** (Anthropic's "Building Effective Agents" framing): - **Workflow**: predefined steps; LLM at fixed points. - **Agent**: LLM dynamically decides actions in a loop until done. - Workflows: prompt chaining, routing, parallelization, orchestrator-worker, evaluator-optimizer. - Most production "agents" are actually workflows — and that's *correct* (cheaper, more reliable). Use agents only when paths are open-ended. ### Q: Explain ReAct. **Cover**: Interleave Reasoning (chain-of-thought) and Action (tool call) and Observation (tool result). Loop until done. Strengths: explainability, error correction. Weakness: long traces, cost, loop traps. ### Q: How does function calling work? **Cover**: - Provide tool schemas (name, description, JSON schema params). - Model outputs structured tool call (token-constrained or fine-tuned for it). - Server executes tool, returns result. - Model continues with result. - Parallel calls: model emits multiple tool calls in one turn. - Pitfalls: hallucinated tools/args; mitigate with strict JSON schema, retries with error feedback. ### Q: Agent memory architectures? **Cover**: - **Short-term**: conversation buffer (raw), buffer-window (last N), summary buffer (LLM summarizes older turns). - **Long-term**: vector store keyed by user/session; retrieve relevant past interactions. - **Episodic**: time-stamped events. - **Semantic**: extracted facts / preferences. - **Working / scratchpad**: current task state. - Frameworks: Mem0, Letta (MemGPT, OS-style paging), Zep, LangMem. - Update strategies: append-only, summarize, edit-on-conflict, decay. ### Q: Multi-agent — patterns? **Cover**: - Supervisor + workers (LangGraph supervisor pattern). - Role-based crew (CrewAI — researcher, writer, reviewer). - Debate (multiple agents argue, judge picks). - Hierarchical (manager → team leads → workers). - Hand-off (OpenAI Swarm style). - Caveat: multi-agent often loses to one well-prompted agent on cost & reliability — use when truly parallel work or distinct expertise needed. ### Q: How do you evaluate agents? **Cover**: - Task-level success rate. - Trajectory metrics: number of steps, tool-call accuracy, cost per task, time per task. - Component evals: planning quality, tool selection, parameter correctness, error recovery. - Benchmarks: SWE-bench (code), τ-bench (tool use), WebArena / VisualWebArena (browser), GAIA (general assistant), OSWorld (computer use), AppWorld. - Build a domain task suite with golden trajectories. - Replay capability — re-run old traces against new versions to catch regressions. ### Q: Production challenges with agents? **Cover**: - Cost spiral (set step + token budgets, halt rules). - Latency (parallelize independent tool calls). - Non-determinism (low temperature, seed, snapshot-test critical paths). - Safety: prompt injection via tool outputs; sandbox writes; allow-list endpoints; least-privilege tools; separate untrusted content from instructions; structured outputs prevent jailbreaks via tool returns. - Observability: full trace per run (LangSmith, Langfuse, Phoenix, Helicone). - Versioning of prompts/tools/models. --- # PART 8 — INFERENCING / SERVING DEEP DIVE This is a hot topic in 2025 interviews. Know it cold. ### Q: Walk through what happens during LLM inference token by token. **Cover**: - **Prefill**: process all prompt tokens in parallel → fill KV cache for each layer. Compute-bound (matrix-matrix multiplies on big matrices). - **Decode**: for each new token, only attention against cached K, V; one new token → one matrix-vector multiply per layer. Memory-bandwidth-bound (you're reading the whole model for every token). - Each generated token requires reading every model parameter from HBM → arithmetic intensity is very low → bandwidth limits throughput. ### Q: What is the KV cache and how big is it? **Cover**: - During decode, K and V from prior tokens are reused → cache them. - Size per token = 2 (K and V) × n_layers × n_heads × d_head × dtype_bytes. - E.g., LLaMA-3 70B, d_model=8192, 80 layers, 8 KV heads (GQA), d_head=128, FP16: 2 × 80 × 8 × 128 × 2 = 327 KB / token. 128k context = ~40GB just for KV. - KV cache often dominates memory over weights at long context. - Mitigations: GQA/MQA, KV quantization (INT8/FP8), paged attention, KV eviction (StreamingLLM, H2O). ### Q: Explain continuous batching. **Cover**: - Naive static batching: wait for full batch → high latency, low GPU util (variable lengths cause stragglers). - Continuous (in-flight) batching: process at iteration level — new sequences join mid-batch as others finish. - Different sequences may be in different decoding stages. - Massive throughput gains (3-20x) — core to vLLM, TGI, TensorRT-LLM. ### Q: Explain paged attention. **Cover**: - Standard KV cache requires contiguous memory per sequence → either pre-allocate max length (waste) or grow dynamically (fragmentation). - PagedAttention: split KV cache into fixed-size blocks; per-sequence block table maps logical → physical blocks (like virtual memory). - Enables: near-zero memory waste, sharing prefix KV across requests (prefix caching), dynamic batching. ### Q: Speculative decoding? **Cover**: - Small draft model proposes k tokens; large verifier model runs them in one forward pass; accept longest matching prefix using rejection sampling. - Maintains exact distribution of the verifier. - 2–3× speedup when draft is good and aligned with verifier. - Variants: Medusa (multi-head extra prediction in same model), EAGLE (better draft architecture), lookahead decoding (no draft model, parallel verification of n-grams). ### Q: Quantization — types & trade-offs? **Cover**: - **PTQ (post-training)**: calibrate on small dataset. Cheap. Common methods: - **GPTQ**: layer-wise weight quant, minimize output error. - **AWQ**: protect important weight channels by activation magnitude. - **SmoothQuant**: shift activation outlier magnitude into weights. - **GGUF (llama.cpp)**: various k-quants (Q4_K_M, Q5_K_M etc.) for CPU/edge. - **QAT**: quantize during training; best quality, expensive. - **Bit widths**: FP16/BF16 (baseline), FP8 (H100+; minimal loss), INT8 (good), INT4 (some loss, big memory savings), INT2/INT3 (only for huge models). - **What you quantize**: weights, activations, KV cache. Weight-only is safest. - **Engines**: GPTQ on vLLM, AWQ on vLLM/TGI, TensorRT-LLM (FP8/INT8 well-supported). ### Q: What is FlashAttention? **Cover**: - IO-aware exact attention. - Standard attention materializes n×n score matrix in HBM → memory-bound and OOM at long seq. - Flash: tile Q, K, V into blocks that fit in SRAM; compute softmax online (using running max trick for numerical stability); never materialize full matrix. - FA2: better parallelism across heads & seq. - FA3: H100-specific (async, FP8). Used in nearly all modern training/inference. ### Q: Throughput vs latency for LLM serving — how do they trade? **Cover**: - Throughput ↑ with larger batches (utilize GPU compute). - Latency per request ↑ with larger batches. - TTFT (time-to-first-token) dominated by prefill (compute-bound, scales with prompt length). - TPOT (time per output token, inter-token latency) dominated by decode (memory-bw-bound, scales with model size and batch). - Decision: bound queue wait + max batch size by SLA. - Disaggregated serving (Splitwise / DistServe): separate prefill and decode pools — different GPU types, independent scaling. ### Q: How would you serve a 70B model with low latency? **Cover** (whiteboard checklist): - Quantize to FP8/INT8 weights, fit on one or two H100s (with tensor parallel). - Use vLLM or TensorRT-LLM with continuous batching + paged attention. - Enable prefix caching for shared system prompts. - GQA (LLaMA-3 has it) reduces KV cache memory. - Speculative decoding with smaller draft (Llama-3 8B → 70B). - Streaming response to client. - Horizontal scaling behind a router; route by load + prefix-cache affinity. - For very-low-latency: route easy queries to smaller model (cascade). - Optimize prompt length; truncate; summarize. ### Q: Distillation? **Cover**: - Train smaller "student" to mimic larger "teacher." - Targets: hard labels, soft labels (teacher probabilities — Hinton KD), feature matching (hidden states), logit matching, sequence-level. - For LLMs: collect (prompt, teacher_response) pairs, SFT student; sometimes step-by-step CoT distillation; or on-policy distillation (DistillKL on student's own outputs). - Common production move: prototype with GPT-4, distill into Llama-3-8B for cost. ### Q: Compare vLLM vs TGI vs TensorRT-LLM. **Cover** (be current — moves fast): - **vLLM**: OSS, paged attention origin, easiest integration, broad model support, strong throughput. Default first choice. - **TGI (HuggingFace)**: prod-ready, multi-model serving, integrates with HF ecosystem; competitive but moves slower than vLLM. - **TensorRT-LLM (NVIDIA)**: highest performance on NVIDIA H100/H200; FP8 support, in-flight batching, specula­tive decoding; tighter coupling, more engineering work. - **SGLang**: fastest for structured generation, agents (RadixAttention prefix sharing). - **llama.cpp**: CPU / Apple Silicon / edge. - Pattern: vLLM/SGLang for OSS speed; TRT-LLM when squeezing last 30%; managed (Together, Fireworks, Anyscale, Bedrock) when you don't want to operate. --- # PART 9 — ML CODING QUESTIONS You'll be asked to *implement* from scratch. Practice these in NumPy / PyTorch: | Problem | What to demonstrate | |---|---| | Implement K-means | Init, assign, update, convergence, vectorize with NumPy | | Implement logistic regression with SGD | Forward, gradient, loss curve | | Implement linear regression closed form + GD | Normal equation, regularization | | Implement KNN | Distance broadcasting, top-k argsort | | Softmax numerically stable | Subtract max before exp | | Cross-entropy from logits | Logsumexp trick | | Scaled dot-product attention | Q K V, mask, softmax, matmul | | Multi-head attention | Reshape into heads, concat | | Causal mask | Triangular | | LayerNorm / RMSNorm | Mean/var across last dim, affine | | Positional encoding (sinusoidal + RoPE) | Frequency bands | | Transformer block | Pre-norm, attention, FFN, residuals | | BPE tokenizer (toy) | Pair counts, merges | | Training loop in PyTorch | Optimizer, scheduler, AMP, grad clip | | BatchNorm in train vs eval | Running stats | | Beam search | Heap, length normalization | | Top-k / top-p sampling | Mask logits, sample | | Top-N retrieval w/ cosine sim | Normalize then dot product | | HNSW concept walkthrough | Layered graph, greedy descent | | Implement RAG end-to-end on a toy corpus | Embed, store, retrieve, prompt | **Tip**: practice on a whiteboard, not just in IDE. Speak as you code. --- # PART 10 — STATISTICS, PROBABILITY, A/B TESTING ### Q: Bayesian vs frequentist? **Cover**: - Frequentist: parameter is fixed unknown; probability over data; p-values, CIs. - Bayesian: parameter has distribution; combine prior with likelihood → posterior. Credible intervals are interpretable directly. - MLE = mode of likelihood. MAP = mode of posterior. - Bayesian gives uncertainty over parameters; useful for small data, decision making. ### Q: Walk me through an A/B test. **Cover**: - Hypothesis (H0, H1), primary metric, guardrails, MDE (minimum detectable effect). - Power analysis → sample size: n ≈ 16σ²/Δ² per arm (rule of thumb, two-sided 80% power, α=0.05). - Randomization unit (user, session) — same unit across tests. - Run for full weekly cycle (day-of-week effects). - Analysis: two-sample t / z / Welch's; for ratios use delta method; for proportions Wald or bootstrap. - Multiple testing correction (Bonferroni / Benjamini-Hochberg). - Watch for: SRM (sample ratio mismatch — sign of bug), novelty effect, network effects (use cluster randomization), interaction with other experiments. - Sequential testing (mSPRT, group sequential) if peeking; else fixed-horizon. - CUPED for variance reduction using pre-experiment data. ### Q: Common probability distributions and uses? **Cover**: - Bernoulli (single binary), Binomial (n trials), Poisson (counts, rare events), Geometric / Negative binomial (overdispersed counts). - Uniform, Normal, Log-normal (multiplicative processes), Exponential (waiting times), Gamma, Beta (proportions, conjugate to Bernoulli/Binomial), Dirichlet. - Heavy-tailed: power-law, Pareto — important for real-world (revenue, latency). ### Q: Central Limit Theorem? When does it fail? **Cover**: Sum of n IID variables with finite mean & variance → Normal as n → ∞. Fails for infinite-variance distributions (Cauchy, power-law with α≤2). Tail latency, financial returns often need different treatment (stable distributions, EVT). ### Q: Bayes' rule on a clinical-test scenario. **Cover**: P(disease|+) = P(+|disease) P(disease) / P(+). Even high sensitivity/specificity gives low PPV when prevalence is tiny. Memorize the intuition: base rates dominate. --- # PART 11 — BEHAVIORAL & ML PROJECT DEEP-DIVE ### Q: Tell me about an ML project you're proud of. **Frame** (STAR + ML twist): - **Context**: business problem, scale, constraints. - **Approach**: data, baseline, iteration, why each choice. - **Trade-offs**: what you didn't do and why. - **Result**: business + ML metrics; A/B numbers if you have them. - **Failure & learning**: be candid about what didn't work. - 5–7 min total. Pre-rehearse 2–3 projects at different depths. ### Q: Tell me about a time a model failed in production. **Have a real story** covering: - Symptom (metric drop, complaints, outage). - Detection (was monitoring there? how fast?). - Root cause (drift, bad feature, infra, label noise, leakage). - Mitigation (rollback, fix, retrain). - Postmortem learning + system change so it can't happen again. ### Q: Why did you choose model X over Y? **Cover**: explicit metric goal, candidates considered, trade-offs (accuracy, latency, cost, interpretability, training data needs, ops burden), experiments run, and explicit decision criterion. Don't hand-wave. ### Q: How do you keep up with the field? **Cover** (be honest): papers (ArXiv-sanity, Hugging Face Daily), blogs (Anthropic, OAI, Google, Eugene Yan, Chip Huyen, Lilian Weng), Twitter/X researchers, podcasts, hands-on (build something with new tech monthly). Mention one concrete recent paper/tool you tried. --- # PART 12 — TOP-100 RAPID-FIRE QUESTIONS If asked any of these you should answer in <60 sec. **Classical ML** 1. Bias-variance trade-off? 2. L1 vs L2? 3. Why softmax & not normalize? 4. Why use log-loss not MSE for classification? 5. Difference between bagging and boosting? 6. How does XGBoost handle missing values? 7. What is entropy and information gain? 8. Why are decision trees high-variance? 9. Cross-validation strategies for time series? 10. ROC vs PR curves? 11. What does AUC mean? 12. Precision, recall, F1 derivation? 13. How does Naive Bayes work and when? 14. KNN — pros and cons? 15. PCA — math and limits? 16. SMOTE — how and when? 17. What is multicollinearity and how to detect? 18. Difference between covariance and correlation? 19. How does feature importance work in random forest? 20. SHAP values — high-level? **Deep Learning** 21. Backprop math? 22. Why vanishing gradients? 23. ReLU vs GELU? 24. Why does dropout work? 25. BatchNorm vs LayerNorm? 26. Why Adam over SGD? 27. Why warmup learning rate? 28. Weight initialization schemes? 29. Why residual connections? 30. Diffusion vs GAN? 31. VAE reparameterization trick? 32. Self-supervised vs supervised? 33. Contrastive loss? 34. Triplet loss vs InfoNCE? 35. Transfer learning strategies? **Transformers** 36. Why scale by √d_k in attention? 37. Multi-head attention — why? 38. Sinusoidal vs RoPE? 39. ALiBi? 40. Encoder vs decoder vs enc-dec? 41. Causal mask — how? 42. MQA vs GQA vs MHA? 43. FlashAttention idea? 44. Why pre-norm? 45. Tokenization methods? 46. Byte-level BPE benefit? 47. Why decoder-only won? 48. KV cache size formula? **LLMs** 49. Chinchilla scaling? 50. SFT vs RLHF vs DPO? 51. LoRA math? 52. QLoRA — what's new? 53. CoT — why does it work? 54. Self-consistency? 55. Temperature, top-k, top-p? 56. Why do LLMs hallucinate? 57. Constitutional AI? 58. GRPO? 59. Evaluating LLMs — MMLU caveat? 60. LLM-as-judge biases? **RAG** 61. RAG vs fine-tune vs long context? 62. Chunking strategies? 63. HNSW vs IVF-PQ? 64. Hybrid search? 65. Reranker — why and how? 66. HyDE? 67. RAG evaluation — Ragas? 68. Lost-in-the-middle? 69. Late interaction (ColBERT)? 70. Permissions in RAG? **VLM** 71. CLIP training? 72. SigLIP vs CLIP? 73. LLaVA architecture? 74. Q-Former (BLIP-2)? 75. High-resolution image handling? 76. ColPali? 77. Multimodal evaluation? **Agentic** 78. ReAct vs Plan-and-Execute? 79. Memory types? 80. Tool calling — failure modes? 81. Multi-agent vs single agent? 82. Prompt injection defenses? 83. Agent evaluation? 84. MCP? **Inference** 85. Prefill vs decode bottlenecks? 86. KV cache memory math? 87. Continuous batching vs static? 88. PagedAttention? 89. Speculative decoding? 90. Quantization methods? 91. GPTQ vs AWQ? 92. FP8 vs INT8? 93. vLLM vs TGI vs TensorRT-LLM? 94. Disaggregated serving? 95. Distillation for LLMs? **Production / Stats** 96. A/B test minimum sample size? 97. Variance reduction techniques (CUPED)? 98. SRM — what and why? 99. Data drift detection? 100. Bayes' rule with low base rate? --- # Final Tracker - [ ] Part 1 — Classical ML - [ ] Part 2 — Deep Learning - [ ] Part 3 — Transformers - [ ] Part 4 — LLMs - [ ] Part 5 — VLM - [ ] Part 6 — RAG - [ ] Part 7 — Agentic - [ ] Part 8 — Inferencing - [ ] Part 9 — ML coding - [ ] Part 10 — Stats & A/B - [ ] Part 11 — Behavioral prep - [ ] Part 12 — 100-Q rapid fire (run twice, timed) When you can answer the 100 rapid-fire in <90 minutes total, and have 3 deep project stories rehearsed cold — you're interview-ready.
# AI/ML Interview Prep — Theory, Models, and Production Deep-Dive
> Companion to the System Design roadmap.
> Covers what interviewers actually ask in ML/DL/LLM/RAG/Agentic/VLM/Inferencing rounds.
> Format per topic: **concept → likely questions → what to cover in your answer → gotchas / follow-ups**.
---
## How to use this guide
- Each section has questions ordered roughly easy → hard.
- For each question, the "Cover" bullets are the **bare minimum** to say. Add detail/examples on top.
- Interviewers probe for: (1) correctness, (2) intuition, (3) trade-offs, (4) when it fails. Hit all four.
- When you don't know: say "I'd reason from first principles" and do exactly that. Never bluff specifics.
- Always tie answers to *production*. You have 3 yrs experience — your edge is "I've seen X break this way."
---
# PART 1 — CLASSICAL ML THEORY
## 1.1 Fundamentals
### Q: Explain the bias-variance trade-off.
**Cover**:
- Total expected error = bias² + variance + irreducible noise (decompose for squared loss).
- Bias = error from wrong assumptions (underfitting). Variance = sensitivity to training set (overfitting).
- High capacity model = low bias, high variance. Regularization, more data, simpler model reduce variance.
- Show curve: training error decreases monotonically; validation error U-shape.
- **Modern twist**: in over-parameterized deep nets we see *double descent* — error drops, rises near interpolation threshold, then drops again. Mention if asked about DL.
**Gotchas**: They may ask "how do you know if you're in bias or variance regime?" → Look at train vs val gap. Small gap + high error = bias. Large gap = variance.
### Q: Generative vs discriminative models — examples and trade-offs?
**Cover**:
- Discriminative models P(y|x) directly (logistic regression, SVM, most NNs).
- Generative models P(x, y) or P(x|y) and use Bayes (Naive Bayes, GMM, HMM, LDA, diffusion, GANs, LLMs technically).
- Discriminative usually beats generative on classification with enough data; generative can be sampled from and handles missing data.
- Naive Bayes converges faster with little data (asymptotic argument from Ng & Jordan paper).
### Q: How do you handle imbalanced datasets?
**Cover** (give a *menu*, then say which you'd pick):
- Re-sampling: random oversampling, SMOTE / ADASYN, random undersampling, NearMiss, Tomek links.
- Class weights in the loss (often best first move — cheap and effective).
- Threshold tuning (don't use 0.5).
- Anomaly-detection framing if extremely rare (<0.1%).
- Two-stage models: cheap recall stage + heavy precision stage.
- Right metric: PR-AUC > ROC-AUC for rare-positive problems; F-beta if recall vs precision asymmetry matters; MCC if both classes matter.
- Stratified k-fold CV.
**Gotcha**: SMOTE in high-dim sparse data (NLP) is usually bad — interpolation isn't meaningful.
### Q: Why is feature scaling important? When can you skip it?
**Cover**:
- Required: distance-based (KNN, K-means, SVM with RBF), gradient-based (NNs, log reg), regularized models (Ridge/Lasso punish large coefficients unfairly).
- Not required: tree-based (RF, XGBoost) — splits are scale-invariant.
- Methods: standardization (z-score), min-max, robust (median/IQR), log/power transforms (Box-Cox, Yeo-Johnson), quantile transform.
- Fit scaler on train only; apply to val/test. Else: data leakage.
### Q: Curse of dimensionality?
**Cover**:
- Volume grows exponentially → data becomes sparse → distances concentrate (all points roughly equidistant) → KNN/clustering degrade.
- Need exponentially more data to maintain density.
- Mitigation: dimensionality reduction (PCA, autoencoders), feature selection, manifold assumption (real data lives on lower-dim manifold — why DL works).
## 1.2 Linear models
### Q: Derive logistic regression.
**Cover**:
- Linear combo: z = w·x + b.
- Sigmoid: σ(z) = 1/(1+e^(-z)).
- Bernoulli likelihood → log-loss / binary cross-entropy.
- No closed form → gradient descent. Gradient: (σ(z) − y) · x. (Be ready to derive on whiteboard.)
- L1/L2 regularization → MAP with Laplace/Gaussian prior on weights.
- Multi-class: softmax + categorical CE.
**Follow-ups**: Why log-loss not MSE? → Non-convex with sigmoid+MSE, slow gradients in saturation, log-loss is convex.
### Q: L1 vs L2 regularization?
**Cover**:
- L1 (Lasso): |w| — induces sparsity (corners of diamond constraint touch axes), feature selection.
- L2 (Ridge): w² — shrinks all weights smoothly, handles multicollinearity, has closed form.
- Elastic Net combines both.
- Bayesian view: L2 = Gaussian prior; L1 = Laplace prior.
- L1 not differentiable at 0 → subgradient or proximal methods (coordinate descent in glmnet).
### Q: How does multicollinearity affect linear regression?
**Cover**:
- Coefficient estimates unstable / high variance, p-values inflated, signs flip.
- Detect: VIF > 5–10, correlation matrix, condition number.
- Fix: drop a feature, PCA, Ridge regression, partial least squares.
## 1.3 Tree-based models
### Q: How does a decision tree decide a split?
**Cover**:
- Classification: minimize Gini impurity or entropy / maximize information gain.
- Regression: minimize variance / MSE.
- Greedy; one feature, one threshold at a time.
- Stopping: max depth, min samples leaf, min impurity decrease.
- Prone to overfitting — pruning (cost-complexity α).

Xet Storage Details

Size:
57.1 kB
·
Xet hash:
d122517abf77d3714306cdfbf3da2acbe6c2c33d32d1d221b390eea080ebad47

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.