Here is the interview question prompt, presented for reference.
Here is how I would enrich the absolute path simplification problem with more details and walkthroughs:
In Unix, an absolute path locates a file/directory from the root directory. It can contain:
.
- Stands for the current directory..
- Stands for the parent directory/
- Treated as a single /
A canonical path simplifies an absolute path by:
.
, ..
and extra /
For example:
Absolute: /a/./b/../c
Canonical: /a/c
Here we removed:
- .
- Current directory in /a/./b
- ..
- Goes up from /b to /a
- Extra /
between a and b
Leaving us with the simplified /a/c
Given an absolute path string, convert it to its canonical form.
Input: /a/./b/../../c
Output: /c
Walkthrough: - /a/./b - Remove . - /a/b/../ - Go up from b to a - /a/../c - Go up from a to root - /c - Final simplified canonical path
Input: /a//b////c/d//././/..
Output: /a/b/c
Walkthrough:
- /a//b/ - Remove extra /
- /a/b/c/d//./. - Remove . and extra /
- /a/b/c/d/.. - Go up from d to c
- /a/b/c - Final simplified path
The goal is to take an absolute path string and simplify it into its canonical form.
You can see the full challenge with visuals at this link.
Challenges • Asked about 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Simplifying Absolute Paths (Main Thread).