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

Sample From a Non-Uniform RNG

Transform a limited or non-uniform random generator into samples from a target distribution without bias.

hardml codingEvidence 77/1001 source reportLinkedIn

The 60-second answer

Characterize the source generator’s outcome probabilities before transforming it; do not assume modulo arithmetic preserves uniformity. Combine source draws to create equiprobable states, reject overflow states, and map the accepted range into the target outcomes.

Build the answer in this order

1
State tensor contract

Characterize the source generator’s outcome probabilities before transforming it; do not assume modulo arithmetic preserves uniformity.

2
Implement the mechanism

Combine source draws to create equiprobable states, reject overflow states, and map the accepted range into the target outcomes.

3
Check numerics + gradients

Prove unbiasedness by showing each target receives the same number/probability mass of accepted source states.

4
Test shapes and edge cases

Quantify acceptance probability and expected number of source calls, then test empirical frequencies.

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 optimize the state-space size to minimize rejection while keeping the proof simple.
  • Explain when exact unbiased sampling is worth extra calls versus an approximate method.

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.