Canonicalize an Absolute Unix Path

Quick Overview

Simplify an absolute Unix path using separator, current-directory and parent-directory rules while preserving ordinary names such as three dots.

Canonicalize an Absolute Unix Path

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Simplify the absolute Unix-style path `absolute_path` into its canonical form by applying these path-component rules: - Repeated slashes act as one separator. - A component equal to `.` refers to the current directory. - A component equal to `..` moves to the parent directory. At the root, moving to a parent leaves you at the root. - Other names are ordinary components. In particular, `...` and longer runs of periods are names, not parent-directory instructions. Return a path beginning with exactly one slash. Use one slash between remaining components and omit a final slash unless the result is the root path `/`. The result must contain no `.` or `..` navigation components. ### Examples **Example 1** ```text Input: absolute_path = "/var//logs/./app/../cache/" Output: "/var/logs/cache" ``` The repeated separator and current-directory component disappear, and the parent-directory component cancels `app`. **Example 2** ```text Input: absolute_path = "/../../.../x/.." Output: "/..." ``` The first two parent moves stay at the root. The name `...` remains, while the final parent move cancels `x`. ### Constraints - `1 <= absolute_path.length <= 3000` - The string is a valid absolute Unix path and starts with `/`. - It contains only English letters, decimal digits, `.`, `/`, and `_`.

Overview: Simplify an absolute Unix path using separator, current-directory and parent-directory rules while preserving ordinary names such as three dots.

Roblox logo
Roblox
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
2
0

Problem

Simplify the absolute Unix-style path absolute_path into its canonical form by applying these path-component rules:

  • Repeated slashes act as one separator.
  • A component equal to . refers to the current directory.
  • A component equal to .. moves to the parent directory. At the root, moving to a parent leaves you at the root.
  • Other names are ordinary components. In particular, ... and longer runs of periods are names, not parent-directory instructions.

Return a path beginning with exactly one slash. Use one slash between remaining components and omit a final slash unless the result is the root path /. The result must contain no . or .. navigation components.

Examples

Example 1

Input: absolute_path = "/var//logs/./app/../cache/"
Output: "/var/logs/cache"

The repeated separator and current-directory component disappear, and the parent-directory component cancels app.

Example 2

Input: absolute_path = "/../../.../x/.."
Output: "/..."

The first two parent moves stay at the root. The name ... remains, while the final parent move cancels x.

Constraints

  • 1 <= absolute_path.length <= 3000
  • The string is a valid absolute Unix path and starts with / .
  • It contains only English letters, decimal digits, . , / , and _ .