KatInterview

Batch-Driven Slash-Path Registry with Leaf-Only Deletion

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designtriehash-mapdesign

Last updated

Simulate a tree-shaped registry addressed by slash-separated paths, processing a batch of create, set, get and remove commands and returning one result per command.

You are building an in-memory registry whose entries are addressed by UNIX-style absolute paths such as /a/b. Entries form a tree: each entry can have child entries, and each entry stores one value.

Implement a function that processes a batch of commands, in order, against a single registry that starts fresh, and returns one result per command.

Commands

Command i is operations[i] and its parameters are arguments[i].

CommandParametersEffectResult
"create"[path, value]Add a new entry at path holding value. Succeeds only if the parent entry exists and path itself does not exist yet.true on success, otherwise false
"set"[path, value]Replace the value of an existing entry.true on success, false if the path does not exist
"get"[path]Look up the value at path.[true, value] if it exists, otherwise [false, null]
"remove"[path]Delete the entry at path, but only if it exists and has no children.true on success, otherwise false

Any command name other than the four above produces false.

Path rules

  • A valid path is a string that begins with /.
  • The path "/" is valid and denotes the root.
  • Any other valid path is / followed by one or more segments separated by single / characters, and every segment must be non-empty. So "", "a", "/a/", "//a" and "/a//b" are all malformed.
  • A non-string path is malformed.
  • Segments are ordinary names; . and .. have no special meaning and no normalization is performed.
  • A malformed path makes the command fail (false for create, set, remove; [false, null] for get) and never changes the registry.

Root behavior

  • The root entry always exists from the start, with initial value null.
  • get("/") therefore returns [true, <current root value>].
  • set("/", v) succeeds and stores v at the root.
  • create("/", ...) fails, since the root already exists.
  • remove("/") always fails.

Other details

  • A stored value may be null. get on such an entry returns [true, null], which differs from the [false, null] returned for a missing path.
  • A failed create must leave the existing value untouched.
  • After a successful remove, the same path may be created again.
  • If create or set arrives without a value argument, treat the value as null. If a command has no path argument at all, treat its path as malformed.

Task

Return a list of results, one per command, in the same order as the commands.

Constraints

  • len(operations) == len(arguments)
  • Paths are limited in length only by reasonable input sizes; aim for time proportional to the path length per command.
  • Values can be any JSON-like data (numbers, strings, null, and so on).
  • Segment names are case-sensitive and compared exactly.

Examples

Example 1

Input: run_path_registry(['create', 'create', 'get', 'set', 'get', 'remove', 'get'], [['/a', 1], ['/a', 2], ['/a'], ['/a', 3], ['/a'], ['/a'], ['/a']])

Output: [True, False, [True, 1], True, [True, 3], True, [False, None]]

Example 2

Input: run_path_registry(['create', 'create', 'create', 'remove', 'remove', 'get'], [['/missing/child', 1], ['/a', 1], ['/a/b', 2], ['/a'], ['/a/b'], ['/a']])

Output: [False, True, True, False, True, [True, 1]]

Example 3

Input: run_path_registry(['get', 'set', 'get', 'create', 'get', 'remove', 'get'], [['/'], ['/', 7], ['/'], ['/', 9], ['//bad'], ['/'], ['/']])

Output: [[True, None], True, [True, 7], False, [False, None], False, [True, 7]]

Hints

Hint 1

Model the registry as a tree where every node holds a value and a dictionary from segment name to child node. The path `/a/b` is root, then child `a`, then child `b`.

Hint 2

Write one helper that validates and splits a path into segments (returning a failure marker for malformed input, and an empty list for the root), and another that walks the tree along a list of segments and returns the node or nothing.

Hint 3

For `create` and `remove`, walk to the parent using every segment except the last, then check the last segment in the parent's children. Handle the root specially: it can be read and set but never created or removed. Remember that `get` must return a found flag so a stored `null` is distinguishable from a miss.