Skip to main content

53. Simplify Path

mediumAsked at Plaid

Simplify a Unix-style absolute file path. Plaid asks this as a stack warm-up before harder hierarchical-category-tree normalization problems.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid platform-team screen.
  • LeetCode Discuss (2026)Plaid stack OA.

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 a slash, any two directories are separated by a single slash, no trailing slash, no '.' or '..' as part of the path.

Constraints

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

Examples

Example 1

Input
path = "/home/"
Output
"/home"

Example 2

Input
path = "/../"
Output
"/"

Example 3

Input
path = "/home//foo/"
Output
"/home/foo"

Example 4

Input
path = "/a/./b/../../c/"
Output
"/c"

Approaches

1. Regex collapse

Replace repeated slashes, drop '.', then string-handle '..'.

Time
O(n)
Space
O(n)
// Multi-pass regex — fragile for nested '..' interactions.

Tradeoff: Hard to get right with nested cases.

2. Split + stack

Split on '/'. Push each segment to a stack; pop on '..'; ignore '.' and empty.

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: Clean. The stack naturally encodes the directory hierarchy and '..' becomes a pop.

Plaid-specific tips

Plaid grades this on whether you spot the stack pattern. Bonus signal: connect to category-tree normalization — when a merchant category is moved or merged, the resulting transaction-category path needs the same '../..' resolution. The trailing slash handling (join with '/') is the easy-to-miss detail.

Common mistakes

  • Forgetting to handle empty segments from '//' — produces double slashes.
  • Popping on '..' when the stack is empty — JS's stack.pop() returns undefined gracefully, but some languages throw.
  • Joining with the wrong separator at the end.

Follow-up questions

An interviewer at Plaid may pivot to one of these next:

  • Handle relative paths.
  • Resolve symlinks.
  • Normalize a category path in a multi-level tagging system.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why split on '/'?

Each segment becomes a stack token. Empty strings from leading/trailing slashes and from consecutive slashes are handled by the empty-check.

What if '..' tries to go above root?

Pop on an empty stack is a no-op (in JS). For Plaid's category tree, the same semantic applies — never go above the root category.

Practice these live with InterviewChamp.AI

Drill Simplify Path and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →