KatInterview

Path-Addressed Tree Store: Replay a Command Log

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designdesigntriehash-map

Last updated

Simulate a path-keyed tree store by replaying a list of commands, testing trie or hash-map design, path validation, and exact failure semantics.

You are building a small in-memory store whose entries are arranged as a tree and addressed by slash-separated absolute paths. Instead of a class, you implement one function that replays a log of commands against a fresh, empty store and returns one result per command.

The store

  • The tree always contains a root node at path /, whose initial value is the string "#".
  • Every node holds a string value. The empty string is a legitimate value.
  • A node is identified by its absolute path, for example /a/b/c.

Valid paths

A path is valid if all of the following hold:

  • It is a string that starts with /.
  • It is exactly / (the root), or it is / followed by one or more segments separated by single / characters.
  • No segment is empty, ., or ... This rules out //, trailing slashes such as /a/, and relative-style segments.

Anything else (for example "", a/b, /a/, /a//b, /a/./b, /a/../b) is invalid.

Commands

Each command is a name plus an argument list. Commands are applied in order, and each produces one result.

CommandArgumentsBehaviorResult
create[path, value]Add a new node with the given value. The path must be valid and not the root, the parent node must exist, and the node itself must not already exist.true on success, otherwise false
set_value[path, value]Overwrite the value of an existing node (the root is allowed).true on success, otherwise false
get_value[path]Read the value of an existing node (the root is allowed).the value string, or null if the path is invalid or the node does not exist
delete[path]Remove an existing node that has no children. The root can never be deleted.true on success, otherwise false

Additional rules:

  • A failed command must leave the store unchanged.
  • If a command has the wrong number of arguments, create, set_value and delete return false, and get_value returns null.
  • If the command name is not one of the four above, the result is null.
  • After a node is deleted, the same path may be created again, and the new node starts with no children.

Task

Implement replay_tree_store(commands, command_args), where commands[i] is the name of the i-th command and command_args[i] is its list of string arguments. Return a list with the result of every command, in order.

Constraints

  • len(commands) == len(command_args)
  • Up to about 10^4 commands; each path is up to about 10^3 characters.
  • All values and path segments are strings; values may be empty.
  • Aim for time proportional to the path length per command.

Examples

Example 1

Input: replay_tree_store(['create', 'create', 'get_value', 'create', 'delete', 'set_value', 'get_value', 'delete', 'get_value', 'delete'], [['/shop', 'store'], ['/shop/apple', 'red'], ['/shop/apple'], ['/shop/apple', 'blue'], ['/shop'], ['/shop/apple', 'green'], ['/shop/apple'], ['/shop/apple'], ['/shop/apple'], ['/shop']])

Output: [True, True, 'red', False, False, True, 'green', True, None, True]

Hints

Hint 1

Model the store as a tree where every node keeps its own value and a dictionary from segment name to child node.

Hint 2

Write one helper that validates a path and splits it into segments (an empty list for the root), and another that walks from the root and returns the node or None.

Hint 3

For create and delete, walk to the parent of the target. Create fails if the parent is missing or the name already exists; delete fails if the target is missing or still has children. Handle the root and wrong argument counts before touching the tree.