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
All no-ops.
Cannot go below root.
d1,d2,..,d3,.. = depth 1.
Algorithm Flow

Solution Approach
Track depth. For each log: if "../", depth = max(0, depth-1). Else if not "./", depth++. Return depth.
Time O(n), Space O(1).
Best Answers
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;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
