53. Simplify Path
mediumAsked at DatadogConvert a Unix-style absolute path into its canonical form. Datadog uses this for the stack-based path-segment pattern — same shape as their tag-prefix normalization for hierarchical metric names.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Datadog loops.
- Glassdoor (2026-Q1)— Datadog backend infra onsite.
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. 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 are treated as a single slash '/'. The canonical path should: start with a single '/', not have any '.' or '..' or trailing '/'.
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. String replacement loop
Repeatedly replace '/./', '//', and 'foo/../' until stable.
- Time
- O(n^2)
- Space
- O(n)
// Loop: replace '//' with '/', replace '/./' with '/', replace '/foo/../' with '/'.
// Quadratic and fragile.Tradeoff: Pattern-based; brittle on edge cases. Datadog will fail this.
2. Split + stack (optimal)
Split on '/'. For each segment: empty or '.' = skip; '..' = pop; else push. Join with '/' and prefix with '/'.
- Time
- O(n)
- Space
- O(n)
function simplifyPath(path) {
const stack = [];
for (const seg of path.split('/')) {
if (seg === '' || seg === '.') continue;
if (seg === '..') stack.pop();
else stack.push(seg);
}
return '/' + stack.join('/');
}Tradeoff: Single pass, clean. Datadog-canonical: the segment-stack is the same primitive they use for hierarchical-tag normalization.
Datadog-specific tips
Datadog grades on whether you recognize the stack model: directories are LIFO; '..' is pop, others are push, '.' and empty are no-ops. Articulate this before coding. Bonus: discuss what happens on a relative path (you'd seed the stack with the cwd).
Common mistakes
- Popping an empty stack on '..' at the root — silently OK in JS (returns undefined), but the canonical form is '/'.
- Forgetting to filter empty segments — '/home//foo' would give '//home//foo'.
- Joining without the leading '/' — produces 'home/foo' instead of '/home/foo'.
Follow-up questions
An interviewer at Datadog may pivot to one of these next:
- Validate path syntax — additional checks.
- Resolve relative paths — start with current dir as the initial stack.
- Datadog-style: canonicalize hierarchical metric names like 'aws.ec2..cpu' -> 'aws.ec2.cpu'.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a stack?
Directories are LIFO: '..' goes back one level. A stack models the directory chain naturally.
What if '..' at the root?
Pop on an empty stack is a no-op in JS (Array.pop returns undefined). The result remains the root, which is the correct behavior — you can't go above /.
Practice these live with InterviewChamp.AI
Drill Simplify Path and other Datadog interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →