Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Simplify Nested Path String

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Simplify Nested Path String. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given a string path representing a slash-separated path, return its canonical simplified form. The path may contain repeated slashes, . segments for the current directory, and .. segments for the parent directory. The result must start with a single /, contain no trailing slash unless it is the root, and never move above root.

Constraints

  • 1 <= len(path) <= 10^5
  • path contains English letters, digits, ., _, -, and /
  • The input path is absolute and always starts with /

Function Signature

def simplify_path(path):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output