KatInterview

Replay Path Insertions and Filename Lookups on a Component Trie

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designtrietree-traversaliterative-dfs

Last updated

Simulate an in-memory path trie driven by a list of add and search operations, with iterative traversal, path normalization, and exact rules for what counts as a file.

Replaying Operations on a Path Tree

You are given a script of operations to run against an initially empty, in-memory directory tree. Return what each operation produces.

Write a function that takes two parallel lists:

  • operations: strings, each either "add_path" or "find_file".
  • arguments: for each operation, a list holding exactly one string, which is the argument to that operation.

Run the operations in order, starting from an empty tree. Pair the i-th operation with the i-th argument list. If the two lists have different lengths, process only as many pairs as the shorter list provides and ignore the rest.

Operations

add_path(path)

  • Split the path on / and discard every empty piece. This makes leading, repeated, and trailing slashes irrelevant. For example, //a//b/ is the same as /a/b, and a string with no leading slash is split the same way.
  • Walk from the root through the resulting components in order, creating any component that does not yet exist.
  • Mark the final node reached as a file. Adding the same path again changes nothing.
  • If the path has no components (for example "/"), nothing is marked, because the root is never a file.

find_file(filename)

Return the full absolute paths of every node that satisfies both conditions:

  1. It was marked as a file by some earlier add_path.
  2. Its last component equals filename exactly.

Rules for the result:

  • Directories that were only created implicitly as parents are not files. They are never returned, even if their name matches.
  • A node stays a file even if a longer path is added below it later. For example, after adding /a/b and then /a/b/c, searching for b returns /a/b.
  • Each returned path is / followed by its components joined with /. It has no trailing slash and no repeated slashes.
  • Sort the result with ordinary string comparison, character by character. Note that "/a/b/a/t" comes before "/a/t" because 'b' < 't'.
  • If nothing matches, return an empty list.

Output

Return a list with one entry per processed operation:

  • None for each add_path.
  • The sorted list of path strings for each find_file.

Paths can be nested up to 2000 levels deep, so the search should not rely on recursion. Use an explicit stack.

Constraints

  • Paths contain lowercase letters, digits, . and /.
  • At most 10^4 add_path operations.
  • Every path has at most 2000 components and at most 10^5 characters.
  • Every operation name is "add_path" or "find_file".
  • Each argument list contains exactly one string.

Examples

Example 1

Input: run_path_tree_ops(['add_path', 'add_path', 'add_path', 'find_file', 'find_file', 'find_file'], [['/var/log/syslog'], ['/var/log/nginx/access.log'], ['/home/user/log/syslog'], ['syslog'], ['access.log'], ['missing.txt']])

Output: [None, None, None, ['/home/user/log/syslog', '/var/log/syslog'], ['/var/log/nginx/access.log'], []]

Example 2

Input: run_path_tree_ops(['add_path', 'add_path', 'find_file'], [['/a/b/a/target.txt'], ['/a/target.txt'], ['target.txt']])

Output: [None, None, ['/a/b/a/target.txt', '/a/target.txt']]

Example 3

Input: run_path_tree_ops(['add_path', 'add_path', 'add_path', 'find_file'], [['/file.txt'], ['//a//file.txt/'], ['file.txt']])

Output: [None, None, None]

Hints

Hint 1

Model the tree as a trie keyed by path component, where each node has a dictionary of children and a boolean saying whether a path ended exactly there.

Hint 2

Splitting on '/' and dropping empty strings handles leading, repeated and trailing slashes in one step, and gives an empty component list for '/'.

Hint 3

For lookup, push (node, components_so_far) onto an explicit stack, check the flag and last component when popping, and sort the collected strings at the end. Remember that zip-style pairing ignores extra operations or arguments.