Search a Directory Tree for a File and Return Its Absolute Path in Python
Company: Cresta
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Online Assessment
You are given the path of a directory. Write a Python function that searches that directory and all of its subdirectories for the file that meets a given requirement, and returns that file's absolute path.
This is a practical programming task rather than an algorithm puzzle: you are expected to use the standard library's filesystem tools, such as `os` and `pathlib`, fluently. 30 minutes are allotted to it.
A possible interface, in which the file is identified by its name (adapt it to the requirement you are given):
```python
def find_file(root: str, target_name: str) -> str | None:
"""Return the absolute path of the file named target_name under root, or None."""
```
```hint Let the library walk
The file can be at any depth. Decide which standard-library call visits every subdirectory for you, and what it hands you at each level.
```
```hint Joined is not absolute
Joining a relative root with a file name still gives a relative path. Decide where the path becomes absolute, and whether symbolic links should be resolved on the way.
```
### Constraints and Clarifications
- The search covers the given directory and every directory below it.
- The returned path must be absolute, even when `root` is passed as a relative path such as `"."`.
- Not specified: the exact requirement that identifies the file. The interface above assumes an exact file name; be ready to swap in another criterion (an extension, a pattern, a size, the file's contents) without rewriting the traversal.
### Clarifying Questions
- What identifies the file: an exact name, an extension or pattern, or something inside it?
- If several files match, should the function return the first one found, the one closest to the root, or all of them, and in what order?
- What should happen when nothing matches: return `None`, raise an error, or return an empty list?
- Should the search follow symbolic links to directories, and should the returned path have its symbolic links resolved?
- How should directories you are not allowed to read be handled, and should hidden directories be searched?
### What a Strong Answer Covers
- A traversal of the whole tree with standard-library tools rather than hand-built path strings
- Correct absolute paths for relative roots, with a stated choice between keeping and resolving symbolic links
- A deliberate, deterministic rule for several matches and a defined result for no match
- Robustness: a missing or non-directory root, unreadable subdirectories, symbolic-link cycles
- Cost proportional to the entries visited, an early exit once the answer is known, and a quick test against a temporary directory
### Follow-up Questions
- Return every match instead of one, sorted so that the output is identical on every operating system.
- The tree holds millions of files on a network drive. What makes the search slow, and how do you speed it up?
- Find the file by a string it contains rather than by its name. What changes, and what do you guard against when opening files?
- How would you unit-test this function without depending on the filesystem layout of the machine it runs on?
Overview: Write a Python function that searches a directory and all of its subdirectories for a file matching a requirement and returns the file's absolute path. It tests practical use of os and pathlib, deterministic handling of multiple or missing matches, relative roots, symbolic links and unreadable directories.
Read the full Cresta Software Engineer interview experience this question came from