Normalize an Absolute Unix Path With Dots and Repeated Slashes

Last updated: November 29, 2026

Quick Overview

Given an absolute Unix-style path that may contain '.', '..', and repeated slashes, return its canonical form: single slashes, no trailing slash, and no '.' or '..' components.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given an absolute Unix-style path that may contain '.', '..', and repeated slashes, return its canonical form: single slashes, no trailing slash, and no '.' or '..' components.

Path normalization comes up in Meta screens as a test of stack usage and careful tokenizing, the kind of logic behind build tooling and storage path handling in large monorepos. Interviewers check edge cases at the root and unusual names like '...'.

What the Interviewer Expects
  • Splits on '/' and ignores empty tokens
  • Treats '.' as no-op and '..' as pop
  • Never pops above the root
  • Keeps names like '...' as regular directories
  • Joins with a single leading slash
Key Topics to Cover
Stack
Strings
Parsing
File systems
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Support relative paths given a current working directory.
  • Add symbolic links given as a map from path to target, with cycle detection.
  • Return the shortest relative path from directory A to directory B.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input starts with '/'. Directory names contain letters, digits, '_' and '.' characters. Output must start with '/', have no trailing slash (unless it is just the root), and no empty components....

High-Level Design

Split on '/', which turns repeated slashes into empty strings. Walk the tokens with a stack: skip empty strings and '.', pop on '..' when the stack is non-empty, and push anything else. Note that '......


Submit Your Answer
Markdown supported