Skip to content
Algorithms for Beginners

Problems / Stack

Tidy a file path

medium

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. 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

  1. Example 1

    Input
    path = "/photos/2024/../holiday/./beach"
    Output
    "/photos/holiday/beach"

    Go into 2024, then back up out of it. The . stays put.

  2. Example 2

    Input
    path = "/music//jazz/"
    Output
    "/music/jazz"

    The double slash counts as one, and the slash at the end goes.

  3. Example 3

    Input
    path = "/../.."
    Output
    "/"

    You can't go above the top folder.

Rules

  • path starts 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.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function tidyPath(path) {
  // Return the simplest form of path.
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in stack

  • Learn the stack pattern

    Keep the most recent unfinished thing on top, and finish it first.

  • Days until warmer

    Medium. For each day, count how many days you'd wait for a warmer one.

  • Balanced brackets

    Easy. Check whether every bracket in a string is closed by the right kind, in the right order.

  • Typing with backspace

    Easy. Work out what's on the screen after someone types, including presses of the backspace key.

  • Stack with minimum

    Medium. Build a stack that can also tell you its smallest item instantly.

  • Postfix calculator

    Medium. Work out a sum written with the operators after the numbers, like 3 4 + 2 *.