Coding questions · Meta
Resolve a cd Path Against the Current Directory
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.
Function Signature
def resolve_cd(cwd: str, argument: str) -> str:
...
Constraints
1 <= len(cwd), len(argument) <= 200_000cwdstarts 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: cwd = "/home/alex/projects", argument = "../docs/./api"
Output: "/home/alex/docs/api"
Input: cwd = "/a/b", argument = "/x//y/../../z"
Output: "/z"
Input: cwd = "/", argument = "../../../tmp"
Output: "/tmp"
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.
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:
Input: cwd = "/home/alex/projects", argument = "../docs/./api"
Output: "/home/alex/docs/api"
Example 2:
Input: cwd = "/a/b", argument = "/x//y/../../z"
Output: "/z"
Example 3:
Input: cwd = "/", argument = "../../../tmp"
Output: "/tmp"
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 fromcwdonly whenargumentis relative. - Only a component that is exactly
..goes up....,..a, anda.bare ordinary directory names, so compare whole components, never prefixes.
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))