Find a File in a Directory Snapshot
Updated
Problem statement
For this exercise, assume a directory is supplied as a snapshot of its files. rootDir is its absolute path, and every value in relativePaths is the relative path of one regular file below that root, including files in nested directories.
Find files whose last path segment is exactly fileName. Return the matching absolute path that comes first in lexicographic order. If no file matches, return the empty string.
Join the root and relative path with exactly one slash. Matching is case-sensitive and compares the entire filename, including its extension. No real filesystem access is required.
Function
findAbsolutePath(rootDir: String, relativePaths: String[], fileName: String) → StringExamples
Example 1
rootDir = "/work"relativePaths = ["z/log.txt","a/log.txt","notes.md"]fileName = "log.txt"return = "/work/a/log.txt"Both matching files end in log.txt; /work/a/log.txt is lexicographically smaller.
Example 2
rootDir = "/"relativePaths = ["main.py"]fileName = "main.py"return = "/main.py"Joining the root with the relative file gives /main.py.
Example 3
rootDir = "/data"relativePaths = ["x/a.txt"]fileName = "b.txt"return = ""No basename equals b.txt.
Constraints
0 <= relativePaths.length <= 10^4; paths are unique.- The root is
/or a valid absolute path without a trailing slash. Relative paths have no leading or trailing slash. - Segments contain lowercase English letters, digits, dots, underscores or hyphens, and are never
.or... - Each path contains at most
200characters.fileNameis one nonempty valid segment.
More Cresta interview questions
- Decode Words with Anagram KeysOA · Seen Oct 2026
- Leaves Remaining After WindOA · Seen Oct 2026
- Maximum Suffix–Prefix OverlapOA · Seen Oct 2026
- Particle Moves to TargetOA · Seen Jul 2026
- Rightward Bacteria ConsumptionOA · Seen Jul 2026
- Top Three Companies by ProfitOA · Seen Jul 2026
- Peak Chat Load Across AgentsPHONE SCREEN · Seen Nov 2026
- File Storage ServicePHONE SCREEN · Seen Nov 2026
- Dynamic Least-Loaded Job DispatcherPHONE SCREEN · Seen Oct 2026
- Feature Flag Evaluation EngineONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company interview questions
- Flat JSON Value DifferencesChubb · OAChubb interview questions
- Road Trip (Analyst Program :PBlackRock · OABlackRock interview questions
- Get Search ResultsJP Morgan Chase · OAJP Morgan Chase interview questions
- Lexicographic Word FrequenciesUpstart · OAUpstart interview questions
- Email Log Processing, Grouping, and SortingStripe · OAStripe interview questions
- Most Frequent CharacterMicrosoft · OAMicrosoft interview questions
- Get Maximum Information GainAmazon · OA