Skip to main content

55. Simplify Path

mediumAsked at Snowflake

Simplify a Unix-style absolute path. Snowflake uses this to test stack-based parsing — relevant to their SQL parser's path resolution for staged file references like @stage/path/to/file.csv.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake compiler-team uses this to discuss stage-path resolution.
  • LeetCode Discuss (2025-10)Reported at Snowflake new-grad onsites.

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 a single slash, does not end with '/' (except root), and only contains directories, not '.' or '..'.

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. Repeated regex replace

Replace '//+'->'/', then '/<dir>/../' iteratively.

Time
O(n^2)
Space
O(n)
// outline: while there's a redundancy, regex-replace it.
// Quadratic from repeated string copies.

Tradeoff: Slow due to repeated allocations.

2. Split + stack (optimal)

Split by '/'. Walk components: '.' = noop; '..' = pop; '' = noop; else push.

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: Linear, single pass. Stack naturally models the directory hierarchy.

Snowflake-specific tips

Snowflake interviewers want the stack approach because it generalizes to other parser problems. Bonus signal: connect to their SQL parser — when resolving stage paths like @my_stage/2026/05/data.csv, they tokenize on / and reduce ../ patterns similarly.

Common mistakes

  • Popping an empty stack on '..' — must guard.
  • Forgetting to prepend '/' to the joined result.
  • Treating multiple consecutive slashes as multiple empty components and mishandling — splitting just drops them naturally.

Follow-up questions

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

  • Validate path syntax (reject invalid characters).
  • Symbolic link resolution.
  • How does Snowflake resolve stage paths during query compilation?

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 does the stack model the directory hierarchy?

Each push is descending into a subdir; each pop is going up. The stack at any moment represents the current absolute path from root.

Why does '' split happen?

Consecutive slashes ('a//b') split to ['a', '', 'b']. The 'continue on empty' line handles them as noops.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →