Magicsheet logo

Crawler Log Folder

Easy
37.1%
Updated 6/1/2025

Crawler Log Folder

What is this problem about?

The Crawler Log Folder interview question simulates a user navigating through a file system using terminal commands. You are given an array of strings logs, where:

  • "../" moves you to the parent folder.
  • "./" keeps you in the current folder.
  • "x/" moves you into a child folder named x. You start in the main (root) folder. Your task is to find the minimum number of operations needed to return to the root folder after executing all the commands in the log.

Why is this asked in interviews?

Companies like Amazon and Bloomberg use this simulation interview pattern to test basic logic and edge-case handling. It’s an "Easy" difficulty problem that evaluates whether you can maintain a "depth" state and handle boundary conditions (like trying to go up from the root). It's a fundamental exercise in string parsing and state management.

Algorithmic pattern used

This problem is best solved using a Counter or a Stack.

  1. Initialize depth = 0.
  2. Iterate through each log:
    • If log is "../", decrement depth (but don't go below 0).
    • If log is "./", do nothing.
    • Otherwise, increment depth.
  3. The final depth is the number of steps needed to return to the root.

Example explanation

Logs: ["d1/", "d2/", "../", "d21/", "./"]

  1. "d1/": Move into d1. depth = 1.
  2. "d2/": Move into d2. depth = 2.
  3. "../": Move back to d1. depth = 1.
  4. "d21/": Move into d21. depth = 2.
  5. "./": Stay. depth = 2. Result: 2.

Common mistakes candidates make

  • Negative Depth: Forgetting to check if depth > 0 before decrementing, leading to incorrect results if the log tries to go "above" the root.
  • Over-engineering: Using a real stack to store folder names when only the number of folders matters.
  • String Comparison: Using fuzzy matching instead of checking for the exact strings "../" and "./".

Interview preparation tip

Always look for ways to simplify the state. If the problem only asks for "how many steps back," you don't need to know the names of the folders you visited, only how deep you are. This saves both time and space.

Similar Questions