Design Unix file search
I was asked this
No reports yet. Sign in to say where you were asked it.
An object-oriented design interview question asked in Amazon's coding round, last asked Aug 2025. It tests object-oriented design.
Design a find-like file search over a directory tree. A search starts at a directory and returns the files that match a set of filters: name pattern, minimum or maximum size, file type, extension, and others that will be added later. Filters must combine with AND, OR and NOT, and adding a new filter must not change the search code.
Requirements
- Functional: walk a directory tree from a root; apply a filter to every entry and return the paths that match; built-in filters: name matches a pattern, size greater or less than a value, is a file or a directory, has an extension; combine filters with AND, OR and NOT.
- Example: "all
.logfiles over 5 MB" or "files named*.pythat are not in a directory calledtests". - Non-functional: new filters are new classes, not edits to the search; large trees should not exhaust memory or the call stack.
Interface
Unless you agree on a different interface with the interviewer, assume:
class Filter:
def matches(self, entry: FileEntry) -> bool: ...
def search(root: Directory, flt: Filter) -> list[str]: ...
big_logs = And(ExtensionFilter("log"), MinSizeFilter(5 * 1024 * 1024))
search(root, big_logs)
Clarifying questions
- Is this over a real file system or an in-memory tree you define?
- Should symbolic links be followed, and how do you avoid loops?
- Are results needed all at once or streamed as they are found?
- Should the search be able to skip whole subtrees (like
-prune)? - What pattern syntax: glob or regular expressions?
What a strong answer covers
- Core classes:
FileEntry(name, path, size, type, extension, modified time) withFileandDirectory, theFilterinterface withNameFilter,MinSizeFilter,MaxSizeFilter,TypeFilter,ExtensionFilter, the compositesAnd,Or,Not, and aFileSearcher. - The composite pattern for combining filters, and the open/closed principle: a new filter is one class.
- Iterative traversal (an explicit stack or queue) to handle deep trees, a generator to stream results, and a visited set for symlink loops.
- Short-circuit evaluation and putting cheap checks (extension) before expensive ones (reading metadata).
- A separate "descend into this directory?" predicate for pruning subtrees.
- Tests with an in-memory tree: each filter, nested combinations, an empty directory.
Follow-ups
- Add a filter on file contents (contains a string) without slowing every search.
- Parallelize the walk over a very large tree.
- Build a builder or small query language so users can write
ext=log AND size>5MB.
Where it's asked
- Amazon · Coding · Last asked Aug 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
