Frontend School

API Endpoint Path Normalizer

DSA
medium

API Endpoint Path Normalizer

Medium
Stack
String
Arrays
Path Manipulation

Overview

Gupshup's platform handles a vast ecosystem of APIs, requiring robust and consistent management of endpoint paths. Ensuring a canonical and absolute representation of these paths is crucial for reliable routing, security, and logging across various services. This problem challenges you to implement a function that normalizes a sequence of raw path segments into a standard, absolute API endpoint path.

What you need to do / Task

Implement a function normalizeApiPath that takes an array of string segments and returns the canonical absolute path. Your implementation must adhere to the following rules:

  • Valid Segments: Alphanumeric strings (e.g., "users", "v1") represent valid directory or resource names and should be included in the path.
  • Current Directory (.): Segments consisting solely of . should be ignored as they represent the current directory and do not alter the path.
  • Parent Directory (..): Segments consisting solely of .. should navigate up one level in the path. If the path is already at the root (i.e., empty), .. has no effect.
  • Root Directory (/): A segment consisting solely of / acts as a reset. When encountered, it signifies that subsequent segments should be processed relative to the root, effectively clearing any previously accumulated path segments.
  • Final Path Format: The resulting canonical path must always be an absolute path, starting with a single /. It should not end with a / unless it is the root path / itself.

Input & Output

  • Input: segments: string[]
    • An array of strings, where each string represents an atomic path segment. Segments can be valid names (e.g., "api", "messages"), ".", "..", or "/".
  • Output: string
    • The canonical absolute path string, formatted as described above.

Clarifications

  • The input segments array will not be empty, and each segment will be a non-empty string.
  • Segments are already split; you do not need to handle internal / characters within a segment (e.g., you won't receive "users/profile" as a single segment).

Examples

1

Example 1

Input:

segments = ["/", "home", ".", "user", "..", "profile"]

Output:

"/home/profile"

Note:

Starting at root, home adds /home. . is ignored. user adds /home/user. .. removes user to become /home. Finally, profile adds /home/profile.

2

Example 2

Input:

segments = ["/", "..", "var", "log", "..", "tmp"]

Output:

"/var/tmp"

Note:

Starting at root, .. has no effect. var adds /var. log adds /var/log. .. removes log to become /var. Finally, tmp adds /var/tmp.

3

Example 3

Input:

segments = ["home", "user", "/", "api", "v1", "..", "status"]

Output:

"/api/status"

Note:

Implicitly starting at root, home then user would form /home/user. However, _ resets the path to root. Then api adds /api, v1 adds /api/v1. .. removes v1 to become /api. Finally, status adds /api/status.

Constraints

Constraints & assumptions

  • 1 <= segments.length <= 10^4
  • 1 <= segments[i].length <= 100
  • segments[i] consists of English letters, digits, . (dot), .. (double dot), or / (slash).
  • No segment will contain / characters within itself (they are already pre-split).

What interviewers evaluate

  • Correctness: Accurately handles all segment types (., .., /, valid names) and constructs the canonical path.
  • Data Structure Usage: Effective and appropriate application of a stack data structure.
  • Edge Cases: Proper management of scenarios like navigating .. from the root, and the behavior of / as a path reset.
  • Efficiency: Solution demonstrates optimal time and space complexity, typically O(N) where N is the total length of all segments.

Loading editor...