Max Stack Depth

Stack Simulation Data Structures

Problem Description:

You are given a list of operations that simulate pushing and popping elements on a stack. Your task is to determine the maximum number of elements that were present on the stack at any point during these operations.

  • Each operation is a string.
  • A push operation is in the format push X, where X is a value (the value itself does not affect the result).
  • A pop operation is simply the string pop.

If a pop operation is performed when the stack is empty, ignore it.

Example

Input: ["push 1", "push 2", "pop", "push 3", "push 4", "pop"]

Simulation:

  • push 1 → Stack: [1] → Max size: 1
  • push 2 → Stack: [1, 2] → Max size: 2
  • pop → Stack: [1] → Max size: 2
  • push 3 → Stack: [1, 3] → Max size: 2
  • push 4 → Stack: [1, 3, 4] → Max size: 3
  • pop → Stack: [1, 3] → Max size: 3

Output: 3

Function Signature

Implement a function named maxStackDepth that accepts a list (or array) of strings representing the operations and returns an integer that indicates the maximum size the stack reached during the operations.

Remember: Effective use of stacks is crucial in many programming tasks. Enjoy coding and have fun solving this problem on this fine January 10th!