AlgoMaster Logo
AlgoMasterEvaluate Ordered Feature-Flag Rulesmedium

Evaluate Ordered Feature-Flag Rules

medium

Feature flags often combine explicit overrides, audience targeting, percentage rollouts, and a default. When rules overlap, their order is part of the configuration contract.

Design a FeatureFlagRuleEvaluator:

  • The constructor receives aligned ruleTypes, ruleValues, and decisions arrays.
  • int matchedRule(String userId, String region, String plan) returns the first matching rule index, or -1.
  • String evaluate(String userId, String region, String plan) returns that rule's "on" or "off" decision. No match defaults to "off".

Rules match as follows:

  • "USER": ruleValue == userId
  • "REGION": ruleValue == region
  • "PLAN": ruleValue == plan
  • "PERCENT": the user's stable bucket is below the integer percentage in ruleValue
  • "DEFAULT": always matches

For percentage rules, use the unsigned 32-bit polynomial hash from the sticky-bucketing exercise and bucket = hash % 100. Scan rules strictly in input order.

Example 1:

Input:

Output:

Explanation: Alice's explicit off rule wins before the India rule. Bob has no user override, so the India rule enables the flag.

Example 2:

Input:

Output:

Explanation: Eve hashes to bucket 20, while Frank hashes to bucket 50.

Constraints

  • 0 <= ruleTypes.length == ruleValues.length == decisions.length <= 200
  • Rule types are "USER", "REGION", "PLAN", "PERCENT", or "DEFAULT".
  • Percentage values are decimal integers from 0 through 100.
  • Decisions are "on" or "off".
  • User, region, and plan strings contain printable ASCII characters.
  • At most 500 total method calls are made.
Hints

Loading...
CallReturns
new FeatureFlagRuleEvaluator(["USER","REGION","PLAN","PERCENT","DEFAULT"], ["alice","in","pro","25","*"], ["off","on","on","on","off"])null
evaluate("alice", "in", "free")"off"
matchedRule("alice", "in", "free")0
evaluate("bob", "in", "free")"on"
matchedRule("bob", "in", "free")1

Alice's explicit rule wins before the India region rule. Bob has no user override, so the region rule at index 1 wins.

Run checks these cases. Submit also runs a larger hidden set.