Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Normalize a file path
Problem Description
Given an absolute Unix-style file path as a string, return its canonical normalized form.
Rules
- The input always starts with
/. - A single
.signifies the current directory and should be ignored. - A double dot
..signifies moving to the parent directory if possible. - Multiple consecutive slashes should be treated as a single slash.
Output Requirements
The output path must:
- Start with exactly one
/. - Contain directory names separated by a single slash.
- Not end with a trailing slash unless the result is the root path
/.
Example
- Input:
/a//b/./c/../d/ - Output:
/a/b/d
Explain your approach and implement the function.
AI interview prep
Core dilemma
Correctly interpreting and processing path components (especially '.' and '..') in sequence, while robustly handling various edge cases like multiple slashes, root directory, and '..' from root, all within reasonable time and space complexity.
Mid-level signals3
Senior signals3
Red flags3
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Normalize a file path
Problem Description
Given an absolute Unix-style file path as a string, return its canonical normalized form.
Rules
- The input always starts with
/. - A single
.signifies the current directory and should be ignored. - A double dot
..signifies moving to the parent directory if possible. - Multiple consecutive slashes should be treated as a single slash.
Output Requirements
The output path must:
- Start with exactly one
/. - Contain directory names separated by a single slash.
- Not end with a trailing slash unless the result is the root path
/.
Example
- Input:
/a//b/./c/../d/ - Output:
/a/b/d
Explain your approach and implement the function.