Canonicalize an Absolute Unix-Style Path With Dot and Double-Dot Segments

Quick Overview

Convert an absolute Unix-style file path into its canonical form by collapsing repeated slashes, dropping single-dot segments, resolving double-dot segments against the parent directory, and treating other dot sequences as ordinary names. It tests careful string parsing and edge cases such as the root directory.

Canonicalize an Absolute Unix-Style Path With Dot and Double-Dot Segments

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given an absolute path in a Unix-style file system, return its simplified canonical form. ### Function Signature ```python def simplify_path(path: str) -> str: ``` ### Rules Interpreting the input: - `path` starts with `/`. - A single period `.` refers to the current directory and is dropped. - A double period `..` moves up to the parent directory; at the root it stays at the root. - Several consecutive slashes (for example `//`) act as a single slash. - Any other sequence of characters between slashes is a directory or file name, including sequences of three or more periods such as `...`. The canonical output: - starts with a single `/`; - separates names with exactly one `/`; - has no trailing `/` unless the result is the root `/` itself; - contains no `.` or `..` components. ### Constraints - `1 <= len(path) <= 3000` - `path` consists of English letters, digits, `.`, `/`, and `_`. - `path` is a valid absolute Unix path. ### Examples **Example 1** Input: `path = "/home//foo/"` Output: `"/home/foo"` Explanation: the double slash collapses to one and the trailing slash is removed. **Example 2** Input: `path = "/../"` Output: `"/"` Explanation: moving up from the root stays at the root. **Example 3** Input: `path = "/.../a/../b/c/../d/./"` Output: `"/.../b/d"` Explanation: `...` is an ordinary name; `a/..` and `c/..` cancel out, and `.` is dropped.

Overview: Convert an absolute Unix-style file path into its canonical form by collapsing repeated slashes, dropping single-dot segments, resolving double-dot segments against the parent directory, and treating other dot sequences as ordinary names. It tests careful string parsing and edge cases such as the root directory.

|Home/Coding & Algorithms/Meta
Meta logo
Meta
Sep 14, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Given an absolute path in a Unix-style file system, return its simplified canonical form.

Function Signature

def simplify_path(path: str) -> str:

Rules

Interpreting the input:

  • path starts with / .
  • A single period . refers to the current directory and is dropped.
  • A double period .. moves up to the parent directory; at the root it stays at the root.
  • Several consecutive slashes (for example // ) act as a single slash.
  • Any other sequence of characters between slashes is a directory or file name, including sequences of three or more periods such as ... .

The canonical output:

  • starts with a single / ;
  • separates names with exactly one / ;
  • has no trailing / unless the result is the root / itself;
  • contains no . or .. components.

Constraints

  • 1 <= len(path) <= 3000
  • path consists of English letters, digits, . , / , and _ .
  • path is a valid absolute Unix path.

Examples

Example 1

Input: path = "/home//foo/"

Output: "/home/foo"

Explanation: the double slash collapses to one and the trailing slash is removed.

Example 2

Input: path = "/../"

Output: "/"

Explanation: moving up from the root stays at the root.

Example 3

Input: path = "/.../a/../b/c/../d/./"

Output: "/.../b/d"

Explanation: ... is an ordinary name; a/.. and c/.. cancel out, and . is dropped.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...