📖 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 currently in the stack. This helps the app show the minimum value instantly without searching every time.
🎯 Goal: Build a MinStack in C that supports push, pop, top, and getMin operations all in constant time.
📋 What You'll Learn
Create a struct called MinStack with two integer arrays: stack and minStack, and an integer top for the current index.
Add a variable maxSize to set the maximum size of the stack.
Implement push to add a number to stack and update minStack with the current minimum.
Implement pop to remove the top element from both stack and minStack.
Implement top to return the top element of stack.
Implement getMin to return the current minimum from minStack.
Print the results of top and getMin after operations.
💡 Why This Matters
🌍 Real World
Min stacks are used in calculators, stock price trackers, and algorithms that need quick access to minimum values.
💼 Career
Understanding min stack design helps in coding interviews and building efficient data structures for real-time applications.
Progress0 / 4 steps
1
Create the MinStack struct
Create a struct called MinStack with three members: an integer array stack of size 100, an integer array minStack of size 100, and an integer top initialized to -1.
DSA C
Hint
Think of stack as the main stack and minStack as a helper stack to keep track of minimum values.
2
Initialize the MinStack
Write a function void initMinStack(struct MinStack *s) that sets s->top to -1 to initialize the stack as empty.
DSA C
Hint
Setting top to -1 means the stack is empty.
3
Implement push operation
Write a function void push(struct MinStack *s, int x) that increases s->top by 1, stores x in s->stack[s->top], and updates s->minStack[s->top] with the minimum between x and the previous minimum (or x if stack was empty).
DSA C
Hint
Remember to update minStack with the smaller value between the new element and the current minimum.
4
Implement pop, top, getMin and print results
Write functions void pop(struct MinStack *s) to decrease s->top, int top(struct MinStack *s) to return s->stack[s->top], and int getMin(struct MinStack *s) to return s->minStack[s->top]. Then create a main function that initializes a MinStack, pushes 3, 5, 2, 1, pops once, and prints the results of top and getMin after each operation as shown.
DSA C
Hint
Use pop to remove the top element and then print the current top and minimum values.
Practice
(1/5)
1. What is the main purpose of a Min Stack data structure?
easy
A. To reverse the order of elements
B. To retrieve the minimum element in constant time
C. To store elements in a queue fashion
D. To sort elements in ascending order
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 B
Quick Check:
Min Stack = fast minimum retrieval [OK]
Hint: Min Stack always tracks minimum element fast [OK]
Common Mistakes:
Confusing Min Stack with sorting algorithms
Thinking it behaves like a queue
Assuming it reverses elements
2. Which of the following is the correct way to declare a stack node structure for a Min Stack in C?
easy
A. typedef struct Node { int value; struct Node* next; int min; } Node;
B. typedef struct Node { int value; int min; } Node;
C. typedef struct Node { int value; struct Node* prev; } Node;
D. typedef struct Node { int value; struct Node* next; } Node;
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 A
Quick Check:
Node stores value, next, and min [OK]
Hint: Min Stack node needs value, next pointer, and min [OK]
Common Mistakes:
Omitting the min field in node
Using prev pointer instead of next
Not using struct pointer for next
3. Consider the following C code snippet for a Min Stack push operation:
What will be the minimum value after pushing elements 3, 5, 2 in this order on an empty stack?
medium
A. 2
B. 3
C. 5
D. Error
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 A
Quick Check:
Minimum after pushes = 2 [OK]
Hint: Min updates only if new value is smaller [OK]
Common Mistakes:
Not updating min on push
Confusing min with last pushed value
Assuming min resets on each push
4. The following C function is intended to pop an element from a Min Stack. Identify the error:
int pop(Node** top) {
if (*top == NULL) return -1;
int val = (*top)->value;
free(*top);
*top = (*top)->next;
return val;
}
medium
A. Not checking if stack is empty
B. Returning value before freeing node
C. Accessing (*top)->next after freeing *top
D. Missing return statement
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 C
Quick Check:
Free before next access causes error [OK]
Hint: Save next pointer before freeing node [OK]
Common Mistakes:
Freeing node before accessing next
Ignoring NULL check
Returning value after free without saving
5. You want to design a Min Stack that supports push, pop, and getMin all in O(1) time using two stacks. Which approach correctly implements getMin?
hard
A. Use a queue to track minimum values
B. Sort the main stack on every push to keep minimum on top
C. Traverse the main stack on every getMin call to find minimum
D. Maintain a second stack that stores the minimum values; push new min or duplicate current min on push
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 D
Quick Check:
Two stacks: one for values, one for mins [OK]
Hint: Use second stack to track mins for O(1) getMin [OK]
Common Mistakes:
Sorting stack on every push (slow)
Searching stack on getMin (not O(1))
Using queue instead of stack
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.