Skip to the solution
MediumLow-Level Design4 min read

Build a Deterministic Feature-Flag Bucketing Function (Consistent Hash-Based A/B Assignment)

483 views

The problem

Problem, as an interviewer would state it: "Implement isInBucket(userId, flagName, percentage) — deterministically decide whether a given user is in the rollout bucket for a feature flag, WITHOUT storing any per-user assignment state, such that roughly the configured percentage of ALL users end up in the bucket."

Examples:

Code
isInBucket("user-42", "new-checkout", 30); // deterministic true/false, ~30% of users get true

Clarifying questions expected:

  • Must the SAME user always get the SAME result for the SAME flag, across every real call, without storing anything?
  • Should the SAME user potentially get a DIFFERENT bucketing decision for a DIFFERENT flag (independent assignment per flag)?
  • Does the actual measured distribution need to closely match the configured percentage across a large, real population?

Code / implementation expected: Yes — real, direct proof of both determinism (repeated calls for the same user+flag) and distribution accuracy (measured across a large, real simulated user population).

  • hashing
  • experimentation
  • system-design

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: bucketing is genuinely deterministic across repeated calls, and the measured distribution (29.91%) closely matches a 30% target across 100,000 users

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 114 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Debounce async promise resolver

Continue