71. Simplify Path
mediumCanonicalize a Unix-style file path with '.' and '..' segments. A practical, real-world stack problem that's a favorite of platform and infra interviewers.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an absolute path for a Unix-style file system, which begins with a slash '/', transform this absolute path into its simplified canonical path. In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//' or '///') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names. The simplified canonical path should follow these rules: it must start with a single slash '/'; directories within it should be separated by exactly one slash '/'; it should not end with a slash '/', unless it is the root directory; it should exclude any single or double periods used to denote the current or parent directory. 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"Explanation: Note that there is no trailing slash after the last directory name.
Example 2
path = "/home//foo/""/home/foo"Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Example 3
path = "/a/./b/../../c/""/c"Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Split the path on '/'. Now you have a list of segments to process left to right.
Hint 2
Three segment cases to skip: empty string (from '//'), '.', and '..' on an empty stack.
Hint 3
'..' pops one directory off the stack. Any other non-empty segment is a directory name — push it.
Hint 4
Join the final stack with '/' and prepend one '/'. Empty stack means root: return '/'.
Solution approach
Reveal approach
Split path on '/' and iterate. Maintain a stack of directory names. For each segment: skip if it's empty or '.'; if it's '..', pop the stack (only if non-empty); otherwise push it. After processing, join the stack with '/' separators and prepend '/'. If the stack is empty, return '/'. O(n) time and O(n) space. Two subtleties: don't treat '...' as a parent directory (only exact '..' is special), and '..' on an empty stack is a no-op (you can't go above root).
Complexity
- Time
- O(n)
- Space
- O(n)
Related patterns
- stack
- string
Related problems
- 394. Decode String
- 20. Valid Parentheses
- 224. Basic Calculator
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Meta
- Amazon
- Microsoft
- Bloomberg
Practice these live with InterviewChamp.AI
Drill Simplify Path and Stacks problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →