Compose File Search Filters With the Specification Pattern
Last updated: November 29, 2026
Quick Overview
Design an extensible API for searching a Drive-like file tree with criteria such as extension, minimum size, and name, combinable with AND, OR, and NOT, so new criteria can be added without modifying the search code.
0
0
0 solved
Design an extensible API for searching a Drive-like file tree with criteria such as extension, minimum size, and name, combinable with AND, OR, and NOT, so new criteria can be added without modifying the search code.
This is a common Google OOP prompt (a "find" command or Drive search filter) that tests open-closed design. Interviewers check that criteria are objects, combinators compose cleanly, and the traversal is separate from matching.
What the Interviewer Expects
- Defines a filter interface with matches
- Implements And, Or, Not as composite filters
- Keeps traversal independent of filter logic
- Allows new filters without changing existing code
- Handles deep trees without recursion limits
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you short-circuit traversal into directories that cannot match?
- How would you serialize a filter from a query string?
- How would this map to a search index instead of a scan?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Files have name, size, and directory flag with children.
- Filters: extension, min size, name contains; combinators AND, OR, NOT.
search(root, filter)returns matching file names (not directorie...
High-Level Design
An abstract Filter with matches(file). Leaf filters check one attribute. Composite filters And, Or, Not hold children, and operator overloads (&, |, ~) make queries readable. Search is...