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

Implement rejection sampling

Implement rejection sampling to transform one discrete random generator into another target distribution, then explain why the output distribution is correct and what affects efficiency.

mediumml codingEvidence 77/1001 source reportLinkedIn

The 60-second answer

Choose an easy proposal distribution q(x) and a constant M such that target(x) ≤ M·q(x). Sample x~q and u~Uniform(0,1); accept when u ≤ target(x)/(M·q(x)), otherwise retry.

Build the answer in this order

1
State tensor contract

Choose an easy proposal distribution q(x) and a constant M such that target(x) ≤ M·q(x).

2
Implement the mechanism

Sample x~q and u~Uniform(0,1); accept when u ≤ target(x)/(M·q(x)), otherwise retry.

3
Check numerics + gradients

For discrete RNG transformations, map source outcomes into equiprobable states, reject overflow, then remap accepted states.

4
Test shapes and edge cases

Quantify acceptance probability/expected retries and explicitly avoid modulo bias.

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

  • Compare rejection sampling with inverse CDF, alias sampling, and importance sampling.
  • Choose the method based on target shape, update frequency, and sample volume.

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.