← Questions
Remove deleted folders and their descendants from a path list
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's two coding screens round, last asked Sep 2025. It tests tries, strings and trees.
You get a list of hierarchical paths separated by /, such as California, California/San Francisco and California/San Francisco/7th Street, and a list of paths to delete. Deleting a path removes that path and everything under it. Return the paths that remain.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
filter_paths(paths: list[str], deletes: list[str]) -> list[str]returns the surviving paths in their original input order and with their original spelling.
Rules:
- A path is removed when some delete equals it or is an ancestor of it, compared segment by segment:
California/San FranciscoremovesCalifornia/San Francisco/7th Street, but notCalifornia, and notCalifornia/San(a shorter string that shares characters, not segments). - Leading, trailing and doubled slashes are ignored when comparing:
Nevada//RenoandNevada/Reno/are the same path. - An empty delete (
""or"/") matches nothing.
Example
paths = ["California", "California/San Francisco", "California/San Francisco/7th Street",
"California/San", "Nevada/Reno/", "Nevada"]
deletes = ["California/San Francisco/", "Nevada//Reno"]
filter_paths(paths, deletes) -> ["California", "California/San", "Nevada"]
Constraints
- Up to 10^5 paths and 10^5 deletes; each string up to about 200 characters.
Clarifying questions
- Is the comparison by whole segments or by raw string prefix?
- Should duplicate paths both appear in the output?
- Does the output order matter?
- Are paths case-sensitive?
What a strong answer covers
- Normalizing each path into a tuple of non-empty segments, while keeping the original string for the output.
- One efficient approach, justified: a hash set of normalized deletes with a check of every segment prefix of each path, a trie of delete paths with "deleted" markers, or sorting plus prefix skipping.
- Complexity in terms of total characters, for example
O(total length)with the set or trie. - Edge cases: overlapping deletes (a parent and its child both listed), duplicates, empty inputs, a delete that matches nothing.
Follow-ups
- The paths form a live tree with inserts and deletes arriving over time.
- Return the remaining paths as a nested tree instead of a flat list.
- Deletes may contain a wildcard segment such as
California/*/7th Street.
Where it's asked
- Snowflake · Two coding screens · Last asked Sep 202560 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
