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"
```
Quick Answer: 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.
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
- Split both paths into components separated by runs of `/`, and classify each component before doing any string surgery: ordinary name, `.`, or `..`.
- 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.
- Only a component that is exactly `..` goes up. `...`, `..a`, and `a.b` are ordinary directory names, so compare whole components, never prefixes.