Data Structures: Stack and Queue — Practice Questions
Question 1
How many stacks are needed to implement a queue? Consider the situation where no
other data structure like arrays or linked lists is available to you.
A) 1
B) 2
C) 3
D) 4
Answer: B) 2
Question 2
What is the time complexity of push and pop operations in a stack implemented using
an array?
A) O(n)
B) O(log n)
C) O(1)
D) O(n log n)
Answer: C) O(1)
Question 3
Predict the Output:
#include <stdio.h>
#define SIZE 3
int stack[SIZE];
int top = -1;
void push(int val) {
if (top == SIZE - 1) {
printf("Overflow ");
return;
}
stack[++top] = val;
}
void display() {
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
}
int main() {
push(1);
push(2);
push(3);
push(4); // Overflow
display();
return 0;
}
Options:
A) Overflow 3 2 1
B) 3 2 1 Overflow
C) Overflow 1 2 3
D) Compilation Error
Answer: A) Overflow 3 2 1
Question 4
Fill in the blanks to implement the peek function that returns the top element without
removing it:
int peek() {
if (________)
printf("Stack is empty\n");
else
return ________;
}
Answer:
int peek() {
if (top == -1)
printf("Stack is empty\n");
else
return stack[top];
}
Question 5
Which of the following permutation can be obtained in the same order using a stack
assuming that input is the sequence 5, 6, 7, 8, 9 in that order?
A) 7, 8, 9, 5, 6
B) 5, 9, 6, 7, 8
C) 7, 8, 9, 6, 5
D) 9, 8, 7, 5, 6
Answer: C) 7, 8, 9, 6, 5
Question 6
Convert the following infix expression into its equivalent postfix expression:
(A + B^ D) / (E – F) + G
A) ABD^ + EF – / G+
B) ABD + ^EF – / G+
C) ABD + ^EF / – G+
D) ABD^ + EF / – G+
Answer: A) ABD^ + EF – / G+
Question 7
The data structure required to check whether an expression contains a balanced
parenthesis is?
A) Stack
B) Queue
C) Array
D) Tree
Answer: A) Stack
Question 8
What is the outcome of the prefix expression:
*+, -, , 3, 2, /, 8, 4, 1
A) 12
B) 5
C) 11
D) 4
Answer: B) 5
Question 9
The five items P, Q, R, S, and T are pushed in a stack, one after the other starting from
P. The stack is popped four times and each element is inserted in a queue. Then two
elements are deleted from the queue and pushed back on the stack. Now one item is
popped from the stack. The popped item is:
A) P
B) R
C) Q
D) S
Answer: D) S
Question 10
Identify the error in the following stack implementation (using an array):
#define SIZE 5
int stack[SIZE];
int top = -1;
void push(int x) {
if (top == 5) {
printf("Stack Overflow");
} else {
stack[++top] = x;
}
}
int pop() {
if (top == -1) {
printf("Stack Underflow");
return -1;
} else {
return stack[top--];
}
}
}
What is the issue?
A) Incorrect condition for stack overflow
B) Incorrect condition for stack underflow
C) Array size exceeds capacity
D) No error
Answer: A) Incorrect condition for stack overflow
Question 11
Correct the error in the following stack implementation:
#include <stdio.h>
#define SIZE 5
int stack[SIZE];
int top = 0;
void push(int val) {
if (top == SIZE)
printf("Stack Overflow\n");
else
stack[top++] = val;
}
int pop() {
if (top == 0)
printf("Stack Underflow\n");
else
return stack[--top];
}
int main() {
push(1);
push(2);
printf("%d", pop());
return 0;
}
What’s wrong?
top should be initialized to -1, not 0.
In pop(), use --top instead of top--.
Corrected Code:
#include <stdio.h>
#define SIZE 5
int stack[SIZE];
int top = -1;
void push(int val) {
if (top == SIZE - 1)
printf("Stack Overflow\n");
else
stack[++top] = val;
}
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
else
return stack[top--];
}
int main() {
push(1);
push(2);
printf("%d", pop());
return 0;
}
Question 12
In linked list implementation of a queue, front and rear pointers are tracked. Which of
these pointers will change during an insertion into EMPTY queue?
A) Only front pointer
B) Only rear pointer
C) Both front and rear pointer
D) None
Answer: C) Both front and rear pointer
Question 13
What is the time complexity of enqueue and dequeue operations in a queue (using an
array)?
A) O(n)
B) O(log n)
C) O(1)
D) O(n log n)
Answer: C) O(1)
Question 14
In a circular queue of size N, what is the condition for the queue to be full?
A) front == rear + 1
B) rear == (front + 1) % N
C) (rear + 1) % N == front
D) rear == front
Answer: C) (rear + 1) % N == front
Question 15
What is the initial value of front and rear in an empty queue?
A) 0 and 0
B) -1 and -1
C) 1 and -1
D) 0 and -1
Answer: B) -1 and -1
Question 16
What is the time complexity of searching for an element in a queue?
A) O(1)
B) O(n)
C) O(log n)
D) O(n log n)
Answer: B) O(n)
Question 17
Which of the following applications uses a queue?
A) Recursion
B) CPU scheduling
C) Depth-first search
D) Function call stack
Answer: B) CPU scheduling
Question 18
What is the initial value of the front and rear in an empty queue implemented using an
array?
A) front = 0, rear = 0
B) front = -1, rear = -1
C) front = 0, rear = -1
D) front = -1, rear = 0
Answer: B) front = -1, rear = -1
Question 19
What will be the output of the following code?
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void enqueue(int val) {
if (rear == SIZE - 1) {
printf("Queue Overflow\n");
return;
}
if (front == -1) front = 0;
queue[++rear] = val;
}
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
}
return queue[front++];
}
int main() {
enqueue(10);
enqueue(20);
printf("%d ", dequeue());
enqueue(30);
printf("%d\n", dequeue());
return 0;
}
A) 10 20
B) 20 30
C) 10 30
D) Queue Overflow
Answer: C) 10 30
Question 20
Identify the error in the following queue implementation code:
void enqueue(int val) {
if (rear == SIZE) {
printf("Queue Overflow\n");
return;
}
queue[rear++] = val;
}
A) rear should be initialized to -1
B) Condition should check rear == SIZE - 1
C) front should be updated after each enqueue
D) No error
Answer: B) Condition should check rear == SIZE - 1