55. Simplify Path
mediumAsked at RedditSimplify a Unix-style absolute file path. Reddit uses this string + stack problem to test parsing — the same pattern they apply when canonicalizing /r/sub/comments/abc/comment/xyz URLs for cache keying.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone screen, common stack/string 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. In a Unix-style file system: '.' refers to the current directory; '..' refers to the directory up a level; any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. The canonical path should have format: starts with '/', no trailing '/', single slashes between components, no '.' or '..'.
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. Repeated regex replace
Repeatedly replace /./ -> /, /xxx/../ -> /, // -> /.
- Time
- O(n^2)
- Space
- O(n)
// Anti-pattern: regex replace until stable.Tradeoff: Quadratic and fragile (regex order matters).
2. Split + stack (optimal)
Split on '/'. For each token: skip empty/'.', pop on '..', else push. Rejoin.
- Time
- O(n)
- Space
- O(n)
function simplifyPath(path) {
const stack = [];
for (const token of path.split('/')) {
if (token === '' || token === '.') continue;
if (token === '..') stack.pop();
else stack.push(token);
}
return '/' + stack.join('/');
}Tradeoff: Single pass with a stack. Reddit's URL canonicalization library uses this exact pattern.
Reddit-specific tips
Reddit interviewers grade on the stack reflex and on handling all four cases (empty, '.', '..', name). Bonus signal: connect to comment-tree URL canonicalization where each segment is a comment slug.
Common mistakes
- Popping when stack is empty (in JS, .pop on empty is undefined; harmless but check requirements).
- Forgetting the leading slash in the result.
- Using string concatenation in a loop (O(n^2) in some languages).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- URL encode/decode.
- Longest absolute file path (LC 388).
- Mini parser (LC 385) — similar token-based parsing.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
What if '..' is at the root?
Stack is empty; pop is a no-op. Result remains '/'.
Are there variant separators?
Unix only. For Windows-style, you'd normalize backslashes too.
Practice these live with InterviewChamp.AI
Drill Simplify Path and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →