Remove Sub-Folders from the Filesystem

MediumStringstrie

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a list of distinct folder paths. Each path starts with / and is a sequence of folder names separated by /, for example /a/b. A folder X is a *sub-folder* of folder Y when X begins with Y followed by a /. So /a/b is inside /a, but /ab is not. Remove every folder that is a sub-folder of some other folder in the list, and return the remaining folders sorted in ascending lexicographic order.

Examples

Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]
`/a/b` lives inside `/a`, and `/c/d/e` lives inside `/c/d`.
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]
Both deeper paths are inside `/a`.
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
`/a/b/ca` shares the text prefix `/a/b/c` but is a different folder, so nothing is removed.

Constraints

  • 1 <= folder.length <= 4 * 10^4
  • 2 <= folder[i].length <= 100
  • Folder names contain only lowercase letters
  • Every path starts with `/`, has no trailing `/`, and all paths are distinct

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More trie problems