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 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
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.
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
- Decode String Medium Free Expand k[encoded] patterns, nesting included.
- Maximum Bracket Depth Easy Free How deep do the parentheses nest? A counter beats a stack.
- Evaluate Reverse Polish Notation Medium Free Evaluate a postfix expression - and get integer division right.
- Count Substring Occurrences Medium Free Count overlapping occurrences by comparing characters, not slicing strings.