KatInterview

Build a Composable Filesystem Query Engine in the Spirit of find

System DesignModel answer
HardModel answerSystem Designobject-oriented-designfilesystempredicates

Last updated

Design an extensible, object-oriented library and CLI that walks directory trees, filters entries with composable predicates, and handles symlinks, errors, and huge trees.

Problem

Design a module that lets callers search a filesystem by criteria, in the same spirit as the UNIX find utility. It should be usable both as a library and as a command-line tool.

Required capabilities

  • Selection criteria:
    • name pattern (glob or regex)
    • entry type (regular file, directory, symbolic link)
    • size range
    • modification and creation time
    • permissions and owner
    • minimum and maximum depth
  • Composition: criteria can be combined with AND, OR and NOT.
  • Symlink policy: the caller can choose to skip links or follow them.
  • Output: the caller can receive bare paths or richer metadata.

What to cover in your answer

  1. The main classes or interfaces, with a diagram or sketch and the key data structures.
  2. Two entry points: a library API and a command-style API. Show how both reach the same engine.
  3. How a developer adds a brand-new filter without editing existing code.
  4. The traversal strategy and its performance: early pruning, ordering of cheap versus expensive checks, and concurrency.
  5. Robustness: very large directories, very large trees, permission failures, files vanishing mid-scan, broken links and symlink cycles.
  6. A test plan, including how to test without touching a real disk.

State any assumptions you make (platform, scale, error policy).

Hints

Hint 1

Think of each criterion as an object with a single yes/no method on a directory entry, and make AND, OR and NOT objects of the same kind so they nest freely.

Hint 2

Most of the cost in a tree walk is in per-file metadata calls. Which criteria can be answered from the directory listing alone, and how can you avoid fetching metadata unless a check really needs it?

Hint 3

Separate the filesystem access, the walking strategy and the output formatting behind interfaces, so you can swap in a fake filesystem for tests or a parallel walker for speed. Do not forget cycle detection when links are followed.