Design a Recursive File-Search API
Company: Amazon
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: HR Screen
## Scenario
Design a backend API that recursively searches a directory tree for entries whose file name exactly matches a requested name.
Required behavior:
- If the starting path is `null` or does not identify a directory, return the error `Current search location is not a directory`.
- If the directory is empty or no matching file exists anywhere below it, return `File not found: name`, substituting the requested name.
- Otherwise return every matching regular-file path in deterministic lexicographic order.
Explain validation, traversal, symbolic-link policy, permission errors, time and space complexity, and how the API avoids exposing arbitrary server files.
### Constraints & Assumptions
- The requested root must resolve beneath a configured allowlisted base directory.
- Do not follow symbolic links in the baseline design.
- A permission error is returned separately from not-found; it must not be silently interpreted as an empty subtree.
- The tree may be deep enough to make recursive call stacks unsafe.
### Clarifying Questions to Ask
- Is matching exact and case-sensitive? Yes in the baseline.
- Should directories with the requested name count? No, regular files only.
- Are all matches returned or just the first? All.
- How should concurrent file changes be handled? Best-effort snapshot semantics with documented race behavior.
```hint Treat paths as untrusted input
Resolve and normalize the root, then verify it remains beneath the configured base before traversing.
```
### What a Strong Answer Covers
- Exact validation and error distinctions required by the prompt.
- Iterative DFS or BFS, deterministic output sorting, and cycle avoidance through the no-symlink rule.
- Permission and time-of-check/time-of-use behavior.
- Root allowlisting, authorization, result limits, cancellation, and audit logging for a service endpoint.
### Follow-up Questions
1. How would you safely support symbolic links without cycles or base-directory escape?
2. When would an index be preferable to walking the filesystem per request?
3. How would you stream results while preserving deterministic order?
Quick Answer: Design a secure recursive file-search API with exact error messages, deterministic result ordering, explicit symbolic-link and permission policies, and protection against arbitrary server-file access.