Designing a File Search Utility

Difficulty: Intermediate Patterns: Specification, Composite, Iterator Asked at: Amazon, Google, Microsoft, Dropbox


find . -name "*.log" -size +100M -mtime +30 is one line of shell, and building the thing behind it fills an interview. Walk a directory tree, decide which entries match, hand them back. Neither half is hard alone, which is the trap: the difficulty sits in the seams between them.

The first seam is the API. Written the obvious way, the search method takes one parameter per criterion, so the sixth arrives with five nulls at every call site, and the body ANDs them all, so “name matches *.log OR extension is gz” cannot be written through that signature at any length. The Specification lesson uses this exact function as its worked example and derives the fix – each criterion becomes an object, combined with and, or and not – so this page takes it as settled and spends its budget on the two problems that lesson does not touch.

Both are about the walk. Traversal has to be lazy, and recursion cannot be. A recursive walk that collects into a List reads every directory before returning anything, and holds every match in memory while it does. find . -name "*.log" | head -20 over a two-million-file root should touch a few hundred directories and stop; a recursive collector touches all two million entries and throws the work away. The fix is to take the stack out of the language and put it in an object, which is what makes “give me the first twenty, then I will tell you if I want more” expressible.

And pruning is what makes the search fast, which needs a question isSatisfiedBy cannot answer. “Does this file match” says nothing about whether a directory is worth opening: a size criterion tells you nothing about a subtree, while a path criterion rules out millions of files with one answer at one node. So the interface grows a second method, and composing it has an asymmetry that is the best detail to get right here.

💡 Specification = one rule as an object with a single boolean method, so it has a type, a name, its own unit test, and and / or / not to glue it to other rules. Learn more →

💡 Lazy, also called streaming = results produced one at a time as the caller asks for them, so a search abandoned halfway never does the rest of the work. Learn more →

💡 Pruning = deciding not to open a directory at all, because nothing inside could match, so what you save is a whole subtree rather than one comparison. A glob like *.log is the shell wildcard syntax it prunes on – not a regular expression, and * never crosses a path separator.


Functional Requirements

  1. Criteria – search by name glob, size bound, modified time, extension or path prefix, each criterion an object, any two combining with AND, OR or NOT.
  2. Named rules – a composite the team says out loud, like “stale log file”, is registered under that name and rebuilt per run so its cutoff cannot go stale.
  3. Streaming results – search hands back matches one at a time, so a caller who wants the first twenty stops the walk after twenty.
  4. Pruning – before a directory opens, the criterion is asked whether anything inside could match, and a subtree it rules out is never listed.
  5. Traversal options – maximum depth, whether to follow symbolic links, whether to include hidden entries.
  6. Partial failure – an unreadable directory or an entry deleted mid-walk is reported and the search runs to completion.
  7. Ordering – results can come back sorted by name, size or modified time, which is the one mode that cannot stream.

Non-Functional Requirements

  1. Memory is O(depth), not O(matches) – one frame per level of the tree the walker is inside, plus exactly one node of lookahead. Depth on a real filesystem is tens; matches can be millions.
  2. Early exit is real – taking k matches opens only the directories needed to produce them, so a caller stopping at the twentieth never visits the rest.
  3. Per-candidate cost is independent of tree size – one isSatisfiedBy is a few field comparisons plus a glob matcher compiled once, so a full walk is O(n) in the entries visited and only O(n log n) when the caller asks for sorted output.
  4. No partial failure aborts the search – an unreadable directory costs one entry in a problem list, and a symlink cycle terminates instead of looping.