Predict the output
Is the 10% group inside the 25% group, and close to 1,000 users?
import hashlib
def in_rollout(user_id: str, flag: str, percent: int) -> bool:
h = hashlib.sha256(f"{flag}:{user_id}".encode()).digest()
bucket = int.from_bytes(h[:4], "big") % 100
return bucket < percent
users = [f"u{i}" for i in range(10_000)]
on_10 = {u for u in users if in_rollout(u, "new-checkout", 10)}
on_25 = {u for u in users if in_rollout(u, "new-checkout", 25)}
print(on_10 <= on_25, 900 < len(on_10) < 1100)Hint 1 of 1
If bucket < 10, is bucket < 25?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A True True
Correct. Buckets are stable, so raising the percent only adds users.
Hashing the flag and user id gives each user a fixed bucket from 0 to 99. Going from 10% to 25% only adds buckets 10 to 24, and a good hash puts about 1,000 of 10,000 users below 10.
Why not the others?
- B
False True. A user in bucket < 10 is also < 25. - C
True False. SHA-256 spreads 10,000 users evenly enough. - D Different every run. The hash is deterministic.