Skip to main content

53. Simplify Path

mediumAsked at Workday

Simplify a Unix-style absolute path. Workday uses this as a stack-on-strings pattern — same shape as resolving nested cost-center inheritance chains.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen.

Problem

Given an absolute path for a Unix-style file system, simplify it. The path may contain '.', '..', and multiple consecutive slashes. Return the simplified canonical 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 = "/a/./b/../../c/"
Output
"/c"

Approaches

1. Manual character-by-character parse

Walk character-by-character, building tokens and handling /, ., ..

Time
O(n)
Space
O(n)
// hand-rolled parser — works but error-prone

Tradeoff: Lots of edge cases; easy to slip on consecutive slashes.

2. Split on / + stack

Split on '/'. Walk parts. '' or '.' skip. '..' pop. 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: Split handles consecutive slashes for free (yields empty strings, which we skip).

Workday-specific tips

Workday wants the stack approach with split — short, idiomatic, handles all the slash cases for free. Articulate why empty-string parts come from consecutive slashes.

Common mistakes

  • Trying to handle consecutive slashes manually — split + empty-skip is free.
  • Popping without checking if stack is empty (in some languages this errors; in JS it's a no-op).
  • Forgetting the leading '/' in the result.

Follow-up questions

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

  • What if the path is relative (no leading /)?
  • Resolve symlinks (different problem).
  • Path with environment variables.

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 empty-string handling work?

split('/') on '/a//b' yields ['', 'a', '', 'b']. Skipping empty parts naturally collapses consecutive slashes.

What about '...' (three dots)?

Not a special Unix token — treat as a regular filename. Push it onto the stack.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →