FastPrepCanonical Directory After CD Commands
Problems/Canonical Directory After CD Commands

Canonical Directory After CD Commands

Meta logoMeta● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta onsite interview. The report names a harder CD path version of Simplify Path but does not give the added rule or the interface. Here, you process relative and absolute directory changes and return the final cleaned path. Our version is about 70% the same as the reported question.

You start in the root directory /. Each string in commands is one command: a path to move to, like the argument of cd. Apply the commands one by one, then return the final directory as a canonical absolute path.

  • A command that starts with / starts from the root. Any other command starts from the current directory.
  • Split a command at each /. Skip empty parts and parts that are exactly .. A part that is exactly .. moves up to the parent directory. The root has no parent, so you stay at the root.
  • Every other part is a directory name, even ... and ..... You only follow the names. You never check whether a directory really exists.

Canonical means the path starts with one slash, has one slash between names, and has no slash at the end unless it is just the root. If there are no commands, return /.

Function

cdPath(commands: String[]) → String

Examples

Example 1

commands = ["projects/ml", "../vision", "/tmp//a/./", "../../logs"]return = "/logs"

The first command moves to /projects/ml. The second goes up one level and into vision, giving /projects/vision. The third command starts with /, so it starts again from the root. Its empty parts and its . are skipped, which gives /tmp/a. In the last command, the two .. parts go up to /tmp and then to the root, and then logs is added. The answer is /logs.

Example 2

commands = ["../../", "a/../b", "./c"]return = "/b/c"

The first command has two .. parts, but you are at the root, so you stay there. The second command goes into a, back up, and then into b. The third command skips . and goes into c. The answer is /b/c.

Example 3

commands = ["x/.../y", "../z"]return = "/x/.../z"

The first command goes into x, then into a directory named ..., then into y. Three periods make a normal name, not a parent step like ... The second command goes up from y and into z, so the answer is /x/.../z.

Example 4

commands = []return = "/"

There are no commands, so you stay at the root and the answer is /.

Constraints

  • 0 <= commands.length <= 10000.
  • Each command is a nonempty string. All commands together have at most 100000 characters.
  • Commands use only English letters, digits, underscores, periods, and slashes.

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public String cdPath(String[] commands) {
        // Write your code here.
    }
}
Checking account…