Design a Unix File Search
🔎 What is Unix file search? On Linux and macOS, the
findcommand walks a folder and everything inside it, and prints each file or folder that matches your conditions: "files that end in.logand are bigger than 1 MB", for example. Its cousinls -Rwalks the same tree but prints every folder with its contents, one folder at a time.
Interviewers like this problem because the hard part is not the tree. It is the conditions. The prompt almost always ends with "and make it easy to add new kinds of search", which is a direct question about how you model rules. The ls -R-style listing is the same design: the same tree and the same walk, just a different order and output. We cover both, and keep the file system itself small, since /low-level-design/file-system covers that in depth.
Requirements
"Design the Unix
findcommand. Users search a directory by name, extension, size or type, and can combine conditions. It should be easy to add new kinds of conditions later."
Clarifying Questions
You: What are we searching? A real disk, or something we build?
Interviewer: Build a tree in memory. Users add directories, and files with a size. We care about the search, not about disk access.
You: Which conditions do we need on day one?
Interviewer: Exact name, extension, file or directory, and minimum and maximum size. We will add more later, like name patterns and modified dates, so that is where we want flexibility.
You: How do conditions combine? Only AND, or also OR?
Interviewer: Both. A user might want ".txt files over 100 bytes, or anything named README.md". Assume the query arrives as a list of groups: everything in a group must hold, and any one group is enough.
You: In what order should results come back?
Interviewer: The way find prints them: a folder, then everything inside it, then the next folder. Inside a folder, go by name.
You: Does a directory have a size for minSize and maxSize?
Interviewer: No. Size conditions only match files.
You: Is the starting folder itself part of the results? And what if the user starts at a file?
Interviewer: Leave the starting folder out. If they start at a file, just test that file.
You: For the ls -R listing, what should the output look like?
Interviewer: For each folder, a header line with its path and a colon, then its children's names. Then the same for each subfolder.
The third answer shapes the whole design. With AND only, a list of conditions is enough. With AND and OR, conditions have to nest, and nesting is a tree. Ask early, because switching from a list to a tree halfway through the interview means rewriting the core.
💡 Interview tip: When the interviewer says "easy to add new kinds of X", treat it as the most important requirement on the board. Repeat it back ("so adding a condition should not change the search code") and make it the first thing you design.
Final Requirements
- The tree has a root
/.addDir(path)creates a directory and any missing parents;addFile(path, size)creates a file (and missing parents) or replaces its size. find(path, groups)returns the full paths of all entries belowpaththat match the query. Ifpathis a file, it returns that file if it matches.- Conditions:
name=(exact name),ext=(text after the last dot),type=f/type=d,minSize=/maxSize=(files only). - A query is an OR of groups, each group an AND of conditions. No groups means "match everything".
- Results come in depth-first order: an entry, then everything inside it; siblings in name order.
listRecursive(path)returns anls -Rlisting: each directory's header and child names, then its subdirectories in name order.- New kinds of conditions must not require changes to the search code.
Out of scope
- Name patterns (
*.log), timestamps, owners and permissions (all come back in Extensibility as new conditions). - Actions on results, like
-deleteor-exec. - Symbolic links, depth limits and parallel search (Extensibility).