71. Simplify Path
Asked at OpenAI, Roblox
Difficulty
Medium
Time
O(n)
Space
O(n)
Pattern
String
Companies
2
Problem
You are given an absolute Unix file path. It always starts with /. Your job is to turn it into its simplified canonical form.
A path is built from parts separated by /. Some parts are special: a single . means "stay in the current directory", and .. means "go up to the parent directory". Several slashes in a row count as just one slash. Any other run of dots, like ... or ...., is a normal directory name, not a special symbol.
The result must start with exactly one /, have exactly one / between directory names, and must not end with / — unless the path is just the root /. It must not contain . or .. parts. If .. is used at the root, you cannot go above the root, so it has no effect.
Reading a path part by part
Constraints
- ●
1 <= path.length <= 3000 - ●
path consists of English letters, digits, period '.', slash '/' or '_' - ●
path is a valid absolute Unix path
Examples
path = "/home/""/home"path = "/home//foo/""/home/foo"path = "/home/user/Documents/../Pictures""/home/user/Pictures"path = "/../""/"Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| OpenAI | Medium | View all OpenAI questions → |
| Roblox | Medium | View all Roblox questions → |
How to Think About It
Split the path by "/" delimiter to get individual components
Use a stack to process each component sequentially
Push valid directory names onto the stack
Pop from stack when encountering ".." (go up one directory)
Ignore "." and empty components, join stack with "/" for result
Optimal Approach
Split the path by "/" to get components. Use a stack to track directory names. For each component: if it is "." or empty, skip; if it is "..", pop from stack if non-empty; otherwise push the component onto the stack. Join the stack with "/" and prepend a leading slash to form the simplified absolute path.
Stack of directory names
What Trips People Up in Real Interviews
Clarify how to handle edge cases like multiple consecutive slashes
Explain that ".." at root level should not go above root
Discuss why a stack is natural for this directory traversal problem
Mention the importance of not adding trailing slashes
Consider how the solution handles paths like "/../" or "/./"
Solution Code
O(n) timedef simplifyPath(path):
stack = []
for component in path.split("/"):
if component == "..":
if stack:
stack.pop()
elif component and component != ".":
stack.append(component)
return "/" + "/".join(stack)Frequently Asked Questions
What is the Simplify Path problem?
Given a Unix-style absolute file path, simplify it by resolving dots, double dots, and multiple slashes. The simplified path must be absolute, start with a single slash, and have no trailing slash unless it is the root directory.
How do you solve Simplify Path?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Simplify Path?
Simplify Path is asked at OpenAI, Roblox. It is a medium difficulty problem.
What are common mistakes on Simplify Path?
- Clarify how to handle edge cases like multiple consecutive slashes
- Explain that ".." at root level should not go above root
- Discuss why a stack is natural for this directory traversal problem
- Mention the importance of not adding trailing slashes
- Consider how the solution handles paths like "/../" or "/./"