71. Simplify Path
mediumAsked at MetaGiven an absolute Unix-style path, return the canonical simplified form. Meta asks this to test whether you reach for stack-based processing of path segments and handle the '..', '.', and consecutive-slash edge cases correctly.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Meta loops.
- Glassdoor (2026-Q1)— Meta E3/E4 onsite reports cite this as a recurring string-processing problem.
- Blind (2025-10)— Recurring Meta interview question.
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: The path starts with a single slash '/', any two directories are separated by a single slash '/', the path does not end with a trailing '/', and only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..'). Return the simplified canonical path.
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. Stack-based segment processing (optimal)
Split on '/'. For each segment: '..' pops the stack; '.' and '' (empty from //) skip; otherwise push. Join with '/'.
- Time
- O(n)
- Space
- O(n)
function simplifyPath(path) {
const stack = [];
const segs = path.split('/');
for (const seg of segs) {
if (seg === '' || seg === '.') continue;
if (seg === '..') stack.pop();
else stack.push(seg);
}
return '/' + stack.join('/');
}Tradeoff: Single pass over segments. The stack naturally handles nested '..' because each pop corresponds to ascending one directory. Empty stack at the end correctly produces '/'.
Meta-specific tips
Meta interviewers grade this on edge-case discipline. The four cases — empty segment (from //), '.', '..', and normal — must each be handled. State all four out loud before coding. Forgetting that '..' on an empty stack is a NO-OP (not an error) is the classic bug.
Common mistakes
- Throwing on '..' when the stack is empty — should just be a no-op.
- Forgetting to skip empty segments (from consecutive slashes '//').
- Adding a trailing slash to the output.
- Returning empty string when the simplified path is just root ('/').
- Using string concatenation in a loop instead of joining at the end.
Follow-up questions
An interviewer at Meta may pivot to one of these next:
- What if the path is relative (no leading '/')?
- What about Windows paths with backslashes and drive letters?
- What if you needed to handle symbolic links (deferred resolution)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why split on '/' instead of walking char-by-char?
Split gives us a list of meaningful segments directly. Char-by-char works but requires more bookkeeping. Same complexity; split is cleaner.
What's the most-common bug?
Failing to handle '..' on an empty stack — many candidates throw an error or return early. The correct behavior is to no-op because there's no parent of root.
Practice these live with InterviewChamp.AI
Drill Simplify Path and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →