0% found this document useful (0 votes)
50 views

GATE Questions On DSA

Fifth

Uploaded by

tahirhussain.thm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

GATE Questions On DSA

Fifth

Uploaded by

tahirhussain.thm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

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

a. 20, 10, 20, 10, 20


b. 20, 20, 10, 10, 20
c. 10, 20, 20, 10, 20
d. 20, 20, 10, 20, 10

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

3. The postfix expression for the infixexpression A+B∗(C+D)/F+D∗E is:

• 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

a) (top1=MAXSIZE/2 and top2=MAXSIZE/2+1)


b) top1+top2=MAXSIZE
c) (top1=MAXSIZE/2) or (top2=MAXSIZE)
d) top1=top2−1

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. Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT


2. Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n ==REAR
3. Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT
4. Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT

14. The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is


a. 284
b. 213
c. 142
d. 71
15. A Circular queue has been implemented using singly linked list where each node consists of a value and a pointer
to next node. We maintain exactly two pointers FRONT and REAR pointing to the front node and rear node of
queue. Which of the following statements is/are correct for circular queue so that insertion and deletion
operations can be performed in O(1) time?
I. Next pointer of front node points to the rear node.

II. Next pointer of rear node points to the front node.

1. I only
2. II only
3. Both I and II
4. Neither I nor II

16. In a circular linked list organization, insertion of a record involvesmodification of


1. One pointer.
2. Two pointers.
3. Multiple pointers.
4. No pointer.

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;
};

int f(struct item *p)


{
return (
(p == NULL) ||
(p->next == NULL) ||
(( P->data <= p->next->data) && f(p->next))
);
}
For a given linked list p, the function f returns 1 if and only if
(A) not all elements in the list have the same data value.
(B) the elements in the list are sorted in non-decreasing order of data value
(C) the elements in the list are sorted in non-increasing order of data value
(D) None of them

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

21. Consider the C code fragment given below.


typedef struct node
{
int data;
node* next ;
} node;

void join(node* m, node* n)


{
node* p = n;
while (p->next != NULL)
{
p = p->next;
}
p–>next = m;
}
Assuming that m and n point to valid NULL- terminated linked lists, invocation of join will
(A) append list m to the end of list n for all inputs
(B) either cause a null pointer dereference or append list m to the end of list n
(C) cause a null pointer dereference for all inputs.
(D) append list n to the end of list m for all inputs.
22. Consider the queues Q1 containing four elements and Q2 containing none (shown as the Initial State in the figure).
The only operations allowed on these two queues are Enqueue (Q, element) and Dequeue (Q). The minimum
number of Enqueue operations on Q1 required to place the elements of Q1 in Q2 in reverse order (shown as the
Final State in the figure) without using any additional storage is

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

(D) Level Order

You might also like