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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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: ...