Find File Paths in a Directory Tree
Updated
Problem statement
You are given the full path of every directory and every file in a filesystem. directoryPaths holds the directory paths and filePaths holds the file paths. The same directory or file name can show up in different places, but no two full paths are the same.
Build the directory tree. Then return the full path of every file whose last part (the name after the final slash) equals targetName. Walk the tree with your own stack, not with recursion. Return the matching paths sorted in lexicographic order.
Function
findFilePaths(directoryPaths: String[], filePaths: String[], targetName: String) → String[]Examples
Example 1
directoryPaths = ["/home","/home/docs","/tmp"]filePaths = ["/home/docs/report.txt","/tmp/report.txt","/home/photo.jpg"]targetName = "report.txt"return = ["/home/docs/report.txt","/tmp/report.txt"]The file name report.txt appears under two different full paths: /home/docs/report.txt and /tmp/report.txt. Both match, and /home/photo.jpg does not. In lexicographic order, the answer is ["/home/docs/report.txt","/tmp/report.txt"].
Example 2
directoryPaths = ["/a","/a/b"]filePaths = ["/a/b/readme.md"]targetName = "missing.md"return = []The only file is /a/b/readme.md. Its last part is readme.md, not missing.md. No file has the requested name, so the answer is an empty list.
Constraints
1 <= directoryPaths.length + filePaths.length <= 20000- Every path is canonical, absolute, nonempty, and unique.
- Every file's parent directory appears in
directoryPaths. - The parts of a path (the names between slashes) never contain a slash.
targetNameis one nonempty part, so it has no slash either.- The total number of characters across all paths is at most
200000.
More Uber Phone Screen questions
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Find Robots by Blocker DistancesPHONE SCREEN · Seen Oct 2026
- Sliding-Window Rate LimiterPHONE SCREEN · Seen Oct 2026
- Count Connected IslandsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN · ONSITE INTERVIEW
- Serialize and Deserialize a Binary TreeNVIDIA · PHONE SCREENNVIDIA interview questions
- Inorder Tree String EquivalenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Turn a BST Into a Sorted Doubly Linked ListAdobe · PHONE SCREENAdobe interview questions
- Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions