54. Simplify Path
mediumAsked at SalesforceConvert a Unix-style file path to its canonical form. Salesforce uses this to test stack-based string processing — analogous to their record-hierarchy canonicalization.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses path-canonicalization in their content-management module.
- LeetCode Discuss (2025)— Tests split + stack pattern.
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: starts with '/', any two directories are separated by '/', does not end with '/' (unless root). It should also be the shortest string representing the absolute path. 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. Manual character walk
Walk char by char; build segments; handle . and .. inline.
- Time
- O(n)
- Space
- O(n)
// Verbose. Salesforce prefers split + stack.Tradeoff: Too much state. Split first.
2. Split on '/' and stack-process
Split by '/'. For each segment: '..' pops stack; '.' or '' ignored; else push. Join with '/'.
- Time
- O(n)
- Space
- O(n)
function simplifyPath(path) {
const stack = [];
for (const part of path.split('/')) {
if (part === '' || part === '.') continue;
if (part === '..') stack.pop();
else stack.push(part);
}
return '/' + stack.join('/');
}Tradeoff: Concise and clear. Split handles repeated slashes naturally (gives empty segments which we skip).
Salesforce-specific tips
Salesforce uses path-canonicalization in their content-management module (folder hierarchies, library structures). They grade on whether you skip empty segments AND single dots. Bonus signal: discuss the '..' behavior at root — popping an empty stack should be a no-op, which is what stack.pop() does naturally on empty arrays.
Common mistakes
- Pushing the '/' itself onto the stack — get '//' separator bugs.
- Returning '/' + stack.join('/') when stack is empty — gives just '/', which is correct, but easy to miss.
- Treating '...' (three dots) as '..' — only '..' is the parent reference; '...' is a valid filename.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Decode URL paths.
- Implement cd command behavior (relative vs absolute).
- Resolve symbolic links given a map.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does split('/') handle empty segments correctly?
Repeated slashes (//) produce empty strings between them. Skipping empty strings collapses runs of slashes naturally.
What happens at root with '..'?
stack.pop() on an empty stack is a no-op in JavaScript. Result is still '/' which is the correct canonical root.
Practice these live with InterviewChamp.AI
Drill Simplify Path and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →