← Snowflake Interview Insights

Snowflake·Data Scientist·Technical Phone Screen·Senior

Senior
May 2026

Summary

Snowflake data scientist interview that was basically a probability and randomness gauntlet. Three connected problems all building on each other, and by part C I was definitely improvising. Tough but fair, the kind of interview where you can tell they actually care about whether you understand the math.

Questions Asked (3)

Q1

You have a function that returns a uniform random integer from 1 to 5. Implement a function that returns a uniform random integer from 1 to 7 using only that function. Your solution must be unbiased, always terminate, and close to optimal in how many calls it makes. Prove it's uniform and compute the exact expected number of calls.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew the rejection sampling trick from somewhere in my past but proving it rigorously on the spot was harder than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use rejection sampling with a base-5 representation: generate enough random digits to cover a range that is a multiple of 7, then reject values outside that range. To minimize calls, generate two digits at a time (25 outcomes), accept 21 values (3 full cycles of 7), and reject the remaining 4. This yields an expected number of calls of 50/21 ≈ 2.38, which is close to the information-theoretic lower bound of log_5(7) ≈ 1.21.

Pro tip: Mention that you can reduce the rejection rate by combining leftover entropy from rejected outcomes with new calls, but for simplicity and near-optimality, the two-digit method is sufficient. Also, explicitly compute the expected calls and compare to the lower bound to show awareness of optimality.

1. Understand the problem and constraints

Clarify that the given function returns uniform integers 1-5, and we need a uniform integer 1-7. The solution must be unbiased, always terminate, and be close to optimal in calls.

2. Choose a rejection sampling strategy

Generate random numbers in a larger range using multiple calls, then map to 1-7 by rejection. For efficiency, use two calls to get 25 equally likely outcomes (5x5 grid).

3. Map outcomes to 1-7 and reject

Assign 21 of the 25 outcomes to the numbers 1-7 (3 outcomes per number). Reject the remaining 4 outcomes and retry. This ensures each number has probability 3/25 per trial, and conditional on acceptance, probability 1/7.

4. Prove uniformity and termination

Show that each accepted outcome is equally likely because the 21 accepted outcomes are symmetric. Termination is guaranteed because each trial has acceptance probability 21/25 > 0, so the number of trials is geometric and finite almost surely.

5. Compute expected number of calls

Each trial uses 2 calls. The expected number of trials is 1/(21/25) = 25/21. Thus expected calls = 2 * (25/21) = 50/21 ≈ 2.38. Compare to the lower bound log_5(7) ≈ 1.21 to show near-optimality.

Key Points to Mention

  • Rejection sampling with a 5x5 grid (two calls per trial) to generate 25 equally likely outcomes.
  • Mapping 21 outcomes to 1-7 (3 per number) and rejecting 4 outcomes.
  • Proof of uniformity: each number has equal probability 3/25 per trial, so conditional on acceptance, probability 1/7.
  • Guaranteed termination: acceptance probability 21/25 > 0, so the process terminates with probability 1.
  • Expected calls: 2 * (25/21) = 50/21 ≈ 2.38, which is close to the information-theoretic lower bound of log_5(7) ≈ 1.21.
  • Possible optimization: reuse rejected outcomes to reduce calls further, but the simple method is near-optimal.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Generalize the previous problem: given a function returning a uniform integer from 1 to M, implement one that returns a uniform integer from 1 to N for any positive integers M and N. Derive an upper bound on the expected number of calls in terms of M and N, and discuss how the approach changes when N is much larger than M versus when M is much larger than N.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where it got genuinely interesting and also where I started to feel the pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining rejection sampling: generate a random number in [1, M^k] by calling the M-sided generator k times, and accept if it falls within the largest multiple of N. Derive the expected number of calls as k / p where p = floor(M^k / N) * N / M^k, and choose k = ceil(log_M N) to minimize calls. Then discuss the two regimes: when N >> M, k is large and acceptance probability is high, so expected calls are O(log_M N); when M >> N, k=1 and expected calls are O(M/N).

Pro tip: Mention that you can optimize by using the leftover randomness from rejected samples (e.g., recycling the remainder) to reduce calls, but note the trade-off in complexity. This shows you think about efficiency beyond the basic algorithm.

1. Understand the problem and constraints

Restate the problem: we have a uniform generator for 1..M and need a uniform generator for 1..N. Note that M and N are positive integers, and we want to minimize the expected number of calls to the M-generator.

2. Describe the rejection sampling approach

Explain that we can simulate a uniform generator for 1..M^k by calling the M-generator k times, treating the results as digits in base M. Then accept the result if it is ≤ the largest multiple of N within M^k, otherwise reject and retry.

3. Derive the expected number of calls

Let k = ceil(log_M N). The acceptance probability p = floor(M^k / N) * N / M^k. The expected number of calls is k / p. Show that p > 1/2, so expected calls < 2k = O(log_M N). Also consider the case when M > N: then k=1 and expected calls = M / (floor(M/N)*N) ≈ M/(M - (M mod N)) which is O(M/N) when M is much larger than N.

4. Analyze the two regimes

When N >> M, k is large, but acceptance probability is high (close to 1), so expected calls are about log_M N. When M >> N, k=1, and acceptance probability is N/M, so expected calls are about M/N. Discuss how the algorithm adapts naturally.

5. Discuss optimizations and trade-offs

Mention that you can reduce calls by using the leftover randomness from rejected samples (e.g., if you generate a number in [1, M^k] and reject, you can use the remainder modulo N to generate additional randomness). Also note that for very large N, you might use a different approach like generating multiple N-sided numbers from one M^k sample.

Key Points to Mention

  • Rejection sampling: generate in [1, M^k] and accept if within a multiple of N.
  • Choice of k = ceil(log_M N) to minimize expected calls.
  • Expected calls = k / p, where p = floor(M^k / N) * N / M^k > 1/2, so O(log_M N).
  • When M >> N, k=1 and expected calls O(M/N).
  • Optimization: recycle rejected randomness to reduce calls.
  • Trade-off: simplicity vs. efficiency; recycling adds complexity but can reduce calls.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Given a biased coin that flips heads with some unknown probability p, construct a fair coin flip. Prove it works and find the expected number of flips as a function of p. Then extend this to sampling uniformly from 1 to K using only the biased coin.

Algorithms & Data StructuresA/B Testing & Experimentation
Author's notes

Von Neumann's trick, flip twice, if you get HT output 0, if TH output 1, otherwise repeat.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the von Neumann extractor: flip the biased coin twice, use HT as heads and TH as tails, and discard HH/TT. Prove fairness by showing P(HT) = P(TH) = p(1-p). Then compute the expected number of flips as 1/[2p(1-p)] by modeling the number of trials until a non-discarded pair appears. For extension to K, use rejection sampling: generate a random integer in [0, 2^n - 1] by flipping n times (mapping heads to 1 and tails to 0), and accept if the value is less than K; otherwise, discard and repeat. Choose n = ceil(log2 K) and prove uniformity by symmetry of the binary representation.

Pro tip: When discussing expected flips, emphasize that the method is optimal in terms of entropy (each fair bit costs 1/H(p) flips on average) and mention that for K, you can optimize by using the largest multiple of K less than 2^n to reduce rejection probability.

1. Understand the problem and constraints

Clarify that the coin is biased with unknown p, and we need a fair coin flip without knowing p. Also, we need to extend to uniform sampling from 1 to K using only this biased coin.

2. Construct a fair coin flip using von Neumann's method

Flip the coin twice. If the result is HT, output heads; if TH, output tails; if HH or TT, discard and repeat. This yields a fair outcome because P(HT) = P(TH) = p(1-p).

3. Prove fairness and compute expected flips

Show that conditional on a non-discarded pair, the probability of HT is 1/2. The number of pairs until success follows a geometric distribution with success probability 2p(1-p), so expected flips = 2 / [2p(1-p)] = 1/[p(1-p)]? Wait, careful: each trial is two flips, so expected flips = 2 / (2p(1-p)) = 1/(p(1-p)). Actually, success probability per pair is 2p(1-p), so expected pairs = 1/(2p(1-p)), expected flips = 2 * that = 1/(p(1-p)). But note: if p=0.5, expected flips = 2, which matches. However, the correct expected flips for von Neumann is 1/(p(1-p))? Let's check: For p=0.5, 1/(0.25)=4, but actually von Neumann with fair coin: each pair has success prob 0.5, so expected pairs = 2, expected flips = 4. Yes, so expected flips = 1/(p(1-p))? Wait, 1/(0.25)=4, correct. So expected flips = 1/(p(1-p)). But earlier I wrote 1/[2p(1-p)] for expected pairs? Let's recalc: success probability per pair = 2p(1-p). Expected number of pairs = 1/(2p(1-p)). Each pair is 2 flips, so expected flips = 2/(2p(1-p)) = 1/(p(1-p)). Yes. So expected flips = 1/(p(1-p)). But note: if p=0.5, expected flips = 4, which is correct for von Neumann. So the formula is 1/(p(1-p)). However, some sources say expected flips = 1/(2p(1-p))? That would be for expected pairs? Actually, expected flips = 1/(p(1-p)). Let's verify: For p=0.5, expected flips = 4. Yes. So the expected number of flips is 1/(p(1-p)). But wait, the question asks: 'find the expected number of flips as a function of p.' So answer: E = 1/(p(1-p)). But note: if p=0 or 1, infinite. So that's correct.

4. Extend to uniform sampling from 1 to K

Use rejection sampling: let n = ceil(log2 K). Generate a random integer X in [0, 2^n - 1] by flipping n times (heads=1, tails=0). If X < K, output X+1; else discard and repeat. This yields uniform distribution over 1..K because each n-bit string is equally likely (by symmetry of the fair bits generated from the biased coin).

5. Analyze efficiency and optimizations

The expected number of fair bits needed is n * (2^n / K) (since acceptance probability is K/2^n). Each fair bit costs 1/(p(1-p)) flips on average, so total expected flips = n * (2^n / K) * 1/(p(1-p)). Mention that you can reduce rejection by using the largest multiple of K less than 2^n, or by using arithmetic coding for optimal efficiency.

Key Points to Mention

  • Von Neumann extractor: using pairs HT and TH to produce fair bits.
  • Proof of fairness: P(HT) = P(TH) = p(1-p), so conditional probability is 1/2.
  • Expected flips: geometric distribution with success probability 2p(1-p) per pair, leading to E = 1/(p(1-p)) flips.
  • Extension to K: rejection sampling with n = ceil(log2 K) bits, accepting if value < K.
  • Uniformity proof: each n-bit string is equally likely because the fair bits are independent and unbiased.
  • Efficiency: expected flips = n * (2^n / K) * 1/(p(1-p)); can optimize by using largest multiple of K or arithmetic coding.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.