Keep Only Folders Without a Listed Ancestor

Quick Overview

Keep Only Folders Without a Listed Ancestor. Given unique valid absolute folder paths, remove every path that is a subfolder of another listed path.

Keep Only Folders Without a Listed Ancestor

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given unique valid absolute folder paths, remove every path that is a subfolder of another listed path. A path is a subfolder only when the ancestor path is followed immediately by a slash and then more path text. Return all surviving paths in any order. ```cpp class Solution { public: vector<string> removeSubfolders(vector<string>& folder); }; ``` ### Examples **Example 1:** ```text Input: folder = ["/q/r","/q","/qr","/z"] Output: ["/q","/qr","/z"] Explanation: /q/r is inside /q, while /qr is a different top-level name. ``` **Example 2:** ```text Input: folder = ["/m/n","/m/n/o","/m/p"] Output: ["/m/n","/m/p"] Explanation: Only /m/n/o has another listed path as an ancestor. ``` ### Constraints - There are 1 to 40000 unique folder paths. - Every path has length 2 to 100, starts with `/`, and uses lowercase English letters and slashes. - Paths follow the stated nonempty-segment format. ### Contract considerations Each path consists of one or more nonempty lowercase segments prefixed by slashes; `/` alone is not a valid path. ```hint Read the boundary condition Does a shared character prefix alone make one path a subfolder, or must a slash mark the ancestor boundary? ``` ### What a Strong Answer Covers Each path consists of one or more nonempty lowercase segments prefixed by slashes; `/` alone is not a valid path. ### Follow-up Questions Optional practice extension, separate from the original task: How could paths be processed incrementally rather than all at once?

Overview: Keep Only Folders Without a Listed Ancestor. Given unique valid absolute folder paths, remove every path that is a subfolder of another listed path.

Amazon logo
Amazon
Jun 15, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given unique valid absolute folder paths, remove every path that is a subfolder of another listed path. A path is a subfolder only when the ancestor path is followed immediately by a slash and then more path text. Return all surviving paths in any order.

class Solution {
public:
    vector<string> removeSubfolders(vector<string>& folder);
};

Examples

Example 1:

Input: folder = ["/q/r","/q","/qr","/z"]
Output: ["/q","/qr","/z"]
Explanation: /q/r is inside /q, while /qr is a different top-level name.

Example 2:

Input: folder = ["/m/n","/m/n/o","/m/p"]
Output: ["/m/n","/m/p"]
Explanation: Only /m/n/o has another listed path as an ancestor.

Constraints

  • There are 1 to 40000 unique folder paths.
  • Every path has length 2 to 100, starts with / , and uses lowercase English letters and slashes.
  • Paths follow the stated nonempty-segment format.

Contract considerations

Each path consists of one or more nonempty lowercase segments prefixed by slashes; / alone is not a valid path.

What a Strong Answer Covers Guidance

Each path consists of one or more nonempty lowercase segments prefixed by slashes; / alone is not a valid path.

Follow-up Questions Guidance

Optional practice extension, separate from the original task: How could paths be processed incrementally rather than all at once?