Code Logo

Crawler Log Folder

Published at23 Jul 2026
Easy 1 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

Solution Approach

Count the minimum number of operations to return to the main folder after a series of folder navigation operations. Operations are '../' (move up), './' (stay), or 'x/' (move into child folder x). Use a depth counter: increment for folder names, decrement for '../' (but not below 0), ignore './'. Return the final depth.

function minOperations(logs) {
  var depth = 0;
  for (var i = 0; i < logs.length; i++) {
    if (logs[i] === '../') { if (depth > 0) depth--; }
    else if (logs[i] !== './') depth++;
  }
  return depth;
}

The depth counter tracks how many levels deep the current folder is from the root. Moving up from the root keeps depth at 0. Child folders increase depth.

Time complexity is O(n), space complexity is 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;
    }
}