Resolve a cd Target to an Absolute Path
Updated
Problem statement
In a terminal, cd moves you to another directory. You are in the directory cwd, written as an absolute path such as /home/ana. The command is cd path. Return the absolute path of the directory you end up in.
If path starts with '/', the walk starts at the root directory /. Otherwise it starts at cwd. Then cut path at every '/' and handle the pieces from left to right:
- An empty piece (from a doubled or trailing slash) or
.leaves you where you are. ..moves up to the parent directory. At the root there is no parent, so you stay at/.- Any other piece is a directory name, and you move into it. This includes names made of dots, such as
..., and names that start with a dot, such as.git.
Every directory you name is assumed to exist. Write the answer with one '/' before each name and no slash at the end. The root is "/".
Function
changeDirectory(cwd: String, path: String) → StringExamples
Example 1
cwd = "/home/ana/docs"path = "../photos/2024"return = "/home/ana/photos/2024"The path does not start with /, so it starts from /home/ana/docs. .. moves up to /home/ana, then photos and 2024 move down, giving /home/ana/photos/2024.
Example 2
cwd = "/srv/app"path = "/etc//nginx/./sites/"return = "/etc/nginx/sites"The path starts with /, so the current directory is ignored and the walk starts at the root. The empty piece between the two slashes, the . piece and the slash at the end change nothing. The result is /etc/nginx/sites.
Example 3
cwd = "/a"path = "../../.../b/.."return = "/..."The first .. moves from /a up to /. The second .. tries to go above the root, so it stays at /. ... is an ordinary directory name, so the walk enters /..., then b, then .. comes back out. The result is /....
Constraints
1 <= cwd.length, path.length <= 2000cwdandpathcontain only English letters, digits,'_','.'and'/'.cwdis"/", or'/'followed by one or more names with one slash between each pair; it never ends with a slash. No name incwdis"."or"..".
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Parse a Partial JSON StreamListen Labs · PHONE SCREEN
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Remove K DigitsProcol · PHONE SCREEN
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Inverse-Depth Nested List SumLinkedIn · PHONE SCREENLinkedIn interview questions
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Run a Stack Language InterpreterJane Street · PHONE SCREENJane Street interview questions