Count the Operations Needed to Return to the Main Folder After a Folder-Change Log

Quick Overview

Given a log of change-folder operations that move into a child folder, up to the parent, or stay in place, find how many operations bring the user back to the main folder. It tests simulating folder navigation with a depth counter and handling a move up while already at the top level.

Count the Operations Needed to Return to the Main Folder After a Folder-Change Log

Company: Mercari

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A file system keeps a log each time a 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 step `i`. The file system starts in the main folder, then the operations in `logs` are performed in order. Return the minimum number of operations needed to go back to the main folder after the change folder operations. ### Function Signature ```python def min_operations(logs: list[str]) -> int: ``` ### Constraints - `1 <= len(logs) <= 10^3` - `2 <= len(logs[i]) <= 10` - `logs[i]` contains lowercase English letters, digits, `'.'` and `'/'`. - `logs[i]` follows the format described above. - Folder names consist of lowercase English letters and digits. ### Examples ```text Input: logs = ["d1/", "d2/", "../", "d21/", "./"] Output: 2 ``` After the operations the user is in `d1/d21`, two levels below the main folder, so the operation `"../"` is needed 2 times. ```text Input: logs = ["d1/", "d2/", "./", "d3/", "../", "d31/"] Output: 3 ``` The user ends in `d1/d2/d31`, three levels below the main folder, so 3 `"../"` operations are required. ```text Input: logs = ["d1/", "../", "../", "../"] Output: 0 ``` The first `"../"` returns to the main folder, and the next two leave the user there, so no operation is needed.

Overview: Given a log of change-folder operations that move into a child folder, up to the parent, or stay in place, find how many operations bring the user back to the main folder. It tests simulating folder navigation with a depth counter and handling a move up while already at the top level.

Mercari logo
Mercari
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A file system keeps a log each time a 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 step i.

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

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

Function Signature

def min_operations(logs: list[str]) -> int:

Constraints

  • 1 <= len(logs) <= 10^3
  • 2 <= len(logs[i]) <= 10
  • logs[i] contains lowercase English letters, digits, '.' and '/' .
  • logs[i] follows the format described above.
  • Folder names consist of lowercase English letters and digits.

Examples

Input: logs = ["d1/", "d2/", "../", "d21/", "./"]
Output: 2

After the operations the user is in d1/d21, two levels below the main folder, so the operation "../" is needed 2 times.

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

The user ends in d1/d2/d31, three levels below the main folder, so 3 "../" operations are required.

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

The first "../" returns to the main folder, and the next two leave the user there, so no operation is needed.