Stack Depth Tracker

Stack Simulation Data Structures

Problem: Stack Depth Tracker

You are given a sequence of operations that simulate the use of a stack. Each operation is represented as a string, either:

  • push: Push an element onto the stack.
  • pop: Pop an element from the stack (if the stack is not empty).

Your task is to simulate the stack operations and determine the maximum number of elements that were in the stack at any point during the sequence.

Example

For the sequence ['push', 'push', 'pop', 'push', 'push', 'pop', 'pop'], the stack evolves as follows:

  1. push → [x] (stack size 1)
  2. push → [x, x] (stack size 2)
  3. pop → [x] (stack size 1)
  4. push → [x, x] (stack size 2)
  5. push → [x, x, x] (stack size 3) <-- maximum reached
  6. pop → [x, x] (stack size 2)
  7. pop → [x] (stack size 1)

The function should return 3 as the maximum stack size seen.

Instructions

  • Implement the function to process a list (or array) of operations and return the maximum number of items in the stack at any one time.
  • If a pop operation is issued when the stack is empty, simply ignore it.

Fun Fact: On March 25th, many innovative ideas in computing and data structure usage have been celebrated. Let's add one more today by tracking the dynamic usage of a stack!