A stack is a collection of elements that follows the Last In, First Out (LIFO) principle. The last element added is the first one to be removed.
Click to reveal answer
beginner
What are the main operations of a stack?
The main operations are: 1. Push: Add an element to the top. 2. Pop: Remove the top element. 3. Peek/Top: View the top element without removing it. 4. IsEmpty: Check if the stack is empty.
Click to reveal answer
intermediate
How is a stack implemented using an array?
A stack can be implemented using an array by keeping a variable (top) to track the index of the last added element. Push adds an element at top+1, Pop removes the element at top, and top is updated accordingly.
Click to reveal answer
intermediate
What happens when you try to push an element into a full stack implemented with an array?
This situation is called stack overflow. Since the array has a fixed size, no more elements can be added beyond its capacity.
Click to reveal answer
beginner
What is the time complexity of push and pop operations in a stack implemented using an array?
Both push and pop operations have a time complexity of O(1) because they involve adding or removing an element at the top index without shifting other elements.
Click to reveal answer
Which principle does a stack follow?
AFirst In, First Out (FIFO)
BLast In, First Out (LIFO)
CRandom Access
DPriority Based
✗ Incorrect
A stack follows the Last In, First Out (LIFO) principle where the last element added is the first to be removed.
In a stack implemented using an array, what does the 'top' variable represent?
AThe first element in the stack
BThe total size of the array
CThe index of the last added element
DThe number of empty spaces
✗ Incorrect
'top' keeps track of the index of the last added element in the stack.
What happens if you pop from an empty stack?
AStack underflow error
BStack overflow error
CAdds a new element
DNothing happens
✗ Incorrect
Popping from an empty stack causes a stack underflow error because there are no elements to remove.
What is the time complexity of the push operation in a stack using an array?
AO(log n)
BO(n)
CO(n^2)
DO(1)
✗ Incorrect
Push operation is O(1) because it adds an element at the top index without shifting other elements.
Which condition indicates that a stack implemented with an array is full?
Atop == size - 1
Btop == 0
Ctop == -1
Dtop > size
✗ Incorrect
When top equals size - 1, the stack is full because the array is filled up to its last index.
Explain how to implement a stack using an array and how push and pop operations work.
Think about how you keep track of the last added element and how you add or remove elements.
You got /5 concepts.
Describe what stack overflow and stack underflow mean in the context of stack implementation using arrays.
Consider what happens when you try to add or remove elements beyond the stack's limits.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using an array in stack implementation?
easy
A. To store elements in a fixed-size linear structure
B. To allow random access to elements anywhere
C. To sort elements automatically
D. To store elements in a tree-like structure
Solution
Step 1: Understand stack storage
A stack stores elements in a linear order, last-in first-out (LIFO).
Step 2: Role of array in stack
An array provides a fixed-size linear structure to hold stack elements efficiently.
Final Answer:
To store elements in a fixed-size linear structure -> Option A
Quick Check:
Stack uses array for fixed linear storage [OK]
Hint: Stack uses array for fixed linear storage [OK]
Common Mistakes:
Confusing stack with queue or tree
Thinking array sorts elements automatically
Assuming stack allows random access
2. Which of the following is the correct syntax to declare a stack array of size 10 in C++?
easy
A. stack int[10];
B. int stack[10];
C. int stack();
D. array stack[10];
Solution
Step 1: Recall C++ array declaration syntax
In C++, arrays are declared as: type name[size];
Step 2: Match with options
int stack[10]; matches correct syntax: int stack[10];
Final Answer:
int stack[10]; -> Option B
Quick Check:
C++ array declaration = int stack[10]; [OK]
Hint: Array declaration: type name[size]; [OK]
Common Mistakes:
Using parentheses instead of brackets
Wrong order of type and name
Using non-existent 'array' keyword
3. What will be the output after executing the following code?
int stack[5];
int top = -1;
// Push 10
stack[++top] = 10;
// Push 20
stack[++top] = 20;
// Pop
int x = stack[top--];
// Push 30
stack[++top] = 30;
// Print top element
std::cout << stack[top];