Drills for every week

Practice

Reading a note takes an hour. Owning it takes problems. Every week here has hand problems, coding drills and a project milestone.

How to use this page

  1. By hand first. Paper and pen, no computer. These are the small calculations interviewers ask you to do on a whiteboard. Check your answers with code afterwards, never before.
  2. Then code. Type every drill yourself in a fresh file. Before running, write down what you expect to see. When reality differs, that gap is the lesson.
  3. Saturday is the milestone. Each week moves the current project forward. The milestone is done when it runs and has a test.
  4. Keep a mistakes file. One line per mistake: what you got wrong and the rule that fixes it. Reread it every Sunday. It becomes your best revision document.
W1

Vectors, matrices and linear regression

Notes this week. ML: What machine learning is: data, model, loss, optimizer · Math: Functions, exponents and logarithms · Math: Vectors and the dot product · ML: Linear regression · Math: Matrices, matrix multiplication and shapes · Math: Norms, distance and cosine similarity · ML: Feature scaling, normalization and encoding.

By hand

  1. Compute ten dot products of small vectors (2 to 4 numbers) and say for each whether the vectors point roughly the same way, opposite ways or at right angles.
  2. Multiply a 2×3 matrix by a 3×2 matrix, then by a 3×1 column. Write the shape of the result before you compute it, every time.
  3. Given ten random matrix shapes, decide which pairs can be multiplied and in which order; write the output shape.
  4. Compute cosine similarity between three pairs of vectors by hand. Change one number and predict whether the similarity rises or falls.
  5. Simplify ten expressions using exponent and logarithm rules: log(ab), log(a/b), log(a^n), e^(ln x), and log base change.
  6. Fit a line by eye to five points on paper, then compute the mean squared error of your line.

Code

  1. In NumPy, implement dot product, matrix multiplication and cosine similarity with plain loops, then assert they match np.dot, @ and the vector formula.
  2. Write a function that checks whether two arrays can be matrix-multiplied and returns the output shape, with tests.
  3. Load a small dataset (the diabetes or California housing set from scikit-learn). Compute mean, min and max of each column. Scale the columns to zero mean and unit variance by hand.
  4. Fit linear regression three ways: the closed-form normal equation in NumPy, scikit-learn’s LinearRegression, and a hand-written loop that tries random slopes and keeps the best. Compare the coefficients.
  5. Plot the loss of a one-feature linear regression as a function of the slope. Find the minimum by eye, then by code.

Saturday milestone

Project 0: create the repository, a package layout with tests, and a working LinearRegression class (normal equation) with a test against scikit-learn.

Problem sets and sources

W2

Derivatives, gradients and gradient descent

Notes this week. Math: Derivatives: slope and rate of change · ML: Loss functions: MSE, MAE and log loss · Math: Partial derivatives and the gradient · ML: Gradient descent: batch, stochastic and mini-batch · Math: The chain rule · ML: Overfitting, underfitting and the bias–variance trade-off.

By hand

  1. Differentiate twenty simple functions: polynomials, e^x, ln x, 1/x, and products and quotients of them.
  2. For f(x, y) = x²y + 3y, compute both partial derivatives and the gradient at (1, 2). Draw the arrow on a sketch.
  3. Apply the chain rule to ten nested functions, including sigmoid(3x + 1) and (2x + 1)⁵. Write every intermediate step.
  4. Compute the derivative of mean squared error with respect to the slope for a two-point dataset, by hand.
  5. Run three steps of gradient descent by hand on f(x) = (x − 3)² from x = 0 with learning rate 0.1, then with learning rate 1.1. Explain what happened in the second case.
  6. Sketch what overfitting and underfitting look like on a scatter plot, and label which has high bias and which has high variance.

Code

  1. Write a numerical derivative function using (f(x + h) − f(x − h)) / 2h and check your hand answers from the by-hand list.
  2. Implement gradient descent for one-feature linear regression in NumPy. Plot the loss per step for learning rates 0.001, 0.01, 0.1 and 1.0.
  3. Extend it to many features using matrix form. Compare with the normal equation from Week 1.
  4. Implement batch, stochastic and mini-batch gradient descent; plot all three loss curves on one chart.
  5. Fit polynomials of degree 1, 3 and 15 to 20 noisy points. Plot train and test error against degree.

Saturday milestone

Project 0: gradient-descent trainer with pluggable loss, learning-rate schedule and loss logging; MSE and MAE losses with tests on their gradients.

Problem sets and sources

W3

Probability, logistic regression and evaluation

Notes this week. Math: Mean, median, variance and standard deviation · Math: Probability, conditional probability and Bayes’ theorem · ML: Logistic regression and the sigmoid · Math: Distributions, expectation and the normal distribution · ML: Evaluation metrics: accuracy, precision, recall, F1, ROC-AUC · Math: Maximum likelihood: where loss functions come from · ML: Train, validation, test and cross-validation · ML: Regularization: L1, L2 and early stopping.

By hand

  1. Compute mean, median, variance and standard deviation of three small lists. Add one outlier and recompute: which changed most?
  2. Solve five Bayes’ theorem problems, including the classic medical test with a 1% base rate and a 95% accurate test.
  3. Compute the expected value and variance of a die roll and of a biased coin.
  4. Evaluate sigmoid(z) for z = −4, −1, 0, 1, 4 by hand, and log loss for predictions 0.9, 0.5 and 0.1 when the true label is 1.
  5. Fill in a confusion matrix from a list of 20 predictions and labels, then compute accuracy, precision, recall and F1.
  6. Draw an ROC curve from five thresholds on a tiny dataset.
  7. Explain in writing why maximizing likelihood for a yes/no model gives the log-loss formula.

Code

  1. Simulate 10,000 coin flips and dice rolls; compare sample means and variances with the formulas.
  2. Implement logistic regression in NumPy with gradient descent on the breast-cancer dataset. Compare the accuracy with scikit-learn.
  3. Implement accuracy, precision, recall, F1 and ROC-AUC from scratch; check against scikit-learn.metrics.
  4. Write k-fold cross-validation from scratch and use it to pick the learning rate.
  5. Plot the normal distribution for three different means and standard deviations; shade one standard deviation.

Saturday milestone

Project 0: LogisticRegression class, a metrics module and a cross-validation helper, each tested against scikit-learn.

Problem sets and sources

W4

Entropy, regularization, similarity models and trees

Notes this week. Math: Entropy, cross-entropy and KL divergence · ML: kNN and k-means: learning from similarity · Math: Eigenvectors and SVD, only as much as PCA and LoRA need · ML: PCA and dimensionality reduction · ML: Decision trees, random forests and gradient boosting · ML: SVM and Naive Bayes at interview depth · ML: Class imbalance, data leakage and other traps · Math: Math you can skip, and what to say if it comes up.

By hand

  1. Compute the entropy of a fair coin, a 90/10 coin and a fair die in bits.
  2. Compute cross-entropy and KL divergence between p = (0.5, 0.5) and q = (0.9, 0.1), both ways round. Note which is smaller.
  3. Write the L1 and L2 regularized loss for a two-weight model and compute the gradient of each penalty. Say in words why L1 makes weights exactly zero.
  4. Classify three new points by hand with kNN (k = 3) on a ten-point dataset; then run two iterations of k-means with k = 2.
  5. Compute the information gain of one split in a tiny decision tree.
  6. Given a dataset with 99% negatives, explain why 99% accuracy can be useless, and name two metrics that would expose it.

Code

  1. Implement kNN and k-means in NumPy; check against scikit-learn on the iris dataset.
  2. Implement PCA with NumPy’s SVD; project the iris data to two dimensions and plot it coloured by class.
  3. Add L2 regularization to your logistic regression; plot weight sizes against the regularization strength.
  4. Train a decision tree, a random forest and gradient boosting with scikit-learn on one tabular dataset; compare cross-validated scores and training time.
  5. Create a deliberately leaky feature (one computed from the label) and watch the validation score become suspiciously perfect. Then remove it.

Saturday milestone

Project 0: kNN, k-means and PCA added to the library. Tabular case study started: a Kaggle dataset with a clean split, a baseline and a results table.

Problem sets and sources

W5

Neurons, backpropagation and softmax

Notes this week. DL: What is a neural network? · DL: Backpropagation: how networks learn · DL: Logits, softmax and cross-entropy · DL: Build a neural network in NumPy, end to end.

By hand

  1. Run the walk neuron and the XOR network from the neural-network note by hand on new inputs.
  2. Count the parameters of three small networks drawn on paper.
  3. Backpropagate by hand through a two-layer network with one hidden neuron, writing every local derivative.
  4. Compute softmax of (2, 1, 0) and the cross-entropy against each possible true class.
  5. Derive the gradient of cross-entropy with softmax with respect to the logits, and check it equals prediction minus target.

Code

  1. Rewrite micrograd from Karpathy’s video from memory: a scalar Value class with backward().
  2. Implement a two-layer network in NumPy with manual backprop; train it on XOR and then on a two-moons dataset.
  3. Check every gradient with a numerical gradient check.
  4. Implement softmax safely (subtract the maximum) and show what happens without the subtraction for large logits.

Saturday milestone

Project 0: a NumPy MLP with configurable layers trained on MNIST digits to above 95% test accuracy, with a gradient check in the tests.

Problem sets and sources

W6

Optimizers, PyTorch and regularization

Notes this week. DL: Optimizers: SGD with momentum, Adam, AdamW and the learning rate · DL: PyTorch essentials: tensors, autograd, the training loop · DL: Dropout, BatchNorm, LayerNorm and weight decay · DL: CNNs, RNNs and LSTMs: what they are and why attention replaced them.

By hand

  1. Run three steps of SGD with momentum and three of Adam by hand on f(x) = x², tracking every running average.
  2. Write the LayerNorm formula and apply it to the vector (1, 2, 3, 6).
  3. Explain dropout at train time and at test time, including the scaling, in writing.
  4. Draw a convolution of a 3×3 filter over a 5×5 image and give the output size; then do it with padding 1.

Code

  1. Reimplement your NumPy MLP in PyTorch with nn.Module, then train it with SGD, SGD+momentum, Adam and AdamW; plot the four loss curves.
  2. Add dropout and weight decay; compare train and test accuracy with and without them.
  3. Add LayerNorm and BatchNorm; measure the effect on training speed.
  4. Write a learning-rate finder: increase the learning rate each step and plot loss against learning rate.
  5. Train a tiny character-level RNN on a short text, generate text, and note where it fails. This is the motivation for attention.

Saturday milestone

Project 0 complete: NumPy and PyTorch MLPs with a README comparing optimizers and regularization on MNIST. Write the resume bullet.

Problem sets and sources

W7

Tokens, embeddings and the attention idea

Notes this week. LLM internals: What is an LLM? Pretraining, fine-tuning and the big picture · LLM internals: Tokenization and byte-pair encoding (BPE) · LLM internals: Token embeddings: turning IDs into vectors · LLM internals: Positional encoding: telling the model about word order · LLM internals: Preparing data: sliding windows and next-token prediction · LLM internals: Self-attention, the simple version.

By hand

  1. Run three merges of byte-pair encoding by hand on a short string.
  2. Given a 5-word vocabulary and 3-dimensional embeddings, look up a sentence and write the resulting matrix and its shape.
  3. Compute a simplified attention: three 2-dimensional vectors, dot products, softmax, weighted sum.

Code

  1. Implement BPE training and encoding from scratch; compare the token count with tiktoken on the same paragraph.
  2. Build the sliding-window dataset and data loader; print input and target pairs and check them by eye.
  3. Implement simplified self-attention (no trainable weights) in PyTorch with loops, then with matrix operations; assert equal.

Saturday milestone

Project 1: tokenizer and data pipeline done, with tests.

W8

Full attention and the transformer block

Notes this week. LLM internals: Scaled dot-product attention: queries, keys and values · LLM internals: Causal masking and attention dropout · LLM internals: Multi-head attention · LLM internals: LayerNorm, GELU, feed-forward layers and residual connections · LLM internals: The transformer block and the full GPT architecture · LLM internals: Counting parameters and weight tying.

By hand

  1. Write the shapes of Q, K, V, the score matrix and the output for batch 2, 4 tokens, 8 dimensions, 2 heads.
  2. Explain why scores are divided by the square root of the key dimension, with a numerical example.
  3. Count the parameters of one GPT-2 small transformer block.

Code

  1. Implement causal multi-head attention from memory, then the full transformer block and the GPT model.
  2. Write a parameter-count test that reproduces 124M for the GPT-2 small configuration.
  3. Visualize an attention map for a short sentence.

Saturday milestone

Project 1: full model forward pass works; parameter count matches GPT-2 small.

W9

Training, decoding and fine-tuning

Notes this week. LLM internals: Pretraining: the training loop, cross-entropy and perplexity · LLM internals: Decoding: greedy, temperature, top-k and top-p · LLM internals: Loading pretrained GPT-2 weights · LLM internals: Fine-tuning for classification · LLM internals: Instruction fine-tuning · LLM internals: Encoder, decoder, encoder–decoder: BERT vs GPT vs T5.

By hand

  1. Compute perplexity from a cross-entropy loss of 2.3, and explain what it means in words.
  2. Apply temperature 0.5 and 2.0 to the logits (2, 1, 0) and compute the new probabilities.
  3. Walk through top-k and top-p sampling on a five-token distribution.

Code

  1. Write the pretraining loop with warmup, cosine decay and gradient clipping; train on TinyStories on a free GPU.
  2. Implement greedy, temperature, top-k and top-p decoding.
  3. Load GPT-2 weights into your model and fine-tune it as a classifier and then on instructions.

Saturday milestone

Project 1 complete with a README and generated samples.

W10

Modern architecture, LoRA and quantization

Notes this week. Modern: From GPT-2 to modern LLMs: pre-norm, RMSNorm, SwiGLU, grouped-query attention · Modern: Rotary position embeddings (RoPE) · Modern: The KV cache: why generation is fast · Modern: Quantization: 16-bit, 8-bit, 4-bit and GGUF · Modern: LoRA, QLoRA and parameter-efficient fine-tuning · Modern: Teaching preferences: RLHF and DPO · Modern: Context windows and reasoning models · Modern: Prompting vs RAG vs fine-tuning: how to choose.

By hand

  1. Count the parameters LoRA adds to a 768×768 layer at rank 8, and the percentage of the original.
  2. Convert four float values to 8-bit integers with a scale and zero point, then back; compute the error.
  3. Explain the KV cache with a diagram, and compute its memory for a given model and sequence length.

Code

  1. Implement LoRA from scratch on your Project 1 model and fine-tune only the adapters.
  2. Add a KV cache to your model and measure tokens per second before and after.
  3. Run the full QLoRA fine-tune, convert to GGUF, and run it on your CPU.

Saturday milestone

Project 2 complete with a before-and-after results table.

W11

Embeddings, vector search and retrieval

Notes this week. RAG: Text embeddings and semantic similarity · RAG: Vector databases and approximate nearest neighbour search (HNSW, IVF) · RAG: Chunking strategies · RAG: Retrieval: BM25, dense, hybrid search and rank fusion · RAG: Reranking with cross-encoders.

By hand

  1. Rank five short documents against a query by cosine similarity on tiny hand-made embeddings.
  2. Compute BM25 for one query term over three documents.
  3. Combine two rankings with reciprocal rank fusion by hand.

Code

  1. Embed a corpus, build a brute-force search, then an HNSW index; compare recall and speed.
  2. Implement BM25 and hybrid search with rank fusion.
  3. Add a cross-encoder reranker and measure recall@k before and after.

Saturday milestone

Project 3: corpus ingested, golden set written, baseline retrieval measured.

W12

The RAG pipeline and its evaluation

Notes this week. RAG: The RAG pipeline end to end · RAG: Advanced RAG: query rewriting, HyDE, multi-hop, GraphRAG, agentic RAG · RAG: Evaluating RAG: recall@k, MRR, nDCG, faithfulness · RAG: RAG failure modes and how to debug them.

By hand

  1. Compute recall@3, MRR and nDCG for three queries by hand.
  2. Design a chunking strategy for a code base and for a legal contract, and justify the difference.

Code

  1. Assemble the full pipeline with citations and streaming.
  2. Build an LLM judge for faithfulness and validate it against 50 of your own labels.
  3. Run the ablation table: chunk size, hybrid on/off, reranker on/off.

Saturday milestone

Project 3 complete with a results table and a failure-mode section in the README.

W13

Prompting, tools and the agent loop

Notes this week. Agents: Prompt engineering that survives production · Agents: Context engineering: what goes in the window · Agents: Structured output and function (tool) calling · Agents: The agent loop and ReAct · Agents: Workflows vs agents: routing, orchestrator–workers, evaluator–optimizer.

By hand

  1. Rewrite a vague prompt into a production prompt with role, constraints, format and examples.
  2. Draw the agent loop and label where failures can happen at each arrow.

Code

  1. Write the agent loop with no framework: model call, tool call, result, repeat, with a step budget.
  2. Add structured output with a schema and retries on validation failure.

Saturday milestone

Project 4: the bare agent completes five of your test tasks.

W14

MCP, memory and security

Notes this week. Agents: Model Context Protocol (MCP) · Agents: Agent memory: short-term, long-term, compaction · Agents: Multi-agent systems: when they help and when they hurt · Agents: Prompt injection and agent security.

By hand

  1. Write three prompt-injection attacks against your own agent and a defence for each.
  2. Design a memory scheme for a week-long task: what is kept, summarized and dropped.

Code

  1. Build an MCP server exposing your tools and connect the agent to it.
  2. Add compaction and a simple long-term memory; run a long task.
  3. Run the injection attacks against the agent and fix what breaks.

Saturday milestone

Project 4: 30 test tasks with a measured success rate.

W15

Evals, cost and serving

Notes this week. Prod: Evals: golden sets, metrics and regression testing · Prod: LLM-as-judge: how to trust a model grading a model · Prod: Hallucination: causes, detection, mitigation · Prod: Guardrails and safety layers · Prod: Cost and latency: caching, batching, streaming, model routing · Prod: Serving LLMs: time to first token, throughput, continuous batching · Prod: Observability and tracing.

By hand

  1. Design an eval set for a feature of your choice: what is measured, how labels are made, what regression means.
  2. Estimate monthly cost for a feature at a given traffic, token count and model price; then halve it on paper.

Code

  1. Add tracing to the agent so every run can be inspected step by step.
  2. Add prompt caching and model routing; report cost and latency before and after.
  3. Add guardrails: input checks, tool permission limits and output validation.

Saturday milestone

Project 4 complete with a cost, latency and success-rate report.

W16

Interview drill

Notes this week. Interview: A framework for LLM system design rounds · Interview: Design: a customer support assistant · Interview: Design: enterprise document search at scale · Interview: Design: a coding assistant · Interview: The ML coding round: attention, BPE and sampling from memory · Interview: Question bank: math, ML and DL fundamentals · Interview: Question bank: applied AI engineering · Interview: Talking about your projects.

By hand

  1. Answer one question-bank section per day out loud, timed, without notes.
  2. Do one full system-design run per day on paper: 45 minutes, one of the three worked designs or a new prompt.
  3. Write and rehearse the two-minute story for each project.

Code

  1. From memory, in under 20 minutes each: scaled dot-product attention, BPE merge step, top-p sampling, logistic regression with gradient descent, k-means.
  2. Solve ten Deep-ML problems under time pressure.

Saturday milestone

All project READMEs polished; resume bullets written with measured numbers.

Problem sets and sources