Design a file system find API with composable filters

Quick Overview

Design an in-memory file search class that stores files with a name, size and extension, and returns the sorted paths under a directory that pass every active filter. It tests the Strategy Pattern, recursive tree traversal, AND composition of filters, and correct lexicographic ordering of full paths.

Design a file system find API with composable filters

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory file system search utility. It supports adding files with metadata (name, size, extension) and searching for files with composable filters. The system should use the **Strategy Pattern** so filters can be composed freely. The filters match by exact name, by minimum size, and by exact extension. Several filters can be active at the same time, and they combine with AND logic: a file must pass every active filter to appear in the results. The `find` operation walks all files under a given directory path, recursively, and returns the matching file paths in lexicographic order. Implement a class `FileSystemFind` with these methods: - `addFile(path: str, size: int, extension: str)`: add a file at the given path with the given size and extension. Missing intermediate directories are created automatically. - `addFilterByName(name: str)`: add a filter strategy that matches files with exactly the given name. - `addFilterByMinSize(minSize: int)`: add a filter strategy that matches files whose size is greater than or equal to `minSize`. - `addFilterByExtension(extension: str)`: add a filter strategy that matches files with exactly the given extension. - `clearFilters()`: remove all active filters, so later `find` calls return every file. - `find(path: str) -> List[str]`: return a lexicographically sorted list of the paths of all files under the given path that pass every active filter. ```hint Shape of a filter Give every filter type the same one-method interface that takes a file and returns a boolean. Then `find` does not need to know which filter types exist. ``` ```hint Sorted order Check whether visiting each directory's children in sorted name order really gives sorted full paths. Compare `/a/b.txt` with `/a/b/c.txt` character by character. ``` ### Examples Example 1: ```text Input: ["FileSystemFind", "addFile", "addFile", "find", "addFilterByExtension", "find", "clearFilters", "find"] [[], ["/a/b.txt", 10, "txt"], ["/a/c.py", 20, "py"], ["/a"], ["py"], ["/a"], [], ["/a"]] Output: [null, null, null, ["/a/b.txt", "/a/c.py"], null, ["/a/c.py"], null, ["/a/b.txt", "/a/c.py"]] ``` ```python fs = FileSystemFind() # empty file system fs.addFile("/a/b.txt", 10, "txt") # add b.txt (10 bytes) fs.addFile("/a/c.py", 20, "py") # add c.py (20 bytes) fs.find("/a") # -> ["/a/b.txt", "/a/c.py"] (no filters) fs.addFilterByExtension("py") # filter: extension == "py" fs.find("/a") # -> ["/a/c.py"] fs.clearFilters() # remove all filters fs.find("/a") # -> ["/a/b.txt", "/a/c.py"] (no filters again) ``` Only `c.py` has the extension `py`, so the second `find` drops `b.txt`. After `clearFilters` both files come back. Example 2: ```python fs = FileSystemFind() fs.addFile("/docs/readme.md", 500, "md") fs.addFile("/docs/notes/todo.md", 50, "md") fs.addFile("/docs/notes/plan.txt", 2000, "txt") fs.addFile("/src/main.py", 1200, "py") fs.find("/") # -> ["/docs/notes/plan.txt", "/docs/notes/todo.md", "/docs/readme.md", "/src/main.py"] fs.addFilterByMinSize(100) fs.find("/docs") # -> ["/docs/notes/plan.txt", "/docs/readme.md"] fs.addFilterByExtension("md") fs.find("/docs") # -> ["/docs/readme.md"] fs.find("/src") # -> [] ``` The search under `/docs` reaches the nested `notes` directory, and `todo.md` (50 bytes) fails the size filter. Once the extension filter is added, a file needs at least 100 bytes and the extension `md`, so only `readme.md` is left under `/docs`, and `main.py` under `/src` fails the extension check. ### Constraints & Assumptions - `1 <= name.length <= 100` - `0 <= size <= 10^6` - An extension is a string without the leading dot, such as `"txt"` or `"py"`. - Paths use `/` as the separator and start with `/`. - File names are unique within a directory. - At most 1000 calls in total across all methods. - A file's name is the last component of its path, so the file at `/a/b.txt` has the name `b.txt`. ### Clarifying Questions to Ask - What should `find` return for a path that does not exist, and for a path that points at a file? - What should `addFile` do when the path already exists, or when it passes through an existing file? - Is lexicographic order plain case-sensitive character order, or a locale-aware order? ### What a Strong Answer Covers - A data model for files and directories that supports inserting by path and walking the subtree under any directory. - Filters as interchangeable strategy objects behind one interface, so a new filter type needs no change to `find`. - AND composition across all active filters, including the case with no filters and the effect of `clearFilters`. - How the result is guaranteed to be in lexicographic order of full paths. - The cost of `addFile` and `find`, and edge cases such as a missing path, a file path passed to `find`, and trailing slashes. ### Follow-up Questions - How would you support OR and NOT combinations, such as "extension is `py` or size is at least 1 MB"? - How would you speed up `find` on a tree with millions of files when most queries filter by extension or size? - How would you add a filter on a name pattern (a glob or a regex) or on modification time without editing `FileSystemFind`? - How would you return matches lazily, in sorted order, without building the whole result list first?

Overview: Design an in-memory file search class that stores files with a name, size and extension, and returns the sorted paths under a directory that pass every active filter. It tests the Strategy Pattern, recursive tree traversal, AND composition of filters, and correct lexicographic ordering of full paths.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory file system search utility. It supports adding files with metadata (name, size, extension) and searching for files with composable filters.

The system should use the Strategy Pattern so filters can be composed freely. The filters match by exact name, by minimum size, and by exact extension. Several filters can be active at the same time, and they combine with AND logic: a file must pass every active filter to appear in the results.

The find operation walks all files under a given directory path, recursively, and returns the matching file paths in lexicographic order.

Implement a class FileSystemFind with these methods:

  • addFile(path: str, size: int, extension: str) : add a file at the given path with the given size and extension. Missing intermediate directories are created automatically.
  • addFilterByName(name: str) : add a filter strategy that matches files with exactly the given name.
  • addFilterByMinSize(minSize: int) : add a filter strategy that matches files whose size is greater than or equal to minSize .
  • addFilterByExtension(extension: str) : add a filter strategy that matches files with exactly the given extension.
  • clearFilters() : remove all active filters, so later find calls return every file.
  • find(path: str) -> List[str] : return a lexicographically sorted list of the paths of all files under the given path that pass every active filter.

Examples

Example 1:

Input:
["FileSystemFind", "addFile", "addFile", "find", "addFilterByExtension", "find", "clearFilters", "find"]
[[], ["/a/b.txt", 10, "txt"], ["/a/c.py", 20, "py"], ["/a"], ["py"], ["/a"], [], ["/a"]]

Output:
[null, null, null, ["/a/b.txt", "/a/c.py"], null, ["/a/c.py"], null, ["/a/b.txt", "/a/c.py"]]
fs = FileSystemFind()              # empty file system
fs.addFile("/a/b.txt", 10, "txt")  # add b.txt (10 bytes)
fs.addFile("/a/c.py", 20, "py")    # add c.py (20 bytes)
fs.find("/a")                      # -> ["/a/b.txt", "/a/c.py"] (no filters)
fs.addFilterByExtension("py")      # filter: extension == "py"
fs.find("/a")                      # -> ["/a/c.py"]
fs.clearFilters()                  # remove all filters
fs.find("/a")                      # -> ["/a/b.txt", "/a/c.py"] (no filters again)

Only c.py has the extension py, so the second find drops b.txt. After clearFilters both files come back.

Example 2:

fs = FileSystemFind()
fs.addFile("/docs/readme.md", 500, "md")
fs.addFile("/docs/notes/todo.md", 50, "md")
fs.addFile("/docs/notes/plan.txt", 2000, "txt")
fs.addFile("/src/main.py", 1200, "py")
fs.find("/")                   # -> ["/docs/notes/plan.txt", "/docs/notes/todo.md", "/docs/readme.md", "/src/main.py"]
fs.addFilterByMinSize(100)
fs.find("/docs")               # -> ["/docs/notes/plan.txt", "/docs/readme.md"]
fs.addFilterByExtension("md")
fs.find("/docs")               # -> ["/docs/readme.md"]
fs.find("/src")                # -> []

The search under /docs reaches the nested notes directory, and todo.md (50 bytes) fails the size filter. Once the extension filter is added, a file needs at least 100 bytes and the extension md, so only readme.md is left under /docs, and main.py under /src fails the extension check.

Constraints & Assumptions

  • 1 <= name.length <= 100
  • 0 <= size <= 10^6
  • An extension is a string without the leading dot, such as "txt" or "py" .
  • Paths use / as the separator and start with / .
  • File names are unique within a directory.
  • At most 1000 calls in total across all methods.
  • A file's name is the last component of its path, so the file at /a/b.txt has the name b.txt .

Clarifying Questions to Ask Guidance

  • What should find return for a path that does not exist, and for a path that points at a file?
  • What should addFile do when the path already exists, or when it passes through an existing file?
  • Is lexicographic order plain case-sensitive character order, or a locale-aware order?

What a Strong Answer Covers Guidance

  • A data model for files and directories that supports inserting by path and walking the subtree under any directory.
  • Filters as interchangeable strategy objects behind one interface, so a new filter type needs no change to find .
  • AND composition across all active filters, including the case with no filters and the effect of clearFilters .
  • How the result is guaranteed to be in lexicographic order of full paths.
  • The cost of addFile and find , and edge cases such as a missing path, a file path passed to find , and trailing slashes.

Follow-up Questions Guidance

  • How would you support OR and NOT combinations, such as "extension is py or size is at least 1 MB"?
  • How would you speed up find on a tree with millions of files when most queries filter by extension or size?
  • How would you add a filter on a name pattern (a glob or a regex) or on modification time without editing FileSystemFind ?
  • How would you return matches lazily, in sorted order, without building the whole result list first?