FreeExam

Coding

Write the pieces an ML engineer codes by hand, such as stable softmax, ranking metrics, attention, sampling, and data splits, in Python in the editor.

These prompts are original practice. They are not questions from a named employer or a live hiring loop.

  1. 1.Logits from a classifier head can be large. Write softmax(logits) so it returns probabilities that sum to 1 without overflowing.

  2. 2.Write log_sum_exp(values), the log of the sum of exponentials, so it stays accurate for values like 1000 and -1000.

  3. 3.A binary classifier outputs raw logits. Write bce_with_logits(logits, labels) that returns the mean binary cross-entropy, computed from the logits directly so large logits stay finite. Labels are 0 or 1.

  4. 4.Write cross_entropy(logits, labels) for a batch: logits has one row of class scores per example, and labels holds each correct class index. Return the mean loss, stable for large logits.

  5. 5.A fraud model flags transactions. Write precision_recall_f1(y_true, y_pred) for labels of 0 and 1, where 1 is fraud. Return {"precision", "recall", "f1"}. When a ratio has a zero denominator, use 0.0.

  6. 6.Write confusion_matrix(y_true, y_pred, num_classes). Row i counts examples whose true class is i, and column j counts those predicted as j.

  7. 7.Write roc_auc(labels, scores): the chance that a random positive gets a higher score than a random negative, with ties counting half. Labels are 0 or 1. Return None when only one class is present.

  8. 8.A search ranker returns results with graded relevance in ranked order. Write ndcg_at_k(relevances, k) using gain 2^rel − 1 and discount log2(position + 1), with positions from 1. Return 0.0 when no result is relevant.

  9. 9.Write mean_reciprocal_rank(queries). Each query is {"ranked": [ids], "relevant": [ids]}. Score a query as 1 / the position of its first relevant id, counting from 1, or 0 when none appears. Return the mean, or 0.0 when there are no queries.

  10. 10.Boxes are [x1, y1, x2, y2] with x1 < x2 and y1 < y2. Write iou(a, b), the intersection area divided by the union area. Boxes that only touch or do not overlap score 0.0.

  11. 11.Write nms(boxes, scores, threshold) for an object detector. Visit boxes from highest score to lowest, ties going to the lower index. Keep a box unless its IoU with an already kept box is greater than threshold. Return the kept indexes in the order they were kept. Boxes are [x1, y1, x2, y2].

  12. 12.Write kmeans_step(points, centroids) for one round of k-means. Assign each point to its nearest centroid by Euclidean distance, ties going to the lower index, then move each centroid to the mean of its points. A centroid with no points stays where it is.

  13. 13.Write knn_classify(points, labels, query, k). Take the k training points nearest to query by Euclidean distance, distance ties going to the lower index, and return the most common label among them. If labels tie on votes, return the tied label whose nearest point is closest.

  14. 14.Write cosine_top_k(query, docs, k) for a small embedding search. Return the indexes of the k docs with the highest cosine similarity to query, highest first, ties going to the lower index. A zero vector has similarity 0.0.

  15. 15.Write linear_gd_step(w, b, xs, ys, lr) for one batch gradient descent step on y ≈ w·x + b with mean squared error. Return [new_w, new_b].

  16. 16.Write attention(q, k, v) for single-head scaled dot-product attention on lists. q is n × d, k is m × d, and v is m × d_v. Score query i against key j as dot(q[i], k[j]) / sqrt(d), softmax each row of scores, and return the weighted sums of v as an n × d_v list.

  17. 17.Write causal_attention_weights(scores) for a decoder. scores is an n × n list where row i holds query i against every key j. Block every key j > i so a token cannot attend to later tokens, then softmax each row. Blocked positions get weight 0.0.

  18. 18.Write layer_norm(x, eps) for one feature vector: subtract the mean, then divide by sqrt(variance + eps), where variance is the mean squared deviation (divide by n, not n − 1).

  19. 19.Write top_p_tokens(probs, p) for nucleus sampling. Sort token indexes by probability, highest first, ties going to the lower index, and keep the shortest prefix whose total probability reaches at least p. Return the kept indexes in that order.

  20. 20.Write beam_search(start, log_probs, steps, width). log_probs[token] maps each next token to its log probability, and every token has entries. Grow sequences from [start] for steps steps, keep the width best by total log probability, ties going to the alphabetically smaller sequence, and return the best sequence.

  21. 21.Write bpe_merges(word_counts, num_merges) to train byte-pair encoding. Start each word as single characters. Each round, count adjacent pairs weighted by word count, merge the most frequent pair everywhere, ties going to the smaller (left, right) pair. Return merges as "left right" strings, and stop early when no pairs remain.

  22. 22.Write build_vocab(texts, min_count, specials) for a word-level tokenizer. Lowercase each text and split on whitespace. Give the special tokens ids 0, 1, 2, … in order, then add words seen at least min_count times, most frequent first, ties alphabetical. Return a dict of token to id.

  23. 23.Write pad_batch(sequences, pad_id) to batch token ids for a model. Right-pad every sequence to the longest length with pad_id, and build an attention mask with 1 for real tokens and 0 for padding. Return {"input_ids": ..., "attention_mask": ...}.

  24. 24.Write chunk_tokens(tokens, size, overlap) to split a document for retrieval. Each chunk holds up to size tokens and starts size − overlap after the previous one. Stop once a chunk reaches the end of the list. 0 ≤ overlap < size.

  25. 25.Write k_fold_splits(n, k) for cross-validation without shuffling. Split indexes 0 to n − 1 into k contiguous folds; when n does not divide evenly, the first n % k folds get one extra index. Return one [train, validation] pair per fold.

  26. 26.Write standard_scale(train, test). Both are lists of rows with the same columns. Compute each column's mean and standard deviation (divide by n) on train only, then return test scaled as (value − mean) / std. A column with std 0 scales to 0.0.

  27. 27.Write impute_median(values) for a numeric feature where missing entries are None. Replace each None with the median of the present values, averaging the two middle values for an even count. If every value is missing, use 0.0.

  28. 28.Write warmup_cosine_lr(step, warmup, total, peak, floor). For step < warmup, rise linearly as peak · (step + 1) / warmup. After that, decay from peak to floor along half a cosine over the remaining total − warmup steps, and hold floor once step passes total.

  29. 29.Write clip_by_global_norm(grads, max_norm). grads is a list of gradient lists, one per parameter. The global norm is the square root of the sum of every squared entry. If it exceeds max_norm, scale every entry by max_norm / norm. Return [clipped_grads, norm], with norm measured before clipping.

  30. 30.Write early_stop(val_losses, patience, min_delta). An epoch improves when its loss is below the best loss so far minus min_delta. Stop at the first epoch that is patience epochs past the last improvement. Return {"best_epoch", "stop_epoch"}; stop_epoch is the last index if training never stops early.

  31. 31.Write psi(expected, actual) for drift monitoring. Both are counts per bin over the same bins. Turn each into proportions, raise any proportion below 0.0001 to 0.0001 so empty bins stay finite, and return the sum over bins of (actual − expected) · ln(actual / expected).

  32. 32.Write word_error_rate(reference, hypothesis) for a speech model. Split both on whitespace and return the word-level edit distance (substitutions, insertions, and deletions) divided by the number of reference words. The reference is never empty.