KatInterview

Emptying a Directory Tree: Record the Deletion Order

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphspost-orderdfsiterative

Last updated

Produce the deterministic bottom-up deletion sequence for a file tree given as flat path lists, using an iterative post-order traversal that survives very deep nesting.

Wiping a Subtree Bottom-Up

A storage backend lets you remove files freely, but it refuses to remove a directory unless that directory is already empty. You want to wipe out everything at a given path, like rm -rf, and you need the exact order in which the removals happen.

The filesystem is described by two flat lists of path strings:

  • file_paths: entries that are regular files.
  • dir_paths: entries that are directories.

Given target, return the ordered list of removal operations that deletes target and everything beneath it.

Output format

Each operation is a string:

  • "file:" + path when a file is removed.
  • "directory:" + path when a directory is removed.

Precise rules

Parent and name. Split a path string at its last /. The part before it is the parent string and the part after it is the name. If the parent string is empty (for example /a), the parent is "/". Paths are treated as literal strings, with no normalization. A directory d has as children exactly those entries whose parent string equals d character for character.

What counts as a file. A path is a file if and only if it appears in file_paths. Every other path, including a target that appears in neither list, is a directory. A file is always a leaf. If other entries have a file as their parent, they are never reached.

Entries. Every occurrence of a path in file_paths or dir_paths is one entry. If the same path occurs more than once, across or within the lists, each occurrence is a separate child entry and is processed separately. Processing it again emits its operations again, including those for its whole subtree. If a path occurs in both lists, it is a file.

Target. Every occurrence of target in the lists is ignored when building children, so target is never a child of anything. The traversal starts at target itself.

Reachability. Only entries reachable from target through the parent-to-child relation above are processed. A directory's parent does not need to be listed for the entry to be a child of it. Entries that are not reachable never appear in the output.

Order of removal.

  1. A directory is removed only after all of its children, and everything below them, have been removed.
  2. A file is removed as soon as it is reached.
  3. The children of a directory are processed in ascending order of their names (the last path component). Names are compared as plain Python strings, by Unicode code point. This is not the order of the full paths. A child's entire subtree is finished before the next sibling begins.
  4. If target is a file, the answer is the single operation file:<target>. If target is an empty directory (no children), the answer is the single operation directory:<target>.
  5. If target is /, the top-level entries (those with parent "/") are its children.

Nesting can be extremely deep, so a solution that relies on ordinary recursion in Python will exceed the interpreter's recursion limit. Use an approach that does not depend on the call stack.

Constraints

  • The total number of entries (files plus directories) is at most 100,000.
  • Nesting depth can reach 20,000.
  • Paths are absolute strings starting with /, use / as the separator, and have no trailing slash (except the root / itself).
  • target may be a file or a directory.

Examples

Example 1

Input: deletion_order([], ['/app', '/app/logs'], '/app')

Output: ['directory:/app/logs', 'directory:/app']

Example 2

Input: deletion_order(['/app/config.json'], ['/app'], '/app/config.json')

Output: ['file:/app/config.json']

Example 3

Input: deletion_order(['/tmp/a', '/tmp/z', '/tmp/logs/x'], ['/tmp', '/tmp/logs'], '/tmp')

Output: ['file:/tmp/a', 'file:/tmp/logs/x', 'directory:/tmp/logs', 'file:/tmp/z', 'directory:/tmp']

Hints

Hint 1

A directory can only go after everything inside it, so you need a post-order traversal: handle children first, then the node itself.

Hint 2

Python cannot recurse 20,000 levels deep. Simulate the call stack yourself, for example by pushing a (path, exiting) pair, and push the directory again with exiting=True before pushing its children.

Hint 3

Build a parent-to-children map by splitting each path at its last '/'; an empty parent means '/'. Skip entries equal to the target, and push children in reverse sorted name order so the smallest name pops first.