Assign experiment units to 100 buckets with a salted MD5 hash

Quick Overview

Implement a Python function that maps an experiment unit to one of 100 buckets with a salted MD5 hash, so assignment is stateless and stable across services. It tests deterministic hashing, string format contracts, salting for independent experiments, and how buckets map to traffic splits.

Assign experiment units to 100 buckets with a salted MD5 hash

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `def hash_bucket(unit_id: str, salt: str) -> int` that statelessly maps an experiment unit to one of 100 buckets using a salted MD5 hash. Requirements: - Build `f"{salt}:{unit_id}"` (salt first, a single ASCII colon, no spaces) and encode it as UTF-8. - Take the `hashlib.md5` hex digest and parse the full 32-character digest with `int(hexdigest, 16)`, with no truncation. - Return that integer `% 100` as a plain Python `int` in `[0, 99]`. - Keep the function pure and deterministic: no file, network or RNG access. Example: ```python hash_bucket("user-123", "exp_signup_v2") # builds "exp_signup_v2:user-123", MD5, int(hexdigest, 16) % 100 # -> a stable int in [0, 99] ``` Calling it again with the same arguments, in any process or on any machine, must return the same bucket. ```hint Bytes in, bytes out `hashlib` hashes bytes, so the key string has to be encoded first, and every service that computes buckets has to encode it the same way. ``` ### What a Strong Answer Covers - Building the key string exactly as specified and encoding it as UTF-8 before hashing. - Parsing the full hex digest as one integer and reducing it modulo 100 to a plain `int`. - Why the function is deterministic and stateless, and why Python's built-in `hash()` would break that. - What the salt does for independence between experiments. ### Follow-up Questions - Why salt first, and why does the exact string format matter? - How would you support unequal split ratios (for example 90/10) instead of even buckets? - How would you check in production that units are spread evenly across buckets?

Overview: Implement a Python function that maps an experiment unit to one of 100 buckets with a salted MD5 hash, so assignment is stateless and stable across services. It tests deterministic hashing, string format contracts, salting for independent experiments, and how buckets map to traffic splits.

General logo
General
Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement def hash_bucket(unit_id: str, salt: str) -> int that statelessly maps an experiment unit to one of 100 buckets using a salted MD5 hash.

Requirements:

  • Build f"{salt}:{unit_id}" (salt first, a single ASCII colon, no spaces) and encode it as UTF-8.
  • Take the hashlib.md5 hex digest and parse the full 32-character digest with int(hexdigest, 16) , with no truncation.
  • Return that integer % 100 as a plain Python int in [0, 99] .
  • Keep the function pure and deterministic: no file, network or RNG access.

Example:

hash_bucket("user-123", "exp_signup_v2")
# builds "exp_signup_v2:user-123", MD5, int(hexdigest, 16) % 100
# -> a stable int in [0, 99]

Calling it again with the same arguments, in any process or on any machine, must return the same bucket.

What a Strong Answer Covers Guidance

  • Building the key string exactly as specified and encoding it as UTF-8 before hashing.
  • Parsing the full hex digest as one integer and reducing it modulo 100 to a plain int .
  • Why the function is deterministic and stateless, and why Python's built-in hash() would break that.
  • What the salt does for independence between experiments.

Follow-up Questions Guidance

  • Why salt first, and why does the exact string format matter?
  • How would you support unequal split ratios (for example 90/10) instead of even buckets?
  • How would you check in production that units are spread evenly across buckets?