KatInterview

Versioned Cache and Longest Dependency Chain

Data Structure DesignAuto-graded · 19 test cases
HardAuto-gradedData Structure Designdesign-dstrees-graphsdynamic-programming

Last updated

Process a series of timestamped key-value operations and find the longest non-cyclic path in a dependency graph.

You are tasked with evaluating two separate system component sub-modules: a time-travel key-value state evaluator and a longest dependency chain analyzer.

Component 1: Time-Travel Operations

You are given a sequence of operations (operations) and their corresponding positional arguments (arguments). Supported operations include:

  • "put": [key, value, timestamp] — Stores value for key at timestamp. If an entry exists at timestamp, overwrite it.
  • "delete": [key, timestamp] — Removes key at timestamp. Subsequent reads at or after this timestamp return None (tombstone) until a newer "put" is applied.
  • "get": [key, timestamp] — Returns the value of key as of timestamp. This corresponds to the write/delete entry with the largest timestamp $\le \text{timestamp}$. Returns None if no entry exists $\le \text{timestamp}$, or if the effective entry is a delete.
  • "scan": [prefix, timestamp] — Returns a list of all active keys (keys whose effective value at timestamp is not None) starting with prefix, ordered lexicographically.

For "put" and "delete", the return output is None. For "get" and "scan", the output is the evaluated result.

Component 2: Longest Dependency Path

Given a list of directed edges [u, v] (where v depends on u, establishing a directed edge $u \to v$) and a start_node:

  • Find the longest path in the graph starting at start_node.
  • If a cycle exists within the graph, return an empty list [].
  • If multiple paths tie for the maximum length, choose the lexicographically smallest path sequence.
  • If start_node has no outgoing edges, the path is [start_node].

Return a single list containing two items: [operations_results, longest_path].

Constraints

  • $0 \le \text{len(operations)} \le 1000$
  • Timestamps are positive integers.
  • Keys, values, and node names are non-empty strings.
  • Graphs may contain up to 5,000 nodes and directed edges.
  • Node comparison for tie-breaking uses standard lexicographical string ordering.

Examples

Example 1

Input: evaluate_system_features(['put', 'put', 'get'], [['config', 'v1', 10], ['config', 'v2', 20], ['config', 15]], [], '')

Output: [[None, None, 'v1'], ['']]

Example 2

Input: evaluate_system_features([], [], [['A', 'B'], ['B', 'C'], ['A', 'C']], 'A')

Output: [[], ['A', 'B', 'C']]

Hints

Hint 1

For time-travel queries, maintain history maps per key and use binary search (bisect_right) on sorted timestamps to find floor entries.

Hint 2

A delete operation can be represented internally as setting a key's timestamp to None.

Hint 3

To find the longest path on a graph, detect cycles using Topological Sort (Kahn's Algorithm). If topological ordering fails to include all nodes, a cycle exists.

Hint 4

Using dynamic programming in reverse topological order allows efficiently computing the maximum length path and resolving lexicographical ties.