Assign users to an arbitrary two-arm traffic split with a salted hash

Quick Overview

Write a deterministic, hash-based function that assigns users to control or treatment for any whole-percentage traffic split. It tests salted hashing, bucket boundaries at the 0 and 100 percent extremes, and how experiment platforms keep a user's assignment stable across visits.

Assign users to an arbitrary two-arm traffic split with a salted hash

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `assign_split` in Python so that it deterministically assigns a user to one of two arms. `control_pct` (an int in `[0, 100]`) is the percentage of traffic assigned to control. ```python def assign_split(user_id: str, salt: str, control_pct: int) -> str: ... ``` Requirements: - `bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100` - Return `"control"` if `bucket < control_pct`, else `"treatment"`. - Return a plain Python `str`. Example: ```python assign_split("user_42", "exp-checkout", 30) # same arm on every call assign_split("anyone", "exp", 0) # -> "treatment" for every user assign_split("anyone", "exp", 100) # -> "control" for every user ``` ```hint Check the extremes Work out what your comparison does when `control_pct` is 0 and when it is 100, and make sure every bucket belongs to exactly one arm. ``` ### What a Strong Answer Covers - Building the salted hash key and reducing the digest to a bucket in [0, 100) - The boundary comparison and why it gives exactly the configured share of buckets to control - Behavior at `control_pct` of 0 and 100, and what to do with values outside the range - Why the assignment is stable across calls and close to uniform across users ### Follow-up Questions - How would you extend this to more than two arms with weighted splits? - Why include a `salt` instead of hashing only the `user_id`? - If you ramp treatment from 10% to 50% by lowering `control_pct`, which users change arms? - How would you support splits finer than 1%?

Overview: Write a deterministic, hash-based function that assigns users to control or treatment for any whole-percentage traffic split. It tests salted hashing, bucket boundaries at the 0 and 100 percent extremes, and how experiment platforms keep a user's assignment stable across visits.

Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement assign_split in Python so that it deterministically assigns a user to one of two arms. control_pct (an int in [0, 100]) is the percentage of traffic assigned to control.

def assign_split(user_id: str, salt: str, control_pct: int) -> str:
    ...

Requirements:

  • bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100
  • Return "control" if bucket < control_pct , else "treatment" .
  • Return a plain Python str .

Example:

assign_split("user_42", "exp-checkout", 30)  # same arm on every call

assign_split("anyone", "exp", 0)    # -> "treatment" for every user
assign_split("anyone", "exp", 100)  # -> "control" for every user

What a Strong Answer Covers Guidance

  • Building the salted hash key and reducing the digest to a bucket in [0, 100)
  • The boundary comparison and why it gives exactly the configured share of buckets to control
  • Behavior at control_pct of 0 and 100, and what to do with values outside the range
  • Why the assignment is stable across calls and close to uniform across users

Follow-up Questions Guidance

  • How would you extend this to more than two arms with weighted splits?
  • Why include a salt instead of hashing only the user_id ?
  • If you ramp treatment from 10% to 50% by lowering control_pct , which users change arms?
  • How would you support splits finer than 1%?