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
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.
For discrete RNG transformations, map source outcomes into equiprobable states, reject overflow, then remap accepted states.
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.
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
Likely follow-up questions
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.