Quick Overview

Implement simplified Unix-style cd path resolution for absolute and relative inputs containing repeated separators, dot components, and parent-directory steps. The challenge focuses on component semantics, root boundaries, empty arguments, exact normalization, and linear processing of long paths without filesystem lookups.

Resolve a cd Path Against the Current Directory

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Resolve a cd Path Against the Current Directory Implement path resolution for a simplified Unix-like `cd` command. `cwd` is a normalized absolute path. `argument` may be absolute or relative and may contain repeated `/` separators, `.` components, and `..` components. Resolving `..` at the root keeps the result at the root. Return a normalized absolute path with no trailing slash unless the result is `/`. Do not implement symbolic links, environment variables, `~`, wildcard expansion, or filesystem existence checks. ## Function Signature ```python def resolve_cd(cwd: str, argument: str) -> str: ... ``` ## Constraints - `1 <= len(cwd), len(argument) <= 200_000` - `cwd` starts with `/` and is already normalized. - Empty components created by repeated separators are ignored; every ordinary component contains non-slash characters. - An empty string argument leaves the current directory unchanged. Any argument beginning with `/` starts from the root, so `/` and `////` both resolve to `/`. ## Examples ```text Input: cwd = "/home/alex/projects", argument = "../docs/./api" Output: "/home/alex/docs/api" ``` ```text Input: cwd = "/a/b", argument = "/x//y/../../z" Output: "/z" ``` ```text Input: cwd = "/", argument = "../../../tmp" Output: "/tmp" ```

Overview: Implement simplified Unix-style cd path resolution for absolute and relative inputs containing repeated separators, dot components, and parent-directory steps. The challenge focuses on component semantics, root boundaries, empty arguments, exact normalization, and linear processing of long paths without filesystem lookups.

Read the full Meta Software Engineer interview experience this question came from

Implement path resolution for a simplified Unix-like `cd` command. `cwd` is a normalized absolute path. `argument` may be absolute or relative and may contain repeated `/` separators, `.` components, and `..` components. Resolving `..` at the root keeps the result at the root. Return a normalized absolute path with no trailing slash unless the result is `/`. Do not implement symbolic links, environment variables, `~`, wildcard expansion, or filesystem existence checks. Only a component that is exactly `..` moves up one directory and only a component that is exactly `.` is a no-op; components such as `...`, `..a`, or `a.b` are ordinary directory names. ## Examples Example 1: ```text Input: cwd = "/home/alex/projects", argument = "../docs/./api" Output: "/home/alex/docs/api" ``` Example 2: ```text Input: cwd = "/a/b", argument = "/x//y/../../z" Output: "/z" ``` Example 3: ```text Input: cwd = "/", argument = "../../../tmp" Output: "/tmp" ```

Constraints

  • 1 <= len(cwd), len(argument) <= 200_000
  • `cwd` starts with `/` and is already normalized.
  • Empty components created by repeated separators are ignored; every ordinary component contains non-slash characters.
  • An empty string argument leaves the current directory unchanged. Any argument beginning with `/` starts from the root, so `/` and `////` both resolve to `/`.

Examples

Input: ('/home/alex/projects', '../docs/./api')

Expected Output: '/home/alex/docs/api'

Explanation: Worked example 1: '..' pops projects, '.' is a no-op, then docs and api are entered.

Input: ('/a/b', '/x//y/../../z')

Expected Output: '/z'

Explanation: Worked example 2: an absolute argument ignores cwd; repeated separators are ignored and the two '..' components unwind x/y before entering z.

Hints

  1. Split both paths into components separated by runs of `/`, and classify each component before doing any string surgery: ordinary name, `.`, or `..`.
  2. A stack of directory names models the current location: ordinary names push, `..` pops when the stack is non-empty, and `.` changes nothing. Start the stack from `cwd` only when `argument` is relative.
  3. Only a component that is exactly `..` goes up. `...`, `..a`, and `a.b` are ordinary directory names, so compare whole components, never prefixes.

Loading coding console...

Show the approach

Approach

The reference treats a path as a sequence of components separated by runs of '/'. If argument starts with '/', resolution begins from an empty stack (the root); otherwise the stack is seeded with the components of cwd, which is already normalized so it contains no '.' or '..' entries. Each component of argument is then applied in order: an empty component (from repeated separators or a trailing slash) and '.' are ignored, '..' pops the top directory when one exists (popping at the root is a no-op, which keeps the result at '/'), and any other component -- including look-alikes such as '...' or '..a' -- is pushed as an ordinary directory name. The answer is '/' followed by the stack joined with '/', which is '/' itself when the stack is empty. Every character of both inputs is inspected a constant number of times and the stack plus output are linear in the input, so time and space are O(len(cwd) + len(argument)).

Time complexity:
O(len(cwd) + len(argument))
Space complexity:
O(len(cwd) + len(argument))