CrackML by @ml.with.umang
Interview questions / ML Coding & PyTorch
ML Coding & PyTorch interview question

Implement a Categorical Sampler

Implement sampling from a categorical probability vector without using a built-in categorical sampler.

mediumml codingEvidence 66/1001 source reportGoogle

The 60-second answer

Validate nonnegative weights and positive total mass; normalize only if the interface requires probabilities. Build cumulative mass, draw a uniform sample in [0,total), and binary-search the first cumulative boundary above the sample.

Build the answer in this order

1
State tensor contract

Validate nonnegative weights and positive total mass; normalize only if the interface requires probabilities.

2
Implement the mechanism

Build cumulative mass, draw a uniform sample in [0,total), and binary-search the first cumulative boundary above the sample.

3
Check numerics + gradients

Define exact boundary behavior to avoid off-by-one errors with zero-weight categories.

4
Test shapes and edge cases

Test deterministic edge cases plus empirical frequencies over many draws.

A useful interview mental model

This is the shape of a strong answer—not a script to memorize.

01Shapes
02Forward pass
03Loss / grads
04Numerics
05Tests

Senior-level signal

  • Senior answers offer the alias method for repeated draws and a Fenwick tree for frequently updated weights.
  • Discuss reproducible RNG injection rather than relying on global random state.

What the interviewer is really testing

Tensor fluency, shape reasoning, numerics, gradients, batching, device awareness, and the ability to debug—not API memorization.

Likely follow-up questions

What are the tensor shapes at each step?
Where could numerical instability or silent broadcasting appear?
How would you verify gradients and batched behavior?

Common weak-answer patterns

  • Ignoring shape, dtype, device, masking, or broadcasting assumptions.
  • Using a framework call without explaining the underlying operation.
  • Skipping gradient, numerical-stability, and batching checks.