Code Logo

Crawler Log Folder

Published at23 Jul 2026
Easy 0 views
Like0

The LeetCode file system keeps a log of folder operations. Each operation is one of three types: "../" moves to the parent folder (if not already in the main folder), "./" stays in the same folder, and "x/" moves to a child folder named x. You start in the main folder. After processing all operations, return the minimum number of operations to return to the main folder (the depth).

A stack depth counter tracks this: increment on "x/", decrement on "../" (but not below 0), ignore "./". The final depth is the answer.

For example, logs = ["d1/","d2/","../","d3/","../"]: start 0, d1=1, d2=2, ..=1, d3=2, ..=1. Final depth 1.

The depth counter represents how deep we are in the folder hierarchy. The main folder is depth 0. Each child folder increases depth by 1. Each ../ decreases depth by 1 but cannot go below 0 (you cannot go above the main folder). The ./ operation does nothing. At the end, the depth is the answer.

This problem is commonly asked in entry-level technical interviews to assess understanding of fundamental stack behavior and array construction patterns. The solution is straightforward once you recognize that the stack operations map directly to the problem requirements.

Example Input & Output

Example 1
Input
["./","../"]
Output
0
Explanation

All no-ops.

Example 2
Input
["d1/","../","../","../"]
Output
0
Explanation

Cannot go below root.

Example 3
Input
["d1/","d2/","../","d3/","../"]
Output
1
Explanation

d1,d2,..,d3,.. = depth 1.

Algorithm Flow

Recommendation Algorithm Flow for Crawler Log Folder
Recommendation Algorithm Flow for Crawler Log Folder

Solution Approach

Track depth. For each log: if "../", depth = max(0, depth-1). Else if not "./", depth++. Return depth.

function solution(logs) {
  var depth = 0;
  for (var i = 0; i < logs.length; i++) {
    if (logs[i] === "../") depth = Math.max(0, depth - 1);
    else if (logs[i] !== "./") depth++;
  }
  return depth;
}

Time O(n), Space O(1).

Best Answers

java
import java.util.*;
class Solution {
    public int solution(String[] logs) {
        int depth = 0;
        for (String log : logs) {
            if (log.equals("../")) depth = Math.max(0, depth - 1);
            else if (!log.equals("./")) depth++;
        }
        return depth;
    }
}