FastPrepResolve a cd Target to an Absolute Path
Problems/Resolve a cd Target to an Absolute Path

Resolve a cd Target to an Absolute Path

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A report from February 2023 says that in the coding round with an engineer, the candidate had to work out which directory a cd command lands in. The inputs were the directory they are in now and the place they asked to go.

The report gives no examples, no rules for the special pieces, and no limits. Here the current directory comes as a clean absolute path. A path that starts with a slash begins at the root. A single dot and empty pieces do nothing. Two dots go up one level and stop at the root. Every other piece, even three dots, is a plain name. Directories are assumed to exist. The examples are our own.

Our version is about 80% the same as the reported question.

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) → String

Examples

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 <= 2000
  • cwd and path contain only English letters, digits, '_', '.' and '/'.
  • cwd is "/", or '/' followed by one or more names with one slash between each pair; it never ends with a slash. No name in cwd is "." 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

OpenAI interview experiences

  • OpenAI OA: What It Tests (2026)
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public String changeDirectory(String cwd, String path) {
        // Write your code here.
    }
}
Checking account…