Skip to main content

Crawler Log Folder - Solution & Explanation

EasyArrayStringStack15 min readAsked at: Meta, Mercari
Practice this problem

Problem Statement

The Leetcode file system keeps a log each time some user performs a change folder operation.

The operations are described below:

  • "../" : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
  • "./" : Remain in the same folder.
  • "x/" : Move to the child folder named x (This folder is guaranteed to always exist).

You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.

The file system starts in the main folder, then the operations in logs are performed.

Return the minimum number of operations needed to go back to the main folder after the change folder operations.

 

Example 1:

Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to the main folder.

Example 2:

Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3

Example 3:

Input: logs = ["d1/","../","../","../"]
Output: 0

 

Constraints:

  • 1 <= logs.length <= 103
  • 2 <= logs[i].length <= 10
  • logs[i] contains lowercase English letters, digits, '.', and '/'.
  • logs[i] follows the format described in the statement.
  • Folder names consist of lowercase English letters and digits.

Approach Overview

Problem Overview: You are given a list of folder navigation logs such as "../", "./", or "x/". Each log represents moving up a directory, staying in the current folder, or entering a subfolder. The goal is to compute the minimum number of operations needed to return to the main folder after processing all logs.

The commands behave exactly like terminal navigation. "../" moves one level up (unless you are already at the root), "./" keeps you in the same directory, and any other string ending with "/" means entering a new folder. The problem becomes a simple state tracking task where you simulate how deep you are in the folder structure.

Approach 1: Using Depth Counter (O(n) time, O(1) space)

This approach keeps a single integer variable representing the current folder depth. Iterate through the array of logs and update the depth based on each command. When encountering "../", decrease the depth only if it is greater than zero. Ignore "./" because it does not change the directory. For any other log (like "d1/"), increment the depth because you move into a subfolder.

The key insight is that the actual folder names do not matter. Only the direction of movement matters. Because the algorithm processes each log once and stores only an integer counter, it runs in O(n) time with O(1) extra space. This is the simplest and most efficient solution and is typically what interviewers expect.

Approach 2: Using Stack for Folder Simulation (O(n) time, O(n) space)

This method simulates the directory structure using a stack. Traverse the logs one by one. When the command is a folder name like "x/", push it onto the stack. When the command is "../", pop from the stack if it is not empty. Ignore "./" since it keeps you in the same folder.

After processing all logs, the number of elements remaining in the stack represents how many directories deep you are from the root. That value equals the minimum operations needed to return to the main folder. This approach models the navigation process explicitly and mirrors how real file systems manage paths.

The stack-based solution also runs in O(n) time because each log is processed once. However, it uses O(n) space in the worst case if every log moves into a new folder. The logic relies heavily on operations typical in string processing and stack manipulation.

Recommended for interviews: The depth counter solution is usually preferred. It shows that you recognized the problem only tracks folder depth rather than actual paths. The stack approach is still valuable because it demonstrates understanding of directory simulation and stack-based state management.

Approach 1: Using Depth Counter

This approach involves maintaining a counter to keep track of the current folder depth. For each log operation, adjust the counter accordingly:

  • For "x/" operations, increment the counter since we're going deeper into the directory structure.
  • For "../" operations, decrement the counter but ensure it doesn't go below zero since we can't go above the root directory.
  • For "./", do nothing as it indicates staying in the same directory.

After processing all the logs, the counter will give the minimum operations needed to return to the main folder, as it represents the current depth.

The C implementation uses the strcmp function to compare strings for directory operations and adjusts a counter representing the current directory depth. The final value of the counter gives the required number of operations to get back to the main folder.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of logs.
Space Complexity: O(1), no additional space is required except for the depth counter.

Try this approach in the editor →

Approach 2: Using Stack for Folder Simulation

This approach simulates navigating through folders using a stack. The stack keeps track of folder paths:

  • For "x/", push the folder onto the stack.
  • For "../", pop from the stack unless it's empty (indicating the root).
  • For "./", do nothing, as it indicates staying in the same directory.

The stack's size at the end represents the current depth, which is the number of operations needed to return to the main folder.

The C solution mimics the behavior of a stack using an integer counter to track how many directories down the user is. It simulates pushing and popping with integer operations.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

JavaScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Depth Counter

Time Complexity: O(n), where n is the number of logs.
Space Complexity: O(1), no additional space is required except for the depth counter.

Using Stack for Folder Simulation

Time Complexity: O(n).
Space Complexity: O(1).

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Depth CounterO(n)O(1)Best choice when only folder depth matters and memory should remain minimal
Stack SimulationO(n)O(n)Useful when explicitly modeling directory navigation or when folder names must be tracked

Video Solution

Crawler Log Folder - Leetcode 1598 - Python • NeetCodeIO • 5,827 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Crawler Log Folder easy or hard?
Crawler Log Folder is classified as an Easy problem on LeetCode with an acceptance rate above 70%. The challenge focuses on correctly interpreting directory navigation commands and implementing a simple simulation using counters or stacks.
Crawler Log Folder Python/Java solution
Both Python and Java implementations typically follow the depth counter approach. Loop through the logs, update an integer depth variable for each command, and return the final depth. The algorithm runs in O(n) time and requires constant extra space.
How to solve Crawler Log Folder in O(n)?
Iterate through the log array once and maintain a depth variable. Increment depth for folder entries like "x/", decrement it for "../" if depth is greater than zero, and ignore "./". After processing all logs, the depth value equals the minimum operations required to return to the root.
What is the best approach for Crawler Log Folder?
The depth counter approach is the most efficient solution. Track the current directory depth with a single integer while iterating through the logs. Increase depth when entering a folder, decrease it when encountering "../" (if depth > 0), and ignore "./". This runs in O(n) time and O(1) space.
Is Crawler Log Folder asked at Google/Amazon/Meta?
Crawler Log Folder is categorized as an easy array and string simulation problem and appears in interview preparation lists for companies such as Amazon and Google. It is commonly used to test basic simulation logic and familiarity with stack-like behavior.
What data structure is used in Crawler Log Folder?
Two main approaches exist. The optimal solution uses a simple integer counter to track folder depth. Another approach uses a stack to simulate directory navigation, pushing folder names and popping when moving up a directory.
What is the time complexity of Crawler Log Folder?
The optimal solution runs in O(n) time where n is the number of log entries. Each log command is processed once with constant work. The depth counter method uses O(1) space, while the stack simulation approach may use O(n) space in the worst case.

Ready to solve this problem?

Practice Crawler Log Folder with our built-in code editor and test cases.

Practice on FleetCode