Monte Carlo & Simulation interview question

Probability & MathMonte Carlo & SimulationDecoding & Samplinghard

An LLM routing service samples one of three candidate reply types from logits with the snippet below, at temperature T = 0.5. Up to simulation noise, what does it print?

python
import numpy as np
rng = np.random.default_rng(42)
logits = np.array([2.0, 1.0, 0.0])   # scores of 3 candidate reply types

def sample(logits, T):
    u = rng.random(logits.shape)
    g = -np.log(-np.log(u))           # standard Gumbel noise
    return np.argmax(logits / T + g)

draws = [sample(logits, T=0.5) for _ in range(200_000)]
print(np.bincount(draws, minlength=3) / len(draws))
  1. AAbout [0.665 0.245 0.090], i.e. softmax(logits): dividing every logit by T does not change which logit is largest, so the temperature has no effect on the distribution of the argmax.
  2. BAbout [0.867 0.117 0.016], i.e. softmax(logits / T) with logits / T = (4, 2, 0): the argmax of logits plus Gumbel noise is an exact softmax sample, and a T below 1 sharpens it.
  3. CAbout [0.506 0.307 0.186], i.e. softmax(logits · T): a temperature below 1 adds randomness by shrinking the logit gaps relative to the Gumbel noise, flattening the distribution.
  4. DExactly [1. 0. 0.]: after dividing by T the top logit leads by at least 2, and standard Gumbel noise stays within about ±1 of zero, so it can never overturn the largest logit.
Show the answer and explanation

Correct answer: B, About [0.867 0.117 0.016], i.e. softmax(logits / T) with logits / T = (4, 2, 0): the argmax of logits plus Gumbel noise is an exact softmax sample, and a T below 1 sharpens it.

Explanation Monte Carlo & Simulation

Gumbel-max trick: if G₁, …, Gₖ are independent standard Gumbel variables (G = −log(−log U), U uniform), then argmaxᵢ (zᵢ + Gᵢ) equals i with probability exactly exp(zᵢ) / Σⱼ exp(zⱼ), i.e. softmax(z). It is an exact sampler, not an approximation, and needs no normalising constant.

The snippet uses z = logits / T = (4, 2, 0), so it samples from softmax(4, 2, 0) = (0.867, 0.117, 0.016); one run printed about [0.869 0.116 0.016]. Dividing by T < 1 widens the gaps relative to the noise, so the distribution sharpens towards greedy decoding; T > 1 flattens it, and argmax(z/T + G) is equivalent to argmax(z + T·G).

TDistribution sampled
2softmax(1, 0.5, 0) = (0.506, 0.307, 0.186)
1softmax(2, 1, 0) = (0.665, 0.245, 0.090)
0.5softmax(4, 2, 0) = (0.867, 0.117, 0.016)
→ 0always index 0 (greedy)

Why the distractors fail: scaling the logits alone preserves the argmax, but the noise is not scaled, so the balance between signal and noise changes. Multiplying by T reverses the direction of the temperature effect. Standard Gumbel noise is unbounded, with mean ≈ 0.577 and SD π/√6 ≈ 1.28, so a lead of 2 is overturned regularly.

Interview tip: the Gumbel-max view explains why sampling from logits can be done with one argmax per draw, and adding Gumbel noise and taking the top-k indices draws k items without replacement (Gumbel-top-k).

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Probability & Math questions

  1. A labelling vendor's error rate is either 2% (prior 0.7) or 10% (prior 0.3). You audit two batches of 10 independent labels: the first has 1 error, the second…
  2. A bank fine-tunes a multilingual support assistant on a corpus that is 90% English and 10% Turkish by tokens. Following common multilingual practice, it…
  3. A churn network maps four features x ∈ ℝ⁴ through a = W₁x + b₁ (W₁ is 3 × 4), h = ReLU(a) and z = W₂h + b₂ (W₂ is 2 × 3). Writing the Jacobian with entry (i,…
  4. Each week a loyalty-app user is either Engaged or Idle. An Engaged user becomes Idle the next week with probability 0.10, and an Idle user becomes Engaged…
  5. In RLHF you need the gradient of J(θ) = E[R(y)], where y is a token sequence sampled from the policy LLM π_θ and R is a reward model's score treated as a…