Skip to content
Katabench
Try free

Simplify Unix Path

Medium About 20 min Free, no account needed

Canonicalize a Unix path - and survive the .. that climbs past root.

The brief

Given an absolute Unix path (starts with /), return its canonical form:

  • collapse repeated slashes,
  • . means "this directory" (drop it),
  • .. means "up one directory", at root it is a no-op,
  • anything else (including ... or ..x) is a regular name,
  • no trailing slash unless the result is exactly /.

The hidden trap

The visible tests never climb above root and never use a name like .... The hidden ones do. Treat path segments as a stack: push names, pop on .., but only when there is something to pop.

Starter code

C#
using System;

public class Solution
{
    // TODO: return the canonical form of an absolute Unix path.
    public string SimplifyPath(string path)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Stacks
Use last-in, first-out state for nesting, matching and expression evaluation.
String processing
Parse, scan and match text character by character with correct edge cases.

Related katas