GATE Questions On DSA
GATE Questions On DSA
The following operations are performed on a stack: push(10), push(20), pop, push(10), push(20),
pop, pop, pop,push(20), pop. The sequence of values popped out is
2. Which of the following permutations can be obtained in the output (in the same order) using a stack
assuming that the inputis the sequence 1, 2, 3, 4, 5 in that order?
a. 3, 4, 5, 1, 2
b. 3, 4, 5, 2, 1
c. 1, 5, 2, 3, 4
d. 5, 4, 3, 1, 2
• AB+CD+∗F/D+E∗AB+CD+∗F/D+E∗
• ABCD+∗F/DE∗++ABCD+∗F/DE∗++
• A∗B+CD/F∗DE++A∗B+CD/F∗DE++
• A+∗BCD/F∗DE++
4. Consider the following statements:
I. First-in-first out types of computations are efficiently supported by STACKS.
II. Implementing LISTS on linked lists is more efficient than implementing LISTS on an array for
almost all the basic LIST operations.
III. Implementing QUEUES on a circular array is more efficient than implementingQUEUES
on a linear array with two indices.
IV. Last-in-first-out type of computations are efficiently supported by QUEUES.Which of the
following is correct?
a. (ii) and (iii) are true
b. (i) and (ii) are true
c. (iii) and (iv) are true
d. (ii) and (iv) are true
5. Which of the following is essential for converting an infixexpression to the postfix form efficiently?
a) An operator stack
b) An operand stack
c) An operand stack and an operator stack
d) A parse tree
6. A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from
opposite ends of the array. Variables top1 and top2 (top1<top2) point to the location of the topmost
element in each of the stacks. If the space is to be used efficiently, the condition for ―stack full‖ is
7. The best data structure to check whether anarithmetic expression has balanced parentheses is a
a) queue
b) stack
c) tree
d) list
8. A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To
which node should p point such that both the operations enQueue and deQueue can be performed in
constant time?
a) rear node
b) front node
c) not possible with a single pointer
d) node next to front
9. Assume that the operators +, -, × are left associative and ^ is right associative. The order of precedence
(from highest to lowest) is ^, x ,+, -. The postfix expression corresponding to the infix expression a +b × c –
d ^ e ^ f is
1. abc × + def ^ ^ –
2. abc × + de ^ f ^ –
3. ab + c × d – e ^ f ^
4. – + a × bc ^ ^ def
10. A program attempts to generate as many permutations as possible of the string, ‗abcd‘ by pushing the
characters a, b, c, d in the same order onto a stack, but it may pop off the top character at any time.
Which one of the following strings cannot be generated using this program?
a. abcd
b. dcba
c. cbad
d. cabd
11. The following postfix expression with single digit operands isevaluated using a stack:
823^/23*+ 51 * -
Note that ^ is the exponentiation operator. The top two elementsof the stack after the first * is evaluated
are:
a. 6, 1
b. 5, 7
c. 3, 2
d. 1, 5
12. Suppose you are given an implementation of a queue of integers. The operations that can be performed on
the queue are:
1. isEmpty (Q) — returns true if the queue is empty, false otherwise.
2. delete (Q) — deletes the element at the front of the queue and returns its value.
3. insert (Q, i) — inserts the integer i at the rear of the queue.
Consider the following function:
void f queue Q) {
int i ;
if (!isEmpty(Q))
{i = delete(Q);
f(Q);
insert(Q, i);
}
}
What operation is performed by the above function f ?
1. Leaves the queue Q unchanged
2. Reverses the order of the elements in the queue Q
3. Deletes the element at the front of the queue Q and inserts it at the rear keeping theother elements in
the same order
4. Empties the queue Q
13. Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n elements. Assume
that the insertion and deletion operation are carried out using REAR and FRONT as array index variables,
respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty are
1. I only
2. II only
3. Both I and II
4. Neither I nor II
17. Linked lists are not suitable data structures for which oneof the following problems?
1. Insertion sort
2. Binary search
3. Radix sort
4. Polynomial manipulation
18. Consider the function f defined below.
struct item
{
int data;
struct item * next;
};
19. The following C function takes a single-linked list of integers as a parameter and rearranges the elements of
the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What
will be the contents of the list after the function completes execution?
struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p, *
q; int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value =
temp; p = q-
>next;
q = p?p->next:0;
}
}
(A) 1,2,3,4,5,6,7
(B) 2,1,4,3,6,5,7
(C)1,3,2,5,4,7,6
(D)2,3,4,5,6,7,1
20. The following C function takes a single-linked list of integers as a parameter and rearranges the elements of
the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What
will be the contents of the list after the function completes execution?
struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p,
* q; int temp;
if ((!list) || !list-
>next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value =
temp; p = q-
>next;
q = p?p->next:0;
}
}
(A) 1,2,3,4,5,6,7
(B) 2,1,4,3,6,5,7
(C) 1,3,2,5,4,7,6
(D) 2,3,4,5,6,7,1
Correct answer is 6
23. The result evaluating the postfix expression 10 5 + 60 6 / ∗ 8 – is
(A) 284
(B) 213
(C) 142
(D) 71
24. How many distinct binary search trees can be created out of 4 distinct keys?
(A) 4
(B) 14
(C) 24
(D) 42
25. Which of the following traversal outputs the data in sorted order in a BST?
(A) Preorder
(B) Inorder
(C) Postorder