Delete Duplicate Folder Subtrees From a Synced Drive Tree

Last updated: November 29, 2026

Quick Overview

Given folder paths forming a tree, mark every folder whose non-empty subtree structure is identical to another folder's subtree, delete all marked folders with their contents, and return the remaining paths.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given folder paths forming a tree, mark every folder whose non-empty subtree structure is identical to another folder's subtree, delete all marked folders with their contents, and return the remaining paths.

A hard Google onsite question modeled on deduplicating a Drive or file system tree. The interviewer wants a trie of folder names, a canonical serialization of each subtree computed bottom-up, and deletion in a second pass so that marking happens only once.

What the Interviewer Expects
  • Build a trie from the paths
  • Serialize each subtree canonically with sorted children
  • Count serializations and mark folders whose non-empty signature repeats
  • Delete in a second pass, not during marking
  • Collect remaining paths with a DFS
Key Topics to Cover
trie
tree serialization
hashing
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
  • Why must children be sorted in the signature?
  • How would you use hashing to avoid long signature strings?
  • What if two folders should count as duplicates only when their names also match?
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: paths, each a list of folder names from the root, such that every prefix of every path is also listed. Two folders are identical if they have the same set of child names and each child is ide...

High-Level Design

Insert each path into a trie keyed by folder name. Post-order DFS computes a signature for each node: concatenate name(signature) for each child in sorted name order. A node's signature describes it...


Submit Your Answer
Markdown supported