Unix find command with pluggable filters
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).
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.

