53. Simplify Path
mediumAsked at VercelGiven an absolute Unix file path, simplify it (canonical form). Vercel asks this directly — they literally need this logic in their edge router for canonicalizing URL paths and resolving `..` segments.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel routing-team onsite; literally their bread and butter.
- Blind (2026-Q1)— Reported as the most likely Vercel-specific problem.
Problem
Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. The canonical path should have the following format: starts with /, no trailing /, no '.' or '..', and no multiple consecutive /.
Constraints
1 <= path.length <= 3000path consists of English letters, digits, period '.', slash '/' or '_'.path is a valid absolute Unix path.
Examples
Example 1
path = "/home/""/home"Example 2
path = "/../""/"Example 3
path = "/home//foo/""/home/foo"Example 4
path = "/a/./b/../../c/""/c"Approaches
1. Regex normalization passes
Repeatedly apply regex substitutions for // and .. until stable.
- Time
- O(n^2) worst case
- Space
- O(n)
// Tempting but error-prone — `..` with no parent, edge cases like `/...` (valid filename) break.Tradeoff: Hard to get right. The stack version is the textbook answer.
2. Stack with split (optimal)
Split on '/'. Walk segments: '' and '.' skip. '..' pops if non-empty. Else push. Join with '/' and prepend.
- Time
- O(n)
- Space
- O(n)
function simplifyPath(path) {
const stack = [];
for (const seg of path.split('/')) {
if (seg === '' || seg === '.') continue;
if (seg === '..') stack.pop();
else stack.push(seg);
}
return '/' + stack.join('/');
}Tradeoff: Five-line solution. The split handles consecutive slashes by producing empty strings, which are skipped. Pop on empty stack is a no-op in JS — already correct for `/..`.
Vercel-specific tips
Vercel grades this on whether you reach for split + stack immediately. Bonus signal: noting that '...' or '....' are VALID filenames (don't treat them as pseudo-parent), and that `stack.pop()` on empty is safely a no-op in JS. They may follow up with relative paths (no leading slash) or Windows-style backslashes.
Common mistakes
- Treating '...' as '..' — wrong, it's a regular filename.
- Using replace on // — works but doesn't handle the `..` walk.
- Forgetting to prepend the leading '/' — `/home` not `home`.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Simplify a relative path (no leading slash).
- Add support for `~/` expansion to a home directory.
- Streaming variant — segments arrive one at a time.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why split + stack?
split('/') turns the path into a list of segments; the stack naturally models 'go up one level' (pop) and 'go into a directory' (push). Both empty segments (consecutive slashes) and '.' are no-ops.
What about '/...'?
Three dots is a regular filename, not a special token. Only '.' (current) and '..' (parent) are special. Your code should push '...' normally.
Practice these live with InterviewChamp.AI
Drill Simplify Path and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →