Normalize S3 Object Key Paths With Dot and Double-Dot Segments

Last updated: November 29, 2026

Quick Overview

Given a Unix-style path like `/media/./img//2024/../thumbs/`, return its canonical absolute form by collapsing repeated slashes, removing `.` segments, and resolving `..` to the parent (never above root).

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a Unix-style path like `/media/./img//2024/../thumbs/`, return its canonical absolute form by collapsing repeated slashes, removing `.` segments, and resolving `..` to the parent (never above root).

A phone-screen question in the style of Amazon storage teams, where a gateway must canonicalize object paths before authorization checks so that `../` tricks cannot escape a prefix. The interviewer wants a clean stack solution and an awareness of why normalization matters for security.

What the Interviewer Expects
  • Uses a stack of segments
  • Ignores empty and . segments
  • Pops on .. and ignores .. at root
  • Returns / for an empty result with no trailing slash otherwise
  • Explains why canonicalizing before an access check matters
Key Topics to Cover
string parsing
stack
path canonicalization
security
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
  • How would you support relative paths that start from a given working directory?
  • Segments like `...` are legal names; does your code treat them correctly?
  • How would you detect that a request escapes an allowed prefix such as `/tenant-42/`?
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 is an absolute path string. Output is the canonical absolute path: single slashes, no . or .., no trailing slash except the root itself. Names such as ... or .hidden are ordinary segment...

High-Level Design

Split on /. Every piece is one of four kinds: empty (from // or the leading slash), . (stay), .. (go up), or a name (go down). A stack models the current directory: push names, pop on .. whe...


Submit Your Answer
Markdown supported