KatInterview

Feature Flag Rollout Validator with Dependency Cycle Detection

Trees & GraphsModel answer
MediumModel answerTrees & Graphstrees-graphsstringsdepth-first-search

Last updated

Parse feature dependency specs, run modular checksum verification, and evaluate user eligibility and feature graph dependencies while handling cyclic rollouts.

This assessment consists of two distinct software components: a string hash generator and a feature rollout validation engine.


Component 1: Email Token Hash Generation

Implement a function build_verification_string(email: str, verification: str, k: int) -> str.

Parameters & Mapping Rules:

  • email: A string consisting solely of lowercase ASCII characters ('a' to 'z') and the symbol characters ':', '/', and '.'.
  • Character values map as follows:
    • 'a' through 'z' $\rightarrow 1$ to $26$
    • ':' $\rightarrow 27$
    • '/' $\rightarrow 28$
    • '.' $\rightarrow 29$
  • verification: A non-empty string used as an index-lookup table.
  • k: A positive integer indicating block size.

Algorithm:

  1. Process email sequentially in non-overlapping blocks of length k from left to right. The trailing block may have a length less than k.
  2. For each block, calculate the checksum as the sum of character values in that block.
  3. Compute the zero-based lookup index via checksum % len(verification).
  4. Append verification[index] to the result sequence.
  5. Return the concatenated string.

Component 2: Feature Rollout Dependency Evaluator

Implement a system to evaluate whether a specific feature flag (target_feature) can be safely enabled for a given User.

Data Models

  • User:

    • country: str
    • platform: str ("android" or "ios")
    • version: int
    • enabled_features: set[str] (features already active for this user)
  • FeatureRule:

    • allowed_countries: set[str]
    • min_android_version: int
    • min_ios_version: int
  • ResultDecision:

    • enabled: bool
    • reason: str

Required Rule Ordering & Rejection Reasons

Evaluate target_feature strictly in the following sequence:

  1. Eligibility Evaluation:

    • Verify country permission: user.country must be in rule.allowed_countries.
    • Verify platform version:
      • For "android": user.version >= rule.min_android_version.
      • For "ios": user.version >= rule.min_ios_version.
      • Any invalid/unsupported platform fails this check.
    • Rejection Logic:
      • If only country eligibility fails, return ResultDecision(False, "COUNTRY_NOT_ALLOWED").
      • If only platform/version eligibility fails, return ResultDecision(False, "PLATFORM_VERSION_NOT_SUPPORTED").
      • If both country and platform checks fail, return ResultDecision(False, "COUNTRY_AND_PLATFORM_NOT_ALLOWED").
  2. Dependency Resolution:

    • Dependency verification must only occur if both country and platform checks succeed. If eligibility fails, dependencies must not be parsed or traversed.
    • Parse the raw dependency text configuration. Lines are formatted as:
      feature_name: dep1, dep2, dep3
      • Strip outer whitespace around feature names and individual dependencies.
      • Split dependencies properly by comma. Empty dependencies or missing right-hand sides must resolve to an empty list.
    • A dependency D is satisfied if:
      • D is already present in user.enabled_features, OR
      • D is included in rollout_features (the current batch of new rollouts) AND all of D's downstream dependencies are also satisfied.
    • If a dependency D is neither in user.enabled_features nor in rollout_features, return ResultDecision(False, f"MISSING_DEPENDENCY: {D}").
    • If a dependency loop is encountered during traversal, return ResultDecision(False, f"CYCLE: {cycle_path}"), where cycle_path lists the path starting at the first repeated node and returning to itself (e.g., "CYCLE: A -> B -> C -> A").
  3. Success:

    • If all checks pass, return ResultDecision(True, "ENABLED").