Unix find command with pluggable filters

0
Sign in

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 directory path and in all of its subdirectories that pass filter. 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 than n.
  • ExtensionFilter(ext): the file has extension ext.
  • NamePrefixFilter(prefix): the file name starts with prefix.

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.
  • find on 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).

GoogleAmazonCodingHardLast asked Sep 2026

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.