Unix find command with pluggable filters
I was asked this
No reports yet. Sign in to say where you were asked it.
Implement a small version of the Unix find command over an in-memory file system.
The file system holds directories and files. Each file has a name and a size in bytes. Its extension is the text after the last dot in its name.
Implement a FileSystem class:
-
mkdir(path)creates a directory and any missing parents. -
add_file(path, size)adds a file, creating missing parent directories. Adding an existing path replaces that file. -
find(path, filter)returns the full paths of all files in the directorypathand in all of its subdirectories that passfilter. Return the paths in sorted order. Directories are never returned.
Filters share one interface:
class Filter(ABC):
@abstractmethod
def apply(self, file: File) -> bool: ...
Implement three filters:
-
MinSizeFilter(n): the file size is greater thann. -
ExtensionFilter(ext): the file has extensionext. -
NamePrefixFilter(prefix): the file name starts withprefix.
The design must make it easy to add new filter types later without changing find.
Example
add_file("/srv/logs/app.log", 2048)
add_file("/srv/logs/old/app-2024.log", 90)
add_file("/srv/config.yaml", 300)
find("/srv", ExtensionFilter("log"))
-> ["/srv/logs/app.log", "/srv/logs/old/app-2024.log"]
find("/srv", MinSizeFilter(200))
-> ["/srv/config.yaml", "/srv/logs/app.log"]
find("/srv/logs", NamePrefixFilter("app-"))
-> ["/srv/logs/old/app-2024.log"]
Constraints
- Paths are absolute and use
/as the separator. - Up to 10^5 files; directory depth up to 10^4.
-
findon a path that does not exist raises an error.
A common next part: let callers combine filters with AND, OR and NOT (for example, .log files larger than 1 KB).
Clarifying questions
- Does
add_fileonto a path that is already a directory raise? - Is the extension compared case-sensitively, and does a name with no dot have no extension?
- Should
findon a file path (not a directory) return that file or raise? - Is sorted order plain string order of full paths?
What a strong answer covers
- Models a tree of
Directorynodes with child maps andFileobjects (name, size, path, extension), somkdirandadd_fileare O(depth). - Walks the subtree iteratively (an explicit stack, since depth can reach 10^4) and keeps files where
filter.apply(file)is true; O(subtree size) plus the sort. - Defines the
Filterinterface sofindnever knows the filter types: open for extension, closed for change. - Implements the three filters precisely: strictly greater than
n, text after the last dot, prefix of the name only (not the path). - Raises on a missing start path, and tests nested directories, a dotless name, a dotfile, and a replaced file's new size.
Follow-ups
- Combine filters with AND, OR and NOT as composite filters.
- Add filters on modified time or depth limits, like
-mtimeand-maxdepth. - Stream results lazily instead of building the full list for huge trees.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.

