What if you could always know the smallest item in your stack instantly, no matter how big it is?
Why Min Stack Design in DSA C?
Imagine you have a stack of books and you want to quickly find the smallest book without taking all books out.
Checking each book one by one every time to find the smallest is slow and tiring, especially if the stack is tall.
A Min Stack keeps track of the smallest book at every step, so you can know the smallest instantly without searching.
int findMin(int stack[], int size) {
int min = stack[0];
for (int i = 1; i < size; i++) {
if (stack[i] < min) min = stack[i];
}
return min;
}typedef struct {
int value;
int currentMin;
} StackNode;
void push(StackNode stack[], int *top, int val) {
int min = (*top == -1) ? val : (val < stack[*top].currentMin ? val : stack[*top].currentMin);
stack[++(*top)] = (StackNode){val, min};
}
You can get the smallest value in the stack instantly, making your program faster and smarter.
In a game, quickly knowing the lowest score so far helps decide the next move without checking all scores again.
Manual search for minimum is slow and repetitive.
Min Stack stores minimum at each step for quick access.
Improves speed and efficiency in stack operations.
Practice
Min Stack data structure?Solution
Step 1: Understand Min Stack functionality
A Min Stack is designed to allow retrieval of the minimum element quickly, usually in constant time.Step 2: Compare with other options
Sorting or queue behavior is not the main goal of a Min Stack; it focuses on minimum element retrieval.Final Answer:
To retrieve the minimum element in constant time -> Option BQuick Check:
Min Stack = fast minimum retrieval [OK]
- Confusing Min Stack with sorting algorithms
- Thinking it behaves like a queue
- Assuming it reverses elements
Solution
Step 1: Identify required fields for Min Stack node
A Min Stack node needs to store the value, a pointer to the next node, and the minimum value at that node.Step 2: Match with options
typedef struct Node { int value; struct Node* next; int min; } Node; includes value, next pointer, and min field, which is correct for Min Stack implementation.Final Answer:
typedef struct Node { int value; struct Node* next; int min; } Node; -> Option AQuick Check:
Node stores value, next, and min [OK]
- Omitting the min field in node
- Using prev pointer instead of next
- Not using struct pointer for next
void push(Node** top, int x) {
Node* newNode = malloc(sizeof(Node));
newNode->value = x;
if (*top == NULL) {
newNode->min = x;
} else {
newNode->min = (x < (*top)->min) ? x : (*top)->min;
}
newNode->next = *top;
*top = newNode;
} What will be the minimum value after pushing elements 3, 5, 2 in this order on an empty stack?Solution
Step 1: Push 3 on empty stack
Stack top is NULL, so min = 3.Step 2: Push 5
Compare 5 with current min 3; min remains 3.Step 3: Push 2
Compare 2 with current min 3; min updates to 2.Final Answer:
2 -> Option AQuick Check:
Minimum after pushes = 2 [OK]
- Not updating min on push
- Confusing min with last pushed value
- Assuming min resets on each push
int pop(Node** top) {
if (*top == NULL) return -1;
int val = (*top)->value;
free(*top);
*top = (*top)->next;
return val;
}Solution
Step 1: Analyze order of operations
The code frees *top, then accesses (*top)->next, which is invalid after free.Step 2: Correct order
Should save next pointer before freeing current node to avoid invalid access.Final Answer:
Accessing (*top)->next after freeing *top -> Option CQuick Check:
Free before next access causes error [OK]
- Freeing node before accessing next
- Ignoring NULL check
- Returning value after free without saving
push, pop, and getMin all in O(1) time using two stacks. Which approach correctly implements getMin?Solution
Step 1: Understand two-stack Min Stack design
One stack stores all values; the second stack keeps track of minimums for O(1) retrieval.Step 2: Evaluate options
Maintain a second stack that stores the minimum values; push new min or duplicate current min on push correctly describes pushing new minimum or current minimum duplicate onto second stack.Final Answer:
Maintain a second stack that stores the minimum values; push new min or duplicate current min on push -> Option DQuick Check:
Two stacks: one for values, one for mins [OK]
- Sorting stack on every push (slow)
- Searching stack on getMin (not O(1))
- Using queue instead of stack
Ready to prove it?
Pass the test and this pattern moves unknown
