Remove Sub-Folders from the Filesystem - Problem

Imagine you're organizing a computer filesystem and need to clean up redundant folder paths. You have a list of folder paths, but some folders are actually sub-folders of others, creating unnecessary clutter.

Your task is to remove all sub-folders and keep only the root-level folders that aren't contained within any other folder in the list.

What makes a sub-folder? A folder is considered a sub-folder if:

  • It starts with another folder's path
  • Followed by a forward slash /
  • Then additional path components

Example: "/a/b" is a sub-folder of "/a", but "/b" is NOT a sub-folder of "/a/b/c"

Goal: Given a list of folder paths, return only the folders that aren't sub-folders of any other folder in the list. The order of the result doesn't matter.

Input & Output

example_1.py — Basic Case
$ Input: folder = ["/a", "/a/b", "/c/d", "/c/d/e", "/c/f"]
Output: ["/a", "/c/d", "/c/f"]
💡 Note: Folders '/a/b' is a subfolder of '/a' and '/c/d/e' is a subfolder of '/c/d', so they are removed.
example_2.py — No Subfolders
$ Input: folder = ["/a", "/b", "/c", "/d"]
Output: ["/a", "/b", "/c", "/d"]
💡 Note: No folder is a subfolder of another, so all folders are kept in the result.
example_3.py — Nested Chain
$ Input: folder = ["/a/b/c", "/a/b", "/a"]
Output: ["/a"]
💡 Note: Both '/a/b' and '/a/b/c' are subfolders of '/a', so only '/a' remains.

Visualization

Tap to expand
Filesystem Cleanup ProcessStep 1: Original Folders/a/b/c/a/a/b/cStep 2: After Sorting/a/a/b/a/b/c/c✓ Keep✗ SkipStep 3: Scan and CompareCompare /a/b with /a: starts with '/a/' → skipCompare /a/b/c with /a: starts with '/a/' → skipCompare /c with /a: doesn't start with '/a/' → keepStep 4: Final Result/a/c
Understanding the Visualization
1
Initial State
We have mixed folder paths in random order
2
Sort Alphabetically
Sorting ensures subfolders appear right after their parents
3
Linear Scan
Check each folder against the last kept folder only
4
Clean Result
Keep only root folders, skip all subfolders
Key Takeaway
🎯 Key Insight: Sorting is the key optimization - it ensures that subfolders always appear immediately after their parent folders, allowing us to make the minimum number of comparisons needed.

Time & Space Complexity

Time Complexity
⏱️
O(n² × m)

n² for nested loops comparing all pairs, m for average string length comparison

n
2n
Quadratic Growth
Space Complexity
O(1)

Only using a few variables, not counting the output array

n
2n
Linear Space

Constraints

  • 1 ≤ folder.length ≤ 4 × 104
  • 2 ≤ folder[i].length ≤ 100
  • folder[i] contains only lowercase letters and '/'
  • folder[i] always starts with the character '/'
  • Each folder name is unique
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen