Back to Practice
#0230

Normalize URL Path

MediumPython25 min20 XP

Problem

Given a URL path, remove duplicate slashes, ignore ".", and resolve ".." by going up one segment.

Why This Matters

Path normalization combines string parsing with stack behavior and shows up in routing, file systems, and URL cleaning.

Function Signature

def normalize_path(path):

Examples

Example 1
Input"/api//v1/../users/./42"
Output"/api/users/42"

v1 is removed by .., duplicate slashes disappear, and . is ignored.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Prefer readable Python before micro-optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input"/api//v1/../users/./42"
Expected"/api/users/42"

v1 is removed by .., duplicate slashes disappear, and . is ignored.

Hidden Test Categories
root pathtoo many .. segmentsduplicate slashes