Configuration Rules SDK with Rule Precedence

Quick Overview

Design a client-side configuration library that resolves a value for a key from a default and a set of conditional rules, picking the winner by priority, then number of conditions, then recency. It tests low-level design, choosing a data structure that keeps precedence order, subset matching, and read versus write complexity.

Configuration Rules SDK with Rule Precedence

Company: JioStar

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a client-side configuration library that is deployed with the app (no server). It resolves configuration values for a key using a default value and a set of conditional rules. Each rule belongs to a key and has a list of conditions, a value, and an integer `priority`. Every condition is an `"attribute=value"` string such as `"country=India"` or `"device=Android"`. A request is described by a list of `"attribute=value"` params. A rule **matches** when every one of its conditions appears in the params; a rule with no conditions always matches. Among matching rules, the winner is chosen by precedence: higher `priority` wins; on a tie, the rule with more conditions (more specific) wins; on a further tie, the most recently added rule wins. If no rule matches, the key's default is returned, and if there is no default, `null` is returned. Implement the following methods: - `setDefault(key: str, value: str)`: set (or overwrite) the fallback value for the key. - `addRule(key: str, conditions: List[str], value: str, priority: int)`: add a rule for the key. Each condition is an `"attribute=value"` string. An empty conditions list always matches. - `getConfig(key: str, params: List[str]) -> Optional[str]`: return the value of the winning matching rule for the key (by priority, then specificity, then recency), else the default, else `null`. Example 1: ```text setDefault("theme", "light") addRule("theme", ["country=India"], "saffron", 1) addRule("theme", ["country=India", "device=Android"], "green", 1) addRule("theme", ["app=beta"], "purple", 2) getConfig("theme", ["device=Android", "country=India"]) -> "green" getConfig("theme", ["country=India", "device=Android", "app=beta"]) -> "purple" getConfig("theme", ["country=US"]) -> "light" getConfig("banner", ["country=India"]) -> null ``` The first lookup matches the `saffron` and `green` rules, both at priority 1, and `green` wins because it has two conditions. In the second lookup all three rules match and `purple` wins on priority even though it has fewer conditions than `green`. The third lookup matches no rule and falls back to the default, and `banner` has neither rules nor a default. Example 2: ```text setDefault("timeout", "10") addRule("timeout", ["device=Android"], "45", 5) addRule("timeout", ["network=3G"], "60", 5) addRule("timeout", [], "30", 0) getConfig("timeout", ["device=Android", "network=3G"]) -> "60" getConfig("timeout", ["Device=Android"]) -> "30" getConfig("timeout", []) -> "30" ``` In the first lookup the two priority-5 rules tie on priority and on condition count, so the one added later wins. `"Device=Android"` does not match `"device=Android"` because matching is case-sensitive, so only the rule with no conditions matches. That rule also matches an empty param list, so this key never falls back to its default `"10"`. ```hint Precedence as one key The three tie-breaks (priority, number of conditions, recency) can be folded into one comparable value per rule. Think about what you must record when a rule is added so that "most recently added" can be compared later. ``` ```hint Where to pay the cost Decide whether the ordering work belongs in `addRule` or in `getConfig`. If each key's rules were already kept in precedence order, how far into the list would a lookup need to read? ``` ### Constraints & Assumptions - `1 <= key.length, value.length <= 50` - Every condition and param is a non-empty string of the form `"attribute=value"`; matching is exact and case-sensitive. - `0 <= conditions.length, params.length <= 20` - Conditions within a single rule are distinct. - `-10⁹ <= priority <= 10⁹` - At most `10⁴` total calls will be made to `setDefault`, `addRule`, and `getConfig`. - `getConfig` returns `null` when no rule matches and no default is set. ### Clarifying Questions to Ask - Is the rule set append-only, or can rules be removed or updated after they are added? - Are rules loaded once at startup and then mostly read, or are reads and writes interleaved at similar rates? - Can the params contain two values for the same attribute (for example `device=Android` and `device=iOS`), and if so is plain string matching still the intended behavior? - Will the library be called from several threads at once? ### What a Strong Answer Covers - A data model for defaults and per-key rules, with an ordering that encodes priority, then condition count, then insertion order, and a unique sequence number for recency. - A matching test that treats a rule's conditions as a subset of the params, handling rules with no conditions and exact, case-sensitive string comparison. - The fallback chain from winning rule to default to `null`, including keys that have no rules at all. - Time and space complexity of each method, and the trade-off between ordering rules on insert and scanning all of them on every lookup. - Working code for the three methods, checked against an example that exercises each tie-break. ### Follow-up Questions - How would you support removing or updating a rule without rebuilding the whole list? - If a key had thousands of rules and `getConfig` ran on every screen render, how would you avoid testing every rule on each lookup? - How would you make the library safe when one thread adds rules while others read configuration? - How would you load the rules from a file bundled with the app and swap in a new rule set without readers seeing a half-loaded state?

Overview: Design a client-side configuration library that resolves a value for a key from a default and a set of conditional rules, picking the winner by priority, then number of conditions, then recency. It tests low-level design, choosing a data structure that keeps precedence order, subset matching, and read versus write complexity.

JioStar logo
JioStar
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a client-side configuration library that is deployed with the app (no server). It resolves configuration values for a key using a default value and a set of conditional rules.

Each rule belongs to a key and has a list of conditions, a value, and an integer priority. Every condition is an "attribute=value" string such as "country=India" or "device=Android". A request is described by a list of "attribute=value" params. A rule matches when every one of its conditions appears in the params; a rule with no conditions always matches.

Among matching rules, the winner is chosen by precedence: higher priority wins; on a tie, the rule with more conditions (more specific) wins; on a further tie, the most recently added rule wins. If no rule matches, the key's default is returned, and if there is no default, null is returned.

Implement the following methods:

  • setDefault(key: str, value: str) : set (or overwrite) the fallback value for the key.
  • addRule(key: str, conditions: List[str], value: str, priority: int) : add a rule for the key. Each condition is an "attribute=value" string. An empty conditions list always matches.
  • getConfig(key: str, params: List[str]) -> Optional[str] : return the value of the winning matching rule for the key (by priority, then specificity, then recency), else the default, else null .

Example 1:

setDefault("theme", "light")
addRule("theme", ["country=India"], "saffron", 1)
addRule("theme", ["country=India", "device=Android"], "green", 1)
addRule("theme", ["app=beta"], "purple", 2)

getConfig("theme", ["device=Android", "country=India"])              -> "green"
getConfig("theme", ["country=India", "device=Android", "app=beta"])  -> "purple"
getConfig("theme", ["country=US"])                                   -> "light"
getConfig("banner", ["country=India"])                               -> null

The first lookup matches the saffron and green rules, both at priority 1, and green wins because it has two conditions. In the second lookup all three rules match and purple wins on priority even though it has fewer conditions than green. The third lookup matches no rule and falls back to the default, and banner has neither rules nor a default.

Example 2:

setDefault("timeout", "10")
addRule("timeout", ["device=Android"], "45", 5)
addRule("timeout", ["network=3G"], "60", 5)
addRule("timeout", [], "30", 0)

getConfig("timeout", ["device=Android", "network=3G"])  -> "60"
getConfig("timeout", ["Device=Android"])                -> "30"
getConfig("timeout", [])                                -> "30"

In the first lookup the two priority-5 rules tie on priority and on condition count, so the one added later wins. "Device=Android" does not match "device=Android" because matching is case-sensitive, so only the rule with no conditions matches. That rule also matches an empty param list, so this key never falls back to its default "10".

Constraints & Assumptions

  • 1 <= key.length, value.length <= 50
  • Every condition and param is a non-empty string of the form "attribute=value" ; matching is exact and case-sensitive.
  • 0 <= conditions.length, params.length <= 20
  • Conditions within a single rule are distinct.
  • -10⁹ <= priority <= 10⁹
  • At most 10⁴ total calls will be made to setDefault , addRule , and getConfig .
  • getConfig returns null when no rule matches and no default is set.

Clarifying Questions to Ask Guidance

  • Is the rule set append-only, or can rules be removed or updated after they are added?
  • Are rules loaded once at startup and then mostly read, or are reads and writes interleaved at similar rates?
  • Can the params contain two values for the same attribute (for example device=Android and device=iOS ), and if so is plain string matching still the intended behavior?
  • Will the library be called from several threads at once?

What a Strong Answer Covers Guidance

  • A data model for defaults and per-key rules, with an ordering that encodes priority, then condition count, then insertion order, and a unique sequence number for recency.
  • A matching test that treats a rule's conditions as a subset of the params, handling rules with no conditions and exact, case-sensitive string comparison.
  • The fallback chain from winning rule to default to null , including keys that have no rules at all.
  • Time and space complexity of each method, and the trade-off between ordering rules on insert and scanning all of them on every lookup.
  • Working code for the three methods, checked against an example that exercises each tie-break.

Follow-up Questions Guidance

  • How would you support removing or updating a rule without rebuilding the whole list?
  • If a key had thousands of rules and getConfig ran on every screen render, how would you avoid testing every rule on each lookup?
  • How would you make the library safe when one thread adds rules while others read configuration?
  • How would you load the rules from a file bundled with the app and swap in a new rule set without readers seeing a half-loaded state?