Shell Path Autocomplete
Updated
Problem statement
Implement path completion similar to pressing Tab in a shell.
You are given absolute file paths, an absolute current directory, and the text already typed by the user. If the typed text is relative, resolve it beneath the current directory. If it begins with /, use it as an absolute prefix.
Find every file path that starts with the resolved prefix. If at least one path matches, return the longest common prefix of all matching absolute paths. Completion may advance through one or more directory components when every match shares them. If no file matches, return the resolved prefix unchanged.
Function
autocompletePath(paths: String[], currentDirectory: String, input: String) → StringExamples
Example 1
paths = ["/home/foo/f1.txt","/home/foo/f1_2.txt","/home/foo/README","/home/foo/bar/f1.txt","/home/foo/bar/README"]currentDirectory = "/home/foo"input = "f"return = "/home/foo/f1"The two matching files agree through f1, then diverge at . and _.
Example 2
paths = ["/work/folder/file1","/work/folder/file2","/work/folder/file/new","/work/other.txt"]currentDirectory = "/work"input = "f"return = "/work/folder/file"Every match is inside folder, so completion enters that directory and continues through the shared file prefix.
Example 3
paths = ["/work/folder/file1","/work/folder/file2","/work/folder/image.png"]currentDirectory = "/tmp"input = "/work/folder/fi"return = "/work/folder/file"An absolute input is completed without using the current directory.
Constraints
1 <= paths.length <= 200000.- The total length of all paths is at most
10^6. - Every path and
currentDirectoryis absolute and normalized: it contains no repeated slash,., or..component. currentDirectoryhas no trailing slash unless it is/.inputcontains no.or..path component.
More Figma Phone Screen questions
- Recursive Display SortPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Route Graph Reachability with a Broken LinkPHONE SCREEN · Seen Oct 2026
- Rich Text Parser Part 3 (Depth Tokens)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rich Text Parser Part 2 (Parse Nested Groups)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rich Text Parser Part 1 (Validate Tokens)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Layer History with Batch Undo and RedoOA · PHONE SCREEN · Seen Sep 2026
- Table Selection and Color UpdatesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Topmost Accessible NodesPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2025
- Intersection of Two RectanglesPHONE SCREEN · Seen Aug 2024
- Sparse Canvas Paint HistoryPHONE SCREEN · Seen Sep 2023
Similar questions at other companies
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Search Suggestions with a Prefix TreeCitadel · ONSITE INTERVIEWCitadel interview questions
- Remove Prefix StringsRoblox · PHONE SCREENRoblox interview questions
- Every Distinct Substring in OrderAdobe · PHONE SCREENAdobe interview questions
- Top K Query SuggestionsMicrosoft · PHONE SCREENMicrosoft interview questions
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions
- Suggested ProductsSquarepoint Capital · OASquarepoint Capital interview questions