In-Memory Filesystem Navigation
Updated
Problem statement
Build an in-memory directory tree that can change, and run a list of shell-like commands on it in order.
Each command has one of these forms:
pwdadds the current directory's canonical absolute path to the result. That is the full path from the root, with no.or..in it.mkdir PATHcreates the last directory named inPATH. The path may be absolute or relative and may contain.and... It has no wildcard, and its parent already exists. If the directory already exists, do nothing.cd PATHmoves to a new current directory once the whole path is resolved. The path may be absolute or relative. It may contain plain directory names,.,.., and*.
An absolute path starts at /. A relative path starts at the current directory. Because of *, a path can match more than one directory, so resolve it one segment at a time and keep a set of candidate directories:
.leaves each candidate where it is...moves each candidate to its parent. The root stays at the root.*matches any one child directory, so it covers exactly one level. Several*segments match several levels.
Every cd pattern matches at least one existing directory. If it matches several, pick the one whose canonical absolute path is lexicographically smallest. A cd changes the current directory only after the whole path is resolved, never partway through.
Return the paths printed by pwd, in command order.
Function
simulateFileSystem(commands: String[]) → String[]Examples
Example 1
commands = ["mkdir /home","mkdir /home/alice","cd /home/alice","pwd","cd ..","pwd"]return = ["/home/alice","/home"]After the directories are made, cd /home/alice moves there, so the first pwd gives /home/alice. Then cd .. moves to its parent, so the second pwd gives /home.
Example 2
commands = ["mkdir /teams","mkdir /teams/alpha","mkdir /teams/beta","mkdir /teams/alpha/docs","mkdir /teams/beta/docs","cd /teams/*/docs","pwd","cd .././..","pwd"]return = ["/teams/alpha/docs","/teams"]/teams/*/docs matches both /teams/alpha/docs and /teams/beta/docs. The lexicographically smaller one is /teams/alpha/docs, so cd goes there and pwd prints it. From there, .././.. goes up a level, stays put, then goes up again, ending at /teams.
Example 3
commands = ["mkdir /a","mkdir /a/x","mkdir /a/z","mkdir /a/x/y","mkdir /a/z/y","cd /a","cd */*","pwd","cd ../../..","pwd"]return = ["/a/x/y","/"]From /a, the relative path */* matches /a/x/y and /a/z/y. /a/x/y is lexicographically smaller, so pwd prints it. Then ../../.. takes three parent steps: to /a/x, to /a, then to the root. So the second pwd prints /.
Constraints
1 <= commands.length <= 2000- Commands use exactly
pwd,mkdir PATH, orcd PATH. - Directory names contain
1to20lowercase English letters. - Every path is canonical in shape: it has no trailing slash except
/and no consecutive slashes. - A
mkdirpath ends in a literal directory name, contains no*, and its parent exists. - Every
cdpath matches at least one existing directory. - At most
2000directories are created, and each path has at most200segments. - The total length of all command strings 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
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Read Across Sound TracksAirbnb · PHONE SCREENAirbnb interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Reference-Counted Deduplicating File SystemCitadel · PHONE SCREENCitadel interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Text Document HistoryNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- String Copy and Move LifecycleSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions