MediumStringStackUpdated Sep 2026

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

parts of /home/user/Documents/../PictureshomeuserDocuments..Pictures01234The '..' part cancels the name right before it, so Documents isremoved.
Each part between slashes is either a real name, a '.' (skip) or a '..' (go up one level).

Constraints

  • ●1 <= path.length <= 3000
  • ●path consists of English letters, digits, period '.', slash '/' or '_'
  • ●path is a valid absolute Unix path

Examples

Example 1
Inputpath = "/home/"
Output"/home"
WhyThe only real part is home. The trailing slash is dropped, so we get /home.
Example 2
Inputpath = "/home//foo/"
Output"/home/foo"
WhyThe double slash between home and foo collapses to one slash, and the trailing slash is removed.
Example 3
Inputpath = "/home/user/Documents/../Pictures"
Output"/home/user/Pictures"
WhyThe .. cancels the Documents part, so Pictures takes its place under /home/user.
Example 4
Inputpath = "/../"
Output"/"
WhyYou cannot go above the root, so the .. does nothing and the path stays at the root.

Asked At

2 companies
CompanyDifficulty
OpenAIMediumView all OpenAI questions →
RobloxMediumView all Roblox questions →

How to Think About It

1

Split the path by "/" delimiter to get individual components

2

Use a stack to process each component sequentially

3

Push valid directory names onto the stack

4

Pop from stack when encountering ".." (go up one directory)

5

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

stackhomeuserDocuments← topAfter reading home, user, Documents — three names areon the stack.stackhomeuser← topThe '..' pops Documents, so we are back at /home/user.stackhomeuserPictures← topPictures is a normal name, so it is pushed on top.skip: "" and "."pop: ".." (if stack not empty)push: anything elseresult = "/" + "/".join(stack)
Push real names, pop on '..', ignore '.' and empty parts. The stack at the end is the answer.
ComplexityTime O(n)Space O(n)We scan each character of the path once, and the stack can hold up to n characters worth of directory names.

What Trips People Up in Real Interviews

1

Clarify how to handle edge cases like multiple consecutive slashes

2

Explain that ".." at root level should not go above root

3

Discuss why a stack is natural for this directory traversal problem

4

Mention the importance of not adding trailing slashes

5

Consider how the solution handles paths like "/../" or "/./"

Solution Code

O(n) time
def simplifyPath(path):
    stack = []
    for component in path.split("/"):
        if component == "..":
            if stack:
                stack.pop()
        elif component and component != ".":
            stack.append(component)
    return "/" + "/".join(stack)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

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 "/./"