Collapse Nested Folder Paths to Their Top-Level Roots

Last updated: November 29, 2026

Quick Overview

Given a list of absolute folder paths, remove every folder that lies inside another folder in the list and return the remaining paths.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of absolute folder paths, remove every folder that lies inside another folder in the list and return the remaining paths.

A Google technical screen question framed as cleaning up a Drive sync selection, where syncing /a already covers /a/b. It checks segment-level prefix logic, avoiding the classic bug where /a/bc is wrongly treated as inside /a/b.

What the Interviewer Expects
  • Split paths into segments rather than comparing raw strings
  • Insert into a trie and stop descending at a marked folder
  • Collect only marked nodes with no marked ancestor
  • Avoid the /a/b vs /a/bc false match
  • Compare with sorting and comparing with the last kept path
Key Topics to Cover
trie
file paths
prefix by segment
DFS
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you handle paths that contain '..' or trailing slashes?
  • How would you support adding and removing folders dynamically?
  • How would you count files covered by each top-level root?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: folders, distinct absolute paths like /a/b. Output: paths that are not nested under any other path in the list, in any order. /a/bc is not inside /a/b....

High-Level Design

String startswith fails because /a/bc starts with /a/b. Treat each path as a list of segments. Insert each into a trie keyed on segments and mark the node where it ends. Then DFS from the root: ...


Submit Your Answer
Markdown supported