Given flags.json (flag definitions with key, enabled, default, and rules made of attribute/operator/value conditions), users.json (10,000 users with country, plan, etc.) and expected_decisions.json (answers for 200 users x 7 flags), implement a feature flag evaluation engine: a function to evaluate a single flag for a user and a function to evaluate all flags for a user. Conditions within a rule are ANDed.
Asked in the onsite practical coding stage. Validate against expected_decisions.json first, handle multi-rule conflicts and percentage rollout, then add unit tests.
Use evaluate_flag(flag, user) and evaluate_all_flags(flags, user). A matching rule returns its value; rules are checked in order, and no match returns default. Supported operators are equals, not_equals, in, and not_in. A rule may include percentage, evaluated deterministically from flag.key and user.id.
def evaluate_flag(flag, user):