What is the main difference between a stack and a queue?
A stack follows Last In First Out (LIFO) order, meaning the last item added is the first to be removed. A queue follows First In First Out (FIFO) order, meaning the first item added is the first to be removed.
Click to reveal answer
beginner
When should you use a stack?
Use a stack when you need to reverse things, track history, or handle nested structures. Examples include undo features, parsing expressions, and backtracking algorithms.
Click to reveal answer
beginner
When is a queue the better choice?
Use a queue when you want to process items in the order they arrive, like in task scheduling, breadth-first search, or handling requests in order.
Click to reveal answer
intermediate
How does a stack help in function calls?
A stack keeps track of active function calls, storing return addresses and local variables. This helps the program return to the right place after a function finishes.
Click to reveal answer
intermediate
Why is a queue useful in breadth-first search (BFS)?
A queue processes nodes in the order they are discovered, ensuring BFS explores all neighbors at the current depth before moving deeper, which is essential for correct traversal.
Click to reveal answer
Which data structure would you use to implement an undo feature?
AStack
BQueue
CLinked List
DArray
✗ Incorrect
Undo features require reversing actions, which fits the Last In First Out (LIFO) behavior of a stack.
Which data structure processes elements in the order they arrive?
AStack
BQueue
CTree
DGraph
✗ Incorrect
A queue follows First In First Out (FIFO), so it processes elements in arrival order.
What order does a stack follow?
ALIFO
BSorted
CRandom
DFIFO
✗ Incorrect
Stack follows Last In First Out (LIFO) order.
Which data structure is best for breadth-first search (BFS)?
ASet
BStack
CHeap
DQueue
✗ Incorrect
BFS uses a queue to explore nodes level by level.
If you want to track nested tasks, which data structure is most suitable?
AQueue
BGraph
CStack
DHash Table
✗ Incorrect
Stacks are ideal for nested tasks because they handle last started tasks first.
Explain when you would choose a stack over a queue and give two real-life examples.
Think about situations where the last thing you did needs to be undone or finished first.
You got /4 concepts.
Describe why a queue is preferred for task scheduling and breadth-first search.
Consider how tasks or nodes should be handled in the order they arrive.
You got /4 concepts.
Practice
(1/5)
1. Which data structure should you use when you want to process tasks in the exact order they arrive, like customers waiting in line at a store?
easy
A. Queue, because it processes elements in First-In-First-Out order
B. Stack, because it processes elements in Last-In-First-Out order
C. Array, because it allows random access
D. Tree, because it organizes data hierarchically
Solution
Step 1: Understand the order of processing tasks
Tasks arriving in order and processed in the same order means First-In-First-Out (FIFO).
Step 2: Match FIFO with the correct data structure
Queue follows FIFO, so it fits the scenario of customers waiting in line.
Final Answer:
Queue, because it processes elements in First-In-First-Out order -> Option A
Quick Check:
FIFO = Queue [OK]
Hint: FIFO means use Queue, like a line of customers [OK]
Common Mistakes:
Confusing FIFO with LIFO
Choosing Stack for ordered processing
Thinking arrays maintain insertion order automatically
2. Which of the following is the correct way to push an element onto a stack in C?
easy
A. push(element, stack);
B. stack.push(element);
C. stack->push(element);
D. push(stack, element);
Solution
Step 1: Recall typical C stack function syntax
In C, stack operations are usually functions taking the stack and element as arguments.
Step 2: Identify correct function call order
push(stack, element) is the standard way to push an element onto a stack in C.
Final Answer:
push(stack, element); -> Option D
Quick Check:
Function call with stack first = push(stack, element) [OK]
Hint: In C, function calls usually have data structure first [OK]
Common Mistakes:
Using object.method() syntax which is not C style
Swapping argument order in function call
Assuming stack is a pointer with methods
3. What will be the output of the following C code snippet using a stack?
#include
#include
#define MAX 5
typedef struct {
int items[MAX];
int top;
} Stack;
void push(Stack *s, int val) {
if (s->top == MAX - 1) return;
s->items[++s->top] = val;
}
int pop(Stack *s) {
if (s->top == -1) return -1;
return s->items[s->top--];
}
int main() {
Stack s;
s.top = -1;
push(&s, 10);
push(&s, 20);
push(&s, 30);
printf("%d ", pop(&s));
printf("%d ", pop(&s));
return 0;
}
medium
A. 10 20
B. 30 20
C. 20 10
D. 30 10
Solution
Step 1: Trace push operations on stack
Push 10, stack: [10]; push 20, stack: [10,20]; push 30, stack: [10,20,30]. Top points to 30.
Step 2: Trace pop operations and output
First pop returns 30, second pop returns 20, so output is "30 20 ".
Final Answer:
30 20 -> Option B
Quick Check:
LIFO pop order = 30 then 20 [OK]
Hint: Stack pops last pushed first (LIFO) [OK]
Common Mistakes:
Assuming FIFO order for stack
Mixing push and pop order
Ignoring initial top = -1
4. The following C code tries to implement a queue but has a bug. What is the main issue?
#include
#define MAX 3
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int val) {
if (rear == MAX - 1) {
printf("Queue full\n");
return;
}
queue[++rear] = val;
if (front == -1) front = 0;
}
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue empty\n");
return -1;
}
return queue[front++];
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
dequeue();
enqueue(4);
printf("%d\n", queue[rear]);
return 0;
}
medium
A. The enqueue function uses wrong index for insertion
B. The dequeue function returns wrong element
C. The queue does not reset front and rear after dequeueing all elements, causing overflow
D. The queue size is too large
Solution
Step 1: Understand enqueue and dequeue behavior
Enqueue adds at rear, dequeue removes from front. After dequeueing all, front > rear but variables not reset.
Step 2: Identify problem with overflow after dequeue
After dequeueing, rear stays at max index, so enqueue thinks queue is full even if space is free.
Final Answer:
The queue does not reset front and rear after dequeueing all elements, causing overflow -> Option C
Quick Check:
Queue pointers not reset = overflow bug [OK]
Hint: Reset front and rear when queue becomes empty [OK]
Common Mistakes:
Ignoring front and rear reset after emptying queue
Thinking enqueue index is wrong
Assuming dequeue returns wrong element
5. You need to design a system that supports undo and redo operations for text editing. Which data structure(s) should you use and why?
hard
A. Two stacks: one for undo and one for redo, because stacks support LIFO order needed for undo/redo
B. One queue, because it processes commands in order
C. One stack only, because redo is not needed
D. Two queues, because they maintain order of commands
Solution
Step 1: Understand undo/redo operation order
Undo reverses last action (LIFO), redo re-applies undone actions in reverse order.
Step 2: Choose data structures matching LIFO behavior
Two stacks allow pushing actions for undo and redo separately, supporting LIFO order for both.
Final Answer:
Two stacks: one for undo and one for redo, because stacks support LIFO order needed for undo/redo -> Option A
Quick Check:
Undo/Redo = Two stacks (LIFO) [OK]
Hint: Undo/Redo needs two stacks for LIFO operations [OK]
Common Mistakes:
Using queues which are FIFO, not suitable for undo
Ignoring redo functionality
Using only one stack which can't handle redo
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.