📖 Scenario: Imagine you are building a special stack for a calculator app. This stack not only stores numbers but also quickly tells you the smallest number in the stack at any time.
🎯 Goal: You will create a MinStack class that supports pushing numbers, popping the top number, and getting the smallest number in the stack efficiently.
📋 What You'll Learn
Create a MinStack class with methods push, pop, and getMin.
Use two stacks internally: one for all numbers, one for tracking minimums.
Ensure getMin returns the smallest number currently in the stack.
Handle empty stack cases gracefully.
💡 Why This Matters
🌍 Real World
Min stacks are used in calculators, stock price tracking, and anywhere you need quick access to the smallest value in a changing list.
💼 Career
Understanding min stack design is useful for software engineers working on performance-critical applications and technical interviews.
Progress0 / 4 steps
1
Create the MinStack class with two empty stacks
Create a class called MinStack with two properties: stack and minStack, both initialized as empty arrays of numbers.
DSA Typescript
Hint
Use stack: number[] = [] and minStack: number[] = [] inside the class.
2
Add the push method to add numbers and track minimum
Add a method called push that takes a num: number. It should push num to stack. Also, push num to minStack if minStack is empty or num is less than or equal to the last element in minStack.
DSA Typescript
Hint
Check if minStack is empty or num is smaller or equal to the current minimum before pushing to minStack.
3
Add the pop method to remove numbers and update minimum
Add a method called pop that removes the top number from stack. If the removed number is equal to the last element in minStack, also remove it from minStack.
DSA Typescript
Hint
Remove the top from stack. If it equals the top of minStack, remove from minStack too.
4
Add the getMin method and print the minimum after operations
Add a method called getMin that returns the last element of minStack or null if minStack is empty. Then create a MinStack instance, push 5, 3, 7, pop once, and print the result of getMin().
DSA Typescript
Hint
Return the last element of minStack or null if empty. Then test by pushing 5, 3, 7, popping once, and printing getMin().
Practice
(1/5)
1. What is the main purpose of a Min Stack data structure?
easy
A. To retrieve the minimum element in constant time
B. To sort elements automatically
C. To store elements in ascending order
D. To remove duplicates from the stack
Solution
Step 1: Understand the Min Stack functionality
A Min Stack is designed to keep track of the minimum element efficiently while supporting normal stack operations.
Step 2: Identify the key feature
The key feature is retrieving the minimum element in constant time, which normal stacks cannot do efficiently.
Final Answer:
To retrieve the minimum element in constant time -> Option A
Quick Check:
Min Stack = constant time minimum retrieval [OK]
Hint: Min Stack always gives min element fast [OK]
Common Mistakes:
Thinking it sorts elements automatically
Confusing with normal stack behavior
Assuming it removes duplicates
2. Which of the following is the correct TypeScript method signature to push an element x onto a Min Stack class?
easy
A. push(x: number): number
B. push(x: string): number
C. push(x: number): void
D. push(): number
Solution
Step 1: Understand push method requirements
The push method should accept a number and return nothing (void) because it adds an element to the stack.
Step 2: Match method signature
push(x: number): void matches: it takes a number and returns void, which is correct for push operation.