← Questions

Design Unix file search

0
AmazonOO designLast asked Aug 2025

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 .log files over 5 MB" or "files named *.py that are not in a directory called tests".
  • 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) with File and Directory, the Filter interface with NameFilter, MinSizeFilter, MaxSizeFilter, TypeFilter, ExtensionFilter, the composites And, Or, Not, and a FileSearcher.
  • 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

object-oriented design

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing