📖 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 it at any time.
🎯 Goal: You will build a MinStack class in Java that supports push, pop, top, and getMin operations all in constant time.
📋 What You'll Learn
Create a class called MinStack with a constructor
Implement push(int val) to add a number to the stack
Implement pop() to remove the top number from the stack
Implement top() to return the top number without removing it
Implement getMin() to return the smallest number in the stack
Use only standard Java classes and no external libraries
💡 Why This Matters
🌍 Real World
Min stacks are useful in calculators, undo systems, and algorithms that need quick access to minimum values.
💼 Career
Understanding how to design efficient data structures like MinStack is important for software engineering interviews and building performant applications.
Progress0 / 4 steps
1
Create the MinStack class with two stacks
Create a public class called MinStack. Inside it, declare two private Stack<Integer> variables named stack and minStack. Initialize both stacks inside the constructor.
DSA Java
Hint
Use stack = new Stack<>(); and minStack = new Stack<>(); inside the constructor.
2
Implement the push method
Add a public method void push(int val) to the MinStack class. Inside it, push val onto stack. Then, push val onto minStack if minStack is empty or val is less than or equal to the top of minStack. Otherwise, push the current top of minStack again to keep track of the minimum.
DSA Java
Hint
Push val to stack. For minStack, push val if empty or smaller, else push the current minimum again.
3
Implement pop and top methods
Add two public methods to MinStack: void pop() which removes the top element from both stack and minStack, and int top() which returns the top element of stack without removing it.
DSA Java
Hint
Pop from both stacks in pop(). Return stack.peek() in top().
4
Implement getMin and print test output
Add a public method int getMin() to MinStack that returns the top element of minStack. Then, in the main method, create a MinStack object, push the numbers 5, 3, 7, and 2 in that order. Print the result of getMin(), then pop once, and print getMin() again.
DSA Java
Hint
Return minStack.peek() in getMin(). In main, push 5, 3, 7, 2, print getMin(), pop once, then print getMin() again.
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 along with stack operations
B. To sort elements in ascending order automatically
C. To store only the minimum elements pushed so far
D. To reverse the order of elements pushed into the stack
Solution
Step 1: Understand stack operations
A stack supports push, pop, and top operations in LIFO order.
Step 2: Identify Min Stack feature
Min Stack extends stack by allowing retrieval of minimum element in O(1) time.
Final Answer:
To retrieve the minimum element in constant time along with stack operations -> Option A
Quick Check:
Min Stack = constant time min retrieval [OK]
Hint: Min Stack always tracks minimum for O(1) min retrieval [OK]
Common Mistakes:
Thinking Min Stack sorts elements automatically
Assuming Min Stack stores only minimum elements
Confusing Min Stack with reversing stack order
2. Which of the following is the correct Java method signature to push an integer val onto a Min Stack?
easy
A. public int push(int val)
B. void push(int val) public
C. public void push(int val)
D. push public void(int val)
Solution
Step 1: Recall Java method syntax
Java method signature starts with access modifier, return type, method name, and parameters.
Step 2: Check each option
public void push(int val) follows correct syntax: public void push(int val). Others have wrong order or return type.
Final Answer:
public void push(int val) -> Option C
Quick Check:
Java method syntax = public void push(int val) [OK]
Hint: Java methods start with access modifier then return type [OK]
Common Mistakes:
Placing 'public' after method name
Using wrong return type for void methods
Incorrect parameter placement
3. Given the following operations on a Min Stack:
MinStack minStack = new MinStack();
minStack.push(3);
minStack.push(5);
minStack.push(2);
minStack.push(1);
minStack.pop();
int min = minStack.getMin();
System.out.println(min);