KatInterview

Incremental Config Value Store with Dependency Tracking

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designgraphtopological-sortdesign

Last updated

Tests ability to build a stateful key-value store that resolves integer expressions with named references, propagates updates through a dependency graph, and rejects cycles.

You are building an in-memory store for a configuration system. Values in the store can be plain integers or expressions that sum up integer literals and references to other stored names. Implement the function described below in three stages, making sure each stage is correct before building on it.

The store exposes two operations, driven by a list of operation names and their corresponding arguments:

  • set_var(name, value) — defines or redefines a named entry.
  • get_var(name) — retrieves the current resolved integer for a name, or None.

The function receives a sequence of such operations and returns one result per operation in the same order. set_var results are None on success and the string "ValueError" when a definition is rejected (see Part 3). get_var results are the resolved integer or None.


Part 1 — Integer entries only

set_var(name, value) stores a plain Python int. get_var(name) returns it, or None if name has never been set. Choose internal storage that you will not need to throw away when expressions arrive in Part 2.

Complexity: set_var O(1), get_var O(1).


Part 2 — Expression entries, full recompute

set_var now also accepts a string that begins with =. Everything after the = is a sum of terms separated by +, where each term is either a plain integer literal or the name of another entry — for example "=base+retry+2".

get_var returns the computed sum. If any name referenced directly or transitively by an expression has never been set, get_var returns None for that expression and for anything that depends on it. You may assume no cycles exist in this stage.

Recomputing every entry from scratch on every set_var call is acceptable here, so that get_var stays O(1) by reading a cached result.

Complexity: set_var O(total size of all expressions), get_var O(1).


Part 3 — Cycle rejection and targeted recompute

Two additional requirements apply:

  1. Cycle rejection. Before accepting a new definition for a name, check whether that definition would introduce a cycle (directly or transitively). If it would, do not modify any existing state and instead record "ValueError" as the result for that operation.

  2. Targeted recompute. After a successful set_var, recompute only the changed entry and the entries that transitively depend on it — not every entry in the store.

Complexity: set_var O(size of affected downstream subgraph), get_var O(1).


Constraints

  • Name strings are valid identifiers (letters, digits, underscores; do not start with a digit), case-sensitive.
  • Integer literals in expressions and plain integer values may be negative or zero; no guaranteed range beyond standard Python integers.
  • A name may be redefined any number of times, including switching between a plain integer and an expression.
  • Expression terms are separated solely by +; no spaces, no other operators.
  • Each term in an expression is either a valid identifier or a decimal integer (possibly with a leading -).
  • get_var on a name that was never set returns None.
  • get_var on an expression whose dependency chain includes any never-set name returns None.
  • Cycles will not appear in the input during Parts 1 and 2; in Part 3 they must be detected and rejected as described.
  • 1 ≤ len(operations) = len(arguments) ≤ 1000.
  • Each expression contains at most 50 terms.
  • At most 200 distinct names will be stored.

Examples

Example 1

Input: run_config_store(['set_var', 'set_var', 'set_var', 'get_var', 'set_var', 'get_var', 'get_var'], [['base_timeout', 5], ['retry_timeout', '=base_timeout+10'], ['total_budget', '=retry_timeout+base_timeout+1'], ['total_budget'], ['base_timeout', 8], ['retry_timeout'], ['total_budget']])

Output: [None, None, None, 21, None, 18, 27]

Example 2

Input: run_config_store(['set_var', 'set_var', 'get_var', 'set_var', 'get_var'], [['a', '=missing+2'], ['b', '=a+3'], ['b'], ['missing', 4], ['b']])

Output: [None, None, None, None, 9]

Example 3

Input: run_config_store(['set_var', 'set_var', 'set_var', 'get_var'], [['a', 1], ['b', '=a+2'], ['a', '=b+1'], ['b']])

Output: [None, None, 'ValueError', 3]

Hints

Hint 1

For Part 2, a memoized recursive evaluator naturally processes dependencies before the variable that depends on them — you get a correct evaluation order without an explicit topological sort.

Hint 2

For cycle detection in Part 3, walk the *existing* dependency graph starting from the new definition's referenced names before touching any stored state; if you reach the name being defined, reject the call.

Hint 3

To do a targeted recompute efficiently, maintain two edge directions: deps[name] (what a name reads from) and dependents[name] (what reads from a name). When a name changes, walk forward through dependents to find all affected entries, then evaluate them in an order where each entry is computed after the entries it reads from.