Tidy a file path
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
On many computers, a path like /photos/2024/../holiday/./beach names a folder by listing the folders to go through, separated by /. It always starts at the top folder, /.
Two names are special: . means "stay in this folder", and .. means "go up to the folder above". Going up from the top folder does nothing. Several slashes in a row, like //, count as one.
Given path, return the same location written in its simplest form: starting with /, folder names separated by single slashes, no . or .., and no slash at the end. The top folder on its own is just /.
Examples
Example 1
- Input
- path = "/photos/2024/../holiday/./beach"
- Output
- "/photos/holiday/beach"
Go into 2024, then back up out of it. The . stays put.
Example 2
- Input
- path = "/music//jazz/"
- Output
- "/music/jazz"
The double slash counts as one, and the slash at the end goes.
Example 3
- Input
- path = "/../.."
- Output
- "/"
You can't go above the top folder.
Rules
pathstarts with/and has between 1 and 150,000 characters.- Folder names use letters, digits,
_, and dots. Only.and..on their own are special: a name like...is a normal folder.
Words on this page, in plain English (1)Open if any word is new to you
- String
- A piece of text, written between quotes. Each letter in it is a character.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
A path lists folders from the top: /photos/holiday means "from the top, go into photos, then into holiday". The messy bits we clean up:
.means stay here, so it can be dropped...means go up one folder, undoing the last folder we went into.//is the same as/, and a slash at the end means nothing.
2. Solve an example by hand
/photos/2024/../holiday/./beach. Into photos. Into 2024. ..: back out of 2024. Into holiday. .: stay. Into beach. We're in photos, then holiday, then beach: /photos/holiday/beach.
Every .. undid the most recent folder. Undo: a stack.
3. The slow way
Repeatedly search the text for patterns like name/.. and cut them out until nothing changes. Each cut rebuilds the string and the search starts over, so a deep path takes O(n²) steps, and the cases (/../ at the top, ... as a real name) are easy to get wrong.
4. Spot the pattern
A route that grows at the end and shrinks from the end: a stack of folder names.
5. The stack idea
Split the path on /. For each piece:
- empty (from
//or the leading/) or.: skip it, ..: pop the last folder (if there is one),- anything else: push it.
Finally, join the stack with / and put a / in front.
Folders on a stack
pieces
folders (stack, top first)
- empty
function tidyPath(path) { const folders = []; // the route from the top folder down for (const name of path.split("/")) { if (name === "" || name === ".") { continue; // from "//", the leading "/", or "stay here" } else if (name === "..") { folders.pop(); // go up (does nothing at the top) } else { folders.push(name); } } return "/" + folders.join("/");}Split the path on "/": "", "photos", "2024", "..", "holiday", ".", "beach".
6. The code, line by line
const folders = []; // the route from the top folder downThe folders on our route, from the top down.
for (const name of path.split("/")) { if (name === "" || name === ".") { continue; // from "//", the leading "/", or "stay here"path.split("/")cuts the path at every slash. The leading slash gives an empty first piece, and//gives an empty piece in the middle; skip those, and..} else if (name === "..") { folders.pop(); // go up (does nothing at the top)..undoes the last folder. At the top, the stack is empty andpop()does nothing, which is exactly the rule.} else { folders.push(name); }A real folder name, including odd ones like
...: go into it.return "/" + folders.join("/");Rebuild the path. With no folders,
joingives"", so the result is just"/".
7. How fast is it?
O(n) time: splitting, one pass, and joining. O(n) extra memory for the pieces and the stack.
Common mistakes
- Treating every name with dots as special. Only
.and..exactly are special;...andv1.2are normal names. - Adding a trailing slash. Join the folders with
/between them; don't add one after the last.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Tidy a file path
Read the 13 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function tidyPath(path) { const folders = []; // the route from the top folder down for (const name of path.split("/")) { if (name === "" || name === ".") { continue; // from "//", the leading "/", or "stay here" } else if (name === "..") { folders.pop(); // go up (does nothing at the top) } else { folders.push(name); } } return "/" + folders.join("/");}