Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Normalize a URI Path

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

Your question is Normalize a URI Path. 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 URI, normalize it (for example, /a/../b/c -> /b/c).

Treat the input as an absolute URI path containing slash-separated segments. Resolve . and .., collapse repeated slashes, and prevent .. from moving above the root. Preserve a trailing slash when the normalized result is not /.

Function Contract

Implement normalize_uri(uri), which accepts a string and returns the normalized path string. Inputs contain no query string or fragment.

Constraints

  • 1 <= len(uri) <= 10000
  • uri begins with /
  • The input contains no query string or fragment
  • Path segments may contain letters, digits, hyphens, underscores, and percent-encoded text

Function Signature

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