KatInterview

Flag Rule Resolver with Hash-Bucketed Rollouts

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designsimulationhashingrules-engine

Last updated

Simulate a feature-flag service: match attribute rules, gate by a SHA-256 percentage bucket, and resolve conflicts so any matching "on" rule wins.

Flag Rule Resolver

You are building the core of a small feature-flag service. It is given a set of flag definitions, then answers a batch of queries asking whether a flag is on or off for a particular user.

Implement a single function that takes the flag definitions plus a list of commands and returns one result per command.

Data model

User: a dictionary with an "id" string and any number of other attributes (for example "country": "US" or "age": 28).

Flag: a dictionary with:

  • key: the unique name of the flag.
  • default_value: the boolean used when no rule applies.
  • rules: a list of rules, which may be empty.

Rule: a dictionary with:

  • conditions: a list of {"attribute": str, "operator": str, "value": Any} objects. A rule's conditions are satisfied only if every one of them holds. An empty list is satisfied by every user.
  • rollout_percentage (optional): an integer from 0 to 100. If it is present, the user must also land inside the rollout for the rule to apply. If it is absent, there is no rollout restriction.
  • serve: the boolean this rule yields when it applies.

Condition operators

Let actual be the user's value for the condition's attribute, and value the value stored in the condition.

  • EQUALS: holds when actual == value.
  • IN: holds when actual is a member of value (a list).
  • GTE: holds when actual >= value.
  • If the user has no such attribute, or its value is None, the condition does not hold, whatever the operator.
  • Any other operator name never holds.

Rollout bucketing

For a rule that has rollout_percentage, compute the user's bucket for that flag:

  1. Build the ASCII string "{flag_key}:{user_id}".
  2. Take its SHA-256 digest as a hexadecimal string and read it as a base-16 integer.
  3. The bucket is that integer modulo 100.

The user is inside the rollout when bucket < rollout_percentage. A percentage of 0 therefore admits nobody and 100 admits everybody. The rollout check is only relevant for a rule whose conditions are already satisfied.

Resolving a flag for a user

A rule applies when all of its conditions hold and, if it has a rollout percentage, the user is inside the rollout.

  • If at least one applying rule has serve equal to True, the flag is True.
  • Otherwise, if at least one rule applies (so all applying rules serve False), the flag is False, even when default_value is True.
  • Otherwise (no rule applies, including when rules is empty), the result is the flag's default_value.
  • If the requested flag key does not exist, the result is False.

Commands

commands and command_args have equal length, and command_args[i] holds the arguments for commands[i]. Process them in order. Each command adds exactly one element to the output list.

  • "evaluate" with arguments [flag_key, user]: output the boolean for that flag and user.
  • "evaluate_all" with arguments [user]: output a dictionary mapping every flag key to its boolean for that user, with keys in the same order as the flags appear in flag_defs.

The inputs must not be modified.

Constraints

  • Flag keys are unique, non-empty ASCII strings. User IDs are non-empty ASCII strings.
  • rollout_percentage, when present, is an integer in [0, 100].
  • Every command name is either "evaluate" or "evaluate_all".
  • Looking up a flag should take constant time, and the function should stay efficient with thousands of commands.

Examples

Example 1

Input: resolve_flag_states([{'key': 'beta_dashboard', 'default_value': False, 'rules': [{'conditions': [{'attribute': 'country', 'operator': 'IN', 'value': ['US', 'CA']}], 'rollout_percentage': 50, 'serve': True}, {'conditions': [{'attribute': 'beta_tester', 'operator': 'EQUALS', 'value': True}], 'serve': True}]}], ['evaluate', 'evaluate', 'evaluate_all'], [['beta_dashboard', {'id': 'u123', 'country': 'US', 'beta_tester': False}], ['beta_dashboard', {'id': 'u456', 'country': 'UK', 'beta_tester': True}], [{'id': 'u789', 'country': 'CA', 'beta_tester': False}]])

Output: [False, True, {'beta_dashboard': False}]

Hints

Hint 1

Index the flags in a dictionary by key once, up front, so each query is a constant-time lookup instead of a scan.

Hint 2

Check a rule's conditions first and compute the SHA-256 bucket only if they all hold. Treat a missing attribute or a None value as a failed condition before applying any operator.

Hint 3

Track whether any rule applied. Return True immediately when an applying rule serves True. At the end, return False if some rule applied, otherwise default_value.