Data Structure by Pankaj Sir
Data Structure by Pankaj Sir
Workbook
Workbook
CS / IT
No part of this publication may be reproduced or distributed in any form or by any means, electronic,
mechanical, photocopying, recording, or otherwise or stored in a database or retrieval system without
the prior written permission of the publishers. The program listings (if any) may be entered, stored
and executed in a computer system, but they may not be reproduced for publication.
Printing of books passes through many stages - writing, composing, proof reading, printing etc. We
try our level best to make the book error- free. If any mistake has inadvertently crept in, we regret it
and would be deeply indebted to those who point it out. We do not take any legal responsibility.
Edition : DS-CS-HPD-0522
GATE ACADEMY ®
Table of Contents
Sr. Chapter Name Page No
1. Array …………………….……….………….…………. 1
2. List ….…………………………..…..……….…………. 6
4. Tree ………………..……………….……….…………. 24
5. Graph ………….…………………………….…………. 40
6. Hashing ………………….………………….…………. 44
Video Lecture Information
Sr. Lecture Name Duration
Chapter 01 : Array
Lecture 01 Introduction To Data Structure 0:29:26
Lecture 02 One Dimensional Array 0:48:21
Lecture 03 Row Major Order Representation 0:49:06
Lecture 04 Column Major Order Representation 0:30:37
Lecture 05 More On RMO And CMO 0:22:19
Lecture 06 Workbook Question 1 Based On RMO 0:09:18
Lecture 07 Workbook Question 2 Based On RMO 0:08:39
Lecture 08 Workbook Question 3 Based On RMO 0:05:03
Lecture 09 Workbook Question 4 0:04:00
Lecture 10 Workbook Question 5 Based On RMO 0:09:32
Lecture 11 Workbook Question 6 Based On RMO 0:08:44
Lecture 12 Workbook Question 7 Based On RMO 0:09:33
Lecture 13 Workbook Question 8 Based On RMO 0:07:44
Lecture 14 Workbook Question 9 Based On RMO 0:06:56
Lecture 15 Workbook Question 10 Based On CMO 0:09:08
Lecture 16 Workbook Question 11 Based On CMO 0:11:17
Lecture 17 Workbook Question 12 Based On CMO 0:13:56
Lecture 18 Sparse Matrix 01 0:32:07
Lecture 19 Workbook Question 13 Based On Lower Triangular Matrix 0:09:26
Lecture 20 Sparse Matrix 02 0:17:21
Lecture 21 Sparse Matrix 03 0:17:10
Lecture 22 Sparse Matrix 04 0:13:51
Lecture 23 Sparse Matrix 05 0:18:01
Lecture 24 Workbook Question 14 Based On Tridiagonal Matrix 0:07:07
Lecture 25 Workbook Question 15 0:03:28
Lecture 26 Workbook Question 16 0:07:30
Lecture 27 Workbook Question 17 0:04:31
Lecture 28 Workbook Question 18 Based On Lower Triangular Matrix 0:10:22
Lecture 29 Workbook Question 19 Based On Z Matrix 0:08:50
Lecture 30 Workbook Question 20 0:08:42
Lecture 31 Workbook Question 21 0:09:41
Lecture 32 Workbook Question 22 0:06:26
Lecture 33 Workbook Question 23 0:10:07
Lecture 34 Workbook Question 24 0:09:18
Lecture 35 Workbook Question 25 0:08:39
Lecture 36 Workbook Question 26 0:06:44
Lecture 37 More On Arrays 0:44:21
Chapter 02 : List
Lecture 01 Linked List 01 0:37:48
Lecture 02 Linked List 02 0:32:53
Lecture 03 Linked List 03 0:24:00
Lecture 04 Linked List Traversal & Counting Number Of Nodes In LL 0:29:34
Lecture 05 Printing Last Node Data & Second Last Node Data Of LL 0:30:14
Lecture 06 Insertion At Beginning Of LL 0:31:45
Lecture 07 Insertion At End Of LL 0:27:29
Lecture 08 Deletion From LL 0:28:23
Lecture 09 Insertion & Deletion At Particular Position in LL 0:26:18
Lecture 10 Searching An Element In A Unsorted LL 0:13:16
Lecture 11 Printing Middle Node Data In LL 0:21:13
Lecture 12 Printing A Linked List In Reverse Order 0:28:10
Lecture 13 Reversing A Linked List Iteratively 0:30:56
Lecture 14 Types Of Linked List 0:52:28
Lecture 15 Workbook Question 1 0:09:13
Lecture 16 Workbook Question 2 0:06:31
Lecture 17 Workbook Question 3 0:07:23
Lecture 18 Workbook Question 4 0:12:49
Lecture 19 Workbook Question 5 0:08:32
Lecture 20 Workbook Question 6 0:08:32
Lecture 21 Workbook Question 7 0:14:44
Lecture 22 Workbook Question 8 0:09:40
Lecture 23 Workbook Question 9 0:06:48
Lecture 24 Workbook Question 10-11 0:09:18
Lecture 25 Workbook Question 12 0:09:52
Lecture 26 Workbook Question 13 0:08:17
Lecture 27 Workbook Question 14 0:07:13
Lecture 28 Workbook Question 15 0:06:05
Lecture 29 Workbook Question 16 0:10:11
Lecture 30 Workbook Question 17 0:11:43
Lecture 31 Workbook Question 18 0:06:08
Lecture 32 Workbook Question 19 0:09:56
Lecture 33 Workbook Question 20 0:06:54
Chapter 03 : Stacks and Queues
Lecture 01 Introduction 0:14:07
Lecture 02 Stack As An ADT 0:28:38
Lecture 03 Implementation Of Stack Using Arrays 0:34:35
Lecture 04 Implementation Of Stack As An ADT Using Structure 0:35:25
Lecture 05 Applications Of Stack 0:21:02
Lecture 06 Workbook Question 1 Based On Stack Operations 0:06:15
Lecture 07 Stack Permutations 01 0:20:39
Lecture 08 Stack Permutations 02 0:10:26
Lecture 09 Workbook Question 3 Based On Stack Permutations 0:10:56
Lecture 10 Why We Need Postfix 0:16:23
Lecture 11 Infix To Postfix Without Using Stack 0:16:37
Lecture 12 Workbook Question 4 Based On Infix To Postfix 0:06:59
Lecture 13 Infix To Postfix Using Stack 01 0:29:20
Lecture 14 Infix To Postfix Using Stack 02 0:30:04
Lecture 15 Infix To Prefix Without Using Stack 0:16:15
Lecture 16 Infix To Prefix Using Stack 01 0:19:42
Lecture 17 Infix To Prefix Using Stack 02 0:21:05
Lecture 18 Postfix Evaluation Using Stack 0:43:45
Lecture 19 Workbook Question 5 Based On Infix To Postfix Conversion 0:04:42
Lecture 20 Workbook Question 6 Based On Infix To Postfix Conversion 0:05:29
Lecture 21 Workbook Question 7 Based On Infix To Postfix Conversion 0:05:11
Lecture 22 Workbook Question 8 Based On Postfix Evaluation 0:05:11
Lecture 23 Workbook Question 9 Based On Postfix Evaluation 0:08:00
Lecture 24 Workbook Question 10 0:03:14
Lecture 25 Prefix Evaluation Using Stack & Without Using Stack 0:11:07
Lecture 26 Expression Tree 0:12:27
Lecture 27 Infix To Expression Tree 0:11:01
Lecture 28 Postfix To Expression Tree Using Stack 0:25:05
Lecture 29 Prefix To Expression Tree 0:13:17
Lecture 30 Implementation Of Multiple Stacks 01 0:27:15
Lecture 31 Implementation Of Multiple Stacks 02 0:10:51
Lecture 32 Workbook Question 11 Based On Multiple Stack Implementation 0:12:07
Lecture 33 Queue 01 0:33:27
Lecture 34 Queue 02 0:29:37
Lecture 35 Circular Queue 0:43:28
Lecture 36 Priority Queue 0:19:14
Lecture 37 Deque 0:18:13
Lecture 38 Tower Of Hanoi 0:24:11
Lecture 39 Tower Of Hanoi Analysis 0:20:06
Lecture 40 Analysis Of Fibonacci Series 0:25:30
Lecture 41 Tracing Recursion 0:35:12
Lecture 42 Head Tail Recursion 01 0:12:17
Lecture 43 Head Tail Recursion 02 0:12:29
Lecture 44 Workbook Question 12 0:07:13
Lecture 45 Workbook Question 13 0:03:58
Lecture 46 Workbook Question 14 Based On Priority Queue 0:10:32
Lecture 47 Workbook Question 15 On Stack & Queue 0:13:06
Lecture 48 Workbook Question 16 0:12:19
Lecture 49 Workbook Question 17 Based On Queue 0:06:14
Lecture 50 Workbook Question 18 0:06:47
Lecture 51 Workbook Question 19 Based On Postfix Evaluation 0:04:45
Lecture 52 Workbook Question 20 0:02:55
Lecture 53 Workbook Question 21 Based On Stack & Queue 0:14:59
Lecture 54 Workbook Question 22 0:21:26
Lecture 55 Workbook Question 23 0:08:28
Lecture 56 Workbook Question 24 0:05:48
Lecture 57 Workbook Question 25 0:06:22
Chapter 04 : Tree
Lecture 01 Tree Introduction 0:09:22
Lecture 02 Basic Terminology 0:31:42
Lecture 03 Binary Tree 0:33:36
Lecture 04 Some Standard Results 0:30:44
Lecture 05 Some Standard Results & Their Proof 0:27:27
Lecture 06 Important Results On K-Ary Tree 0:17:59
Lecture 07 Relation Between No Of Nodes With 2 Childs & Leaf Nodes 0:17:19
Lecture 08 Workbook Question 1 Based On K-Ary Tree 0:04:23
Lecture 09 Workbook Question 2 Based On K-Ary Tree 0:03:52
Lecture 10 Workbook Question 3 Based On K-Ary Tree 0:04:02
Lecture 11 Workbook Question 4 Based On K-Ary Tree 0:04:37
Lecture 12 Workbook Question 5 On Binary Tree 0:05:28
Lecture 13 Tree Traversal (Introduction) 0:10:23
Lecture 14 Pre Order Traversal (Part 1) 0:38:10
Lecture 15 Pre-Order Traversal (Part 2) 0:31:09
Lecture 16 Inorder Traversal 0:41:03
Lecture 17 PostOrder Traversal 0:30:09
Lecture 18 Number Of Unlabelled & Labelled Binary Trees With N Nodes 0:16:29
Lecture 19 Number Of Binary Trees With N Nodes & A Given Traversal 0:23:51
Lecture 20 Number Of Binary Trees Possible With Any 2 Traversal 0:22:12
Lecture 21 Constructing Unique Binary Tree From Given Inorder & Preorder (Part 1) 0:10:38
Lecture 22 Constructing Unique Binary Tree From Given Inorder & Preorder (Part 2) 0:15:44
Lecture 23 Shortcut Method To Construct Tree From Inorder & Preorder (Part 1) 0:06:31
Lecture 24 Shortcut Method To Construct Tree From Inorder & Preorder (Part 2) 0:04:58
Lecture 25 Constructing Binary Tree From Given Inorder & Postorder 0:20:59
Lecture 26 Constructing Binary Tree From Given Inorder & Postorder (Shortcut Method) 0:10:45
Lecture 27 Workbook Question 6 & 7 Based On Tree Traversal 0:07:53
Lecture 28 Workbook Question 8 & 9 Based On Tree Traversal 0:02:58
Lecture 29 Workbook Question 10 Based On Traversal 0:02:32
Lecture 30 Workbook Question 11 Based On Tree Traversal 0:03:38
Lecture 31 Workbook Question 12 Based On Tree Traversal 0:04:13
Lecture 32 Workbook Question 13 Based On Tree Traversal 0:11:45
Lecture 33 Workbook Question 14 Based On Tree Traversal 0:02:24
Lecture 34 Workbook Question 15 Based On Tree Traversal 0:02:30
Lecture 35 Binary Search Tree Introduction) 0:22:43
Lecture 36 Standard Result On Number Of BST Possible 0:23:58
Lecture 37 Inorder Traversal Of BST 0:21:50
Lecture 38 Insertion In BST 0:10:57
Lecture 39 Deletion Of A Leaf Node In BST 0:11:46
Lecture 40 Deletion Of A Node In BST (1 Child & 2 Childs) 0:34:46
Lecture 41 Workbook Question 16 Based On Binary Search Tree 0:06:34
Lecture 42 Workbook Question 17 Based On BST 0:02:02
Lecture 43 Workbook Question 18 Based On BST 0:05:03
Lecture 44 Workbook Question 19 Based On BST 0:02:32
Lecture 45 Workbook Question 20 Based On BST 0:02:50
Lecture 46 Workbook Question 21 Based On BST 0:09:25
Lecture 47 AVL Tree (Introduction) 0:28:42
Lecture 48 AVL Tree Insertion 0:37:15
Lecture 49 More Rotations 0:20:02
Lecture 50 AVL Tree Construction (Example 1) 0:16:18
Lecture 51 AVL Tree Construction (Example 2) 0:42:24
Lecture 52 Four Types Of Rotations (Quick Recap) 0:23:55
Lecture 53 Deletion From AVL Tree (Part 1) 0:26:33
Lecture 54 Deletion From AVL Tree (Part 2) 0:10:36
Lecture 55 Maximum & Minimum Number Of Nodes In AVL Tree Of Height H 0:31:07
Lecture 56 Size Balanced & Height Balanced Tree 0:36:21
Lecture 57 Workbook Question 22 Based On AVL Tree 0:04:34
Lecture 58 Workbook Question 23 Based On AVL Tree 0:06:04
Lecture 59 Workbook Question 24 Based On AVL Tree 0:04:36
Lecture 60 Workbook Question 25 Based On AVL Tree 0:06:15
Lecture 61 Workbook Question 26 Based On AVL Tree 0:03:41
Lecture 62 Heap (Introduction) 0:30:19
Lecture 63 Construction Of Heap By Inserting Nodes One After Another in Given Order 0:20:12
Lecture 64 Implementation Of CBT Using Array 0:14:48
Lecture 65 Given An Array Find Whether It Represents A Max (Heap Or Not) 0:20:09
Analysis of Heap Construction By Inserting Keys One After Another in Given
Lecture 66
Order 0:20:30
Lecture 67 Construct Max (Heap Using Build Heap Method) 0:25:20
Lecture 68 Max (Heapify) Part-1 0:13:12
Lecture 69 Max (Heapify) Part-2 0:14:29
Lecture 70 Analysis Of Build Heap Method 0:16:14
Lecture 71 Deletion From Heap 0:11:26
Lecture 72 Find Max & Min In A Heap 0:09:22
Lecture 73 Workbook Question 27 Based On Heap 0:02:39
Lecture 74 Workbook Question 28 Based On Heap 0:18:14
Lecture 75 Workbook Question 29 Based On Heap 0:03:34
Lecture 76 Workbook Question 30 Based On Heap 0:05:34
Lecture 77 Workbook Question 31 Based On Heap 0:03:18
Lecture 78 Workbook Question 32 Based On Heap 0:03:30
Lecture 79 Workbook Question 33 Based On Heap 0:08:48
Lecture 80 Workbook Question 34 Based On Heap 0:04:36
Lecture 81 Workbook Question 35 Based On Heap 0:05:37
Lecture 82 Workbook Question 36 Based On Heap 0:03:28
Lecture 83 Workbook Question 37 Based On Heap 0:02:23
Lecture 84 Workbook Question 38 Based On Heap 0:05:30
Chapter 05 : Graphs
Lecture 01 Graph Traversal (Part 1) 0:21:58
Lecture 02 Graph Traversal (Part 2) 0:05:47
Lecture 03 Graph Traversal (Part 3) 0:10:56
Lecture 04 BFS On Tree & Graph 0:36:38
Lecture 05 More BFS examples 0:06:52
Lecture 06 DFS (Part 1) 0:35:13
Lecture 07 DFS (Part 2) 0:24:06
Lecture 08 DFS (Part 3) 0:11:43
Lecture 09 Classification of edges 0:27:53
Lecture 10 Applications Of BFS & DFS 0:26:46
Lecture 11 Workbook Question 1 0:03:10
Lecture 12 Workbook Question 2 0:05:55
Lecture 13 Workbook Question 3 0:07:00
Lecture 14 Workbook Question 4 & 5 0:03:09
Lecture 15 Workbook Question 6 & 7 0:05:02
Lecture 16 Workbook Question 8 0:03:51
Chapter 06 : Hashing
Lecture 01 Introduction (Hashing) 0:29:32
Lecture 02 Linear Probing (Part 1) 0:30:50
Lecture 03 Linear Probing (Part 2) 0:10:12
Lecture 04 Quadratic Probing (Part 1) 0:18:12
Lecture 05 Quadratic Probing (Part 2) 0:17:42
Lecture 06 Double Hashing 0:15:32
Lecture 07 Separate Chaining & Comparison of all Collision resolution Techniques 0:31:20
Lecture 08 Workbook Question 1 0:02:36
Lecture 09 Workbook Question 2 0:03:12
Lecture 10 Workbook Question 3 0:03:34
Lecture 11 Workbook Question 4 0:04:20
Lecture 12 Workbook Question 5 0:02:26
Lecture 13 Workbook Question 6 0:04:19
Lecture 14 Workbook Question 7 0:04:45
Lecture 15 Workbook Question 8 0:05:57
Lecture 16 Workbook Question 9 0:02:37
Lecture 17 Workbook Question 10 0:01:57
Lecture 18 Workbook Question 11 0:03:29
1 ARRAY
Q.17 The elements of the triangular array are stored as a vector in the
A[1,1],A[2,1],A[2,2],A[3,1],A[3,2],A[3,3]….A[n,n]
Assuming that A[1,1] is stored at location 1, addressing function for A[i,j] is given by :
(A) (i-1)/2 + j
(B) ((i-1)*i)/2 +j
(C) (i*i) +j
(D) (i*j-1)/2
Q.18 Consider a 2D array A[40..95,40..95] in lower triangular matrix representation .The size of each element
in the array is 1 byte . If the array is implemented in the form of row major order and base address of the
array is 1000, the address of A[66][50] will be ______________
Q.19 Consider the integer array A[1..100,1..100] in which the elements are stored in Z representation. An
example of a 5 X 5 array in Z representation is shown below :
A11 A12 A13 A14 A15
A24
A33
A42
A51 A52 A53 A54 A55
If the base address of A is starting from 1000 onwards , size of each element is 1 byte and A is stored in
Row Major Order , then the address corresponding to A[100][55] is ___________________
Q.20 In a lower triangular matrix (size 15 × 15) representation of compact single dimension array ,non zero
elements (i.e. the elements of the lower triangle) of each row are stored one after another ,starting from
the first row .Assume each integer take 1 Byte and the array is stored in row major order and first element
of the array is stored at location 1000, then the address of the element a[10][6] is ______________
Q.21 An n × n matrix A where n ranging from 1 to n is defined as follows :
A[i,j] = i if(i==j)
A[i,j]= i*i - j*j if(i<j)
A[i,j]= j*j - i*i if(i>j)
The sum of elements of the array A is
(A) 0 (B) n2
(C) n(n + 1) / 2 (D) None of these
Q.22 Consider a 3D array A[90][30][40] stored in linear array in column major order. If the base address starts
at 10, then the location of A[20][20][30] is _______________.
Assume that the first element is stored at A[1][1][1] and each element takes 1 Byte of memeory.
Q.23 An n × n array v is defined as follows: 2000 1 marks
v[i, j] = i-j for all i, j, 1 <= i <= n, 1 <= j <= n
The sum of the elements of the array v is
(A) 0
(B) n – 1
(C) n2 – 3n + 2
(D) n2 (n + 1)/2
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 4 GATE ACADEMY®
Q.24 An n × n array v is defined as follows:
v[i, j] = i+ j for all i, j, 1 <= i <= n, 1 <= j <= n
The sum of the elements of the array v is _______________
Q.25 An n × n array v is defined as follows:
v[i, j] = i+j-2 for all i, j, 1 <= i <= n, 1 <= j <= n
The sum of the elements of the array v is __________________
Q.26 Suppose you are given an array s[1..n] and a procedure reverse (s, i, j) which reverses the order of elements
in a between positions i and j (both inclusive). What does the following sequence do, where 1 <= k <= n:
reverse(s, 1, k) ; [GATE 2000]
reverse(s, k + 1, n);
reverse(s, l, n);
(A) Rotates s left by k positions
(B) Leaves s unchanged
(C) Reverses all elements of s
(D) None of the above
Self-Practice Questions :
Q.1 A is an array of range[1…25,1…25] .It is stored at starting location of 300 and the size of each element
is 2 bytes .If RMO is considered for storing elements ,then the address of A[13,22] is :___________
Q.2 A is an array of range[-5…5,-10…10,-10…10] .It is stored at starting location of 300 and the size of each
element is 2 bytes .If RMO is considered for storing elements ,then the address of A[0,0,0] is __________
Q.3 A is an array of range[-5…5,-10…10,-10…10] .It is stored at starting location of 300 and the size of each
element is 2 bytes .If CMO is considered for storing elements ,then the address of A[0,0,0] is
____________
Q.4 A is an array of range[-5…5,-10…10] .It is stored at starting location of 300 and the size of each element
is 2 bytes .If RMO is considered for storing elements ,then the address of A[0,0] is ___________
Q.5 If the TOEPPLITZ matrix of order 20 X 20 is stored in One Dimensional array , the optimum number of
elements stored is : _____________
Q.6 A tridiagonal Matrix A[-4…1,4…9] is stored in RMO with base address 100. The address of A[0][7](if
non zero elements are stored and the size of each element is 1 byte) is __________
Q.7 Consider the integer array A[1..200,1..200] in which the elements are stored in Z representation. An
example of a 5 X 5 array in Z representation is shown below :
A11 A12 A13 A14 A15
A24
A33
A42
A51 A52 A53 A54 A55
If the base address of A is starting from 1000 onwards , size of each element is 1 byte and A is stored in
Row Major Order , then the address corresponding to A[100][23] is ______________
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 5 ARRAY
Q.8 In a lower triangular matrix A[1..20][1..20] representation of compact single dimension array ,non zero
elements (i.e. the elements of the lower triangle) of each row are stored one after another ,starting from
the first row .Assume each integer take 1 Byte and the array is stored in row major order and first element
of the array is stored at location 1000, then the address of the element a[19][4] is ______________
Q.9 Consider a 3D array A[-25…25][-30…30][-40…40] of character where each element is of 1 byte is stored
in linear array in column major order. If the base address starts at 1000,then the location of A[20][20][30]
is _______________.
Q.10 In a uppar triangular matrix A[1..20][1..20] representation of compact single dimension array ,non zero
elements (i.e the elements of the lower triangle) of each row are stored one after another ,starting from
the first row .Assume each integer take 1 Byte and the array is stored in row major order and first element
of the array is stored at location 1000, then the address of the element a[10][10] is ______________
Answers
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
2 LIST
Q.3 In the worst case, the number of comparisons needed to search a singly linked list of length n for a given
element is
(A) log 𝑛
(B) n/2
(C) log 𝑛 – 1
(D) n [2002 : 1 Mark]
Q.4 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) The list is empty or exactly one element.
(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) not all elements in the list have the same data value. [2003 : 2 Marks]
Q.5 What does the following function do for a given Linked List with first node as head?
void fun1(struct node* head)
{
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data);
}
(A) Prints all nodes of linked lists
(B) Prints all nodes of linked list in reverse order
(C) Prints alternate nodes of Linked List
(D) Prints alternate nodes in reverse order
Q.6 Which of the following points is/are true about Linked List data structure when it is compared with
array
(A) Arrays have better cache locality that can make them better in terms of performance.
(B) It is easy to insert and delete elements in Linked List
(C) Random access is not allowed in a typical implementation of Linked Lists
(D) The size of array has to be pre-decided, linked lists can change their size any time.
(E) All of the above
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 8 GATE ACADEMY®
Q.7 Consider the following function that takes reference to head of a Doubly Linked List as parameter.
Assume that a node of doubly linked list has previous pointer as prev and next pointer as next.
void fun(struct node **head_ref)
{
struct node *temp = NULL;
struct node *current = *head_ref;
while (current != NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL )
*head_ref = temp->prev;
}
Assume that reference of head of following doubly linked list is passed to above function 1 2 3 4 5 6.
What should be the modified linked list after the function call?
(A) 2 1 4 3 6 5
(B) 5 4 3 2 1 6.
(C) 6 5 4 3 2 1.
(D) 6 5 4 3 1 2
Q.8 The following function reverse() is supposed to reverse a singly linked list. There is one line missing
at the end of the function.
struct node
{
int data;
struct node* next;
};
/* head_ref is a double pointer which points to head (or start) pointer of linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 9 LIST
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 10 GATE ACADEMY®
Q.14 Consider an implementation of unsorted single linked list. Suppose it has its representation with a head
and a tail pointer (i.e. pointers to the first and last nodes of the linked list). Given the representation,
which of the following operation can not be implemented in O(1) time ?
(A) Insertion at the front of the linked list.
(B) Insertion at the end of the linked list.
(C) Deletion of the front node of the linked list.
(D) Deletion of the last node of the linked list.
Q.15 Consider a single linked list where F and L are pointers to the first and last elements respectively of the
linked list. The time for performing which of the given operations depends on the length of the linked
list? [ISRO CS 2018]
F L
(A) Delete the first element of the list
(B) Interchange the first two elements of the list
(C) Delete the last element of the list
(D) Add an element at the end of the list
Q.16 Which of the following operations is performed more efficiently by doubly linked list than by linear
linked list? [ISRO CS 2008]
(A) Deleting a node whose location is given
(B) Searching an unsorted list for a given item
(C) Inserting a node after the node with a given location
(D) Traversing the list to process each node
Q.17 The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of
the list. The list is represented as pointer to a structure. 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? [GATE 2005]
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)
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 11 LIST
{
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
Q.18 A singly linked list is declared as follows :
struct list
{
struct list* next;
int data;
};
Where next represents links to adjacent elements of the list. Which of the following code segments deletes
the element pointed by ptr from linked list, if it is assumed that ptr points to neither the first node nor the
last element of the list ?
prev pointer points to previous node
(A) prev->next = ptr->next ;free(ptr) ;
(B) ptr->next =prev->next ;free(ptr);
(C) prev->next =ptr->next ;free(ptr);
(D) ptr->next=prev->next ;free(prev);
Q.19 The following C function takes a simply-linked list as input argument. It modifies the list by moving the
last element to the front of the list and returns the modified list. Some part of the code is left blank.
[GATE 2010]
typedef struct node
{
int value;
struct node *next;
}Node;
Node *move_to_front(Node *head)
{
Node *p, *q;
if ((head == NULL: || (head->next == NULL))
return head;
q = NULL; p = head;
while (p-> next !=NULL)
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 12 GATE ACADEMY®
{
q = p;
p = p->next;
}
_______________________________
return head;
}
Choose the correct alternative to replace the blank line.
(A) q = NULL; p->next = head; head = p;
(B) q->next = NULL; head = p; p->next = head;
(C) head = p; p->next = q; q->next = NULL;
(D) q->next = NULL; p->next = head; head = p;
Q.20 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. [GATE 2017]
Self-Practice Questions :
Q.1 What is the run-time complexity of inserting a new element at the beginning of a circular, doubly-linked
list with a sentinel head?
(A) O(1) (B) O(log N)
(C) O(N) (D) O(N2)
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 13 LIST
Q.2 Given a circular, doubly-linked list whose contents are sorted in ascending order, what is the run-time
complexity for inserting a new element into the list so that it remains correctly sorted? (Including the
time required to search for the element’s correct position.)
(A) O(1) (B) O(log N)
(C) O(N) (D) O(N2)
Q.3 What is the best data structure to solve the following problem? A list needs to be built dynamically. Data
must be easy to find, preferably in O(1). The user does not care about any order statistics such as finding
max or min or median.
(A) Use an Array (B) Use a Singly LL
(C) Use a Stack (D) Use a Queue
Q.4 Which of the following application makes use of a circular linked list?
(A) Undo operation in a text editor (B) Recursive function calls
(C) Allocating CPU to resources (D) All of the mentioned
Q.5 Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst-
case time complexity of the best known algorithm to delete the node x from the list?
(A) O(n) (B) O(log2n)
(C) O(log n) (D) O(1)
Q.6 The concatenation of two lists is to be performed in O(1) time. Which of the following implementations
of a list should be used?
(A) singly linked list (B) doubly linked list
(C) circular doubly linked list (D) array implementation of lists
Q.7 Here is the code for this single linked list:
First
(i) P = First → Link → Link
(ii) P → Link → Link → Link = first
(iii) P = P → Link → Link → LINK
(iv) printf ("%C", P → data);
(A) A (B) B
(C) C (D) D
Q.10 What does the following function do for a given Linked List with first node as head ?
void fun1 (struct node* head)
{
if(head == NULL)
return;
fun1(head->next);
printf("%d head->data);
}
(A) Prints all nodes of linked lists
(B) Prints all nodes of linked list in reverse order
(C) Prints alternate nodes of Linked List
(D) Prints alternate nodes in reverse order
Answers
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
3 STACKS AND QUEUES
Front Rear
P ?
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 18 GATE ACADEMY®
(A) rear node
(B) front node
(C) not possible with a single pointer
(D) node next to front [2004 : 2Marks]
Q.18 A function f defined on stacks of integers satisfies the following properties. f(∅) = 0 and f (push (S, i)) =
max (f(S), 0) + i for all stacks S and integers i. If a stack S contains the integers 2, -3, 2, -1, 2 in order
from bottom to top, what is f(S)? [2005 : 1Mark]
(A) 6
(B) 4
(C) 3
(D) 2
Q.19 Consider the following C program: [2007 : 2Marks]
#include
#define EOF -1
void push (int); /* push the argument on the stack */
int pop (void); /* pop the top of the stack */
void flagError ();
int main ()
{ int c, m, n, r;
while ((c = getchar ()) != EOF)
{
if (isdigit (c) )
push (c);
else if ((c == '+') || (c == '*'))
{
m = pop ();
n = pop ();
r = (c == '+') ? n + m : n*m;
push (r);
}
else if (c != ' ')
flagError ();
}
printf("% c", pop ());
}
What is the output of the program for the following input ? 5 2 * 3 3 2 + * +
(A) 15
(B) 25
(C) 30
(D) 150
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 19 STACKS AND QUEUES
Q.20 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
(A) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
(B) Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n == REAR
(C) Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT
(D) Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT [2012 : 2Marks]
Q.21 Suppose a stack implementation supports an instruction REVERSE, which reverses the order of
elements on the stack, in addition to the PUSH and POP instructions. Which one of the following
statements is TRUE with respect to this modified stack?
(A) A queue cannot be implemented using this stack
(B) A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE takes a
sequence of two instructions
(C) A queue can be implemented where ENQUEUE takes a sequence of three instructions and
DEQUEUE takes a single instruction
(D) A queue can be implemented where both ENQUEUE and DEQUEUE take a single instruction each
[2014 : 2Marks]
Q.22 Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element
at the head of the queue Q without removing it from Q. Similarly Top(S) returns the element at the top
of S without removing it from S. Consider the algorithm given below.
while Q is not Empty do
if S is Empty OR Top( S) < Head (Q) then
x : = Dequene (Q);
Push ( S, x);
else
x : = Pop (S );
Enqueue ( Q, x);
end
end
The maximum possible number of iterations of the while loop in the algorithm is______
[2016 : 2Marks]
Q.23 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.
(A) I only
(B) II only
(C) Both I and II
(D) Neither I nor II [2017 : 1Mark]
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 20 GATE ACADEMY®
Q.24 A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail
pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let ‗enqueue‘ be
implemented by inserting a new node at the head, and ‗dequeue‘ be implemented by deletion of a node
from the tail.
head tail
Which one of the following is the time complexity of the most time-efficient implementation of ‘enqueue’
and ‘dequeue, respectively, for this data structure?
(A) Θ(1), Θ(1)
(B) Θ(1), Θ(n)
(C) Θ(n), Θ(1)
(D) Θ(n), Θ(n) [2018 : 1 Mark]
Q.25 Suppose you are given an implementation of a queue of integers. The operations that can be performed
on the queue are:
I. isEmpty (Q) — returns true if the queue is empty, false otherwise.
II. delete (Q) — deletes the element at the front of the queue and returns its value.
III. 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 ?
(A) Leaves the queue Q unchanged
(B) Reverses the order of the elements in the queue Q
(C) Deletes the element at the front of the queue Q and inserts it at the rear keeping the other elements
in the same order
(D) Empties the queue Q [2007 : 2Marks]
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 21 STACKS AND QUEUES
Self-Practice Questions :
Q.1 Let n insert and m (<=n) delete operations be performed in an arbitrary order on an empty queue Q. Let
x and y be the number of push and pop operations performed respectively in the process. Which one of
the following is true for all m and n?
(A) n+m <= x < 2n and 2m <= y <= n+m (B) n+m <= x < 2n and 2m<= y <= 2n
(C) 2m <= x < 2n and 2m <= y <= n+m (D) 2m <= x <2n and 2m <= y <= 2n
Q.2 Which of the following option is not correct?
(A) If the queue is implemented with a linked list, keeping track of a front pointer, Only rear pointer s
will change during an insertion into an non-empty queue.
(B) Queue data structure can be used to implement least recently used (LRU) page fault algorithm and
Quick short algorithm.
(C) Queue data structure can be used to implement Quick short algorithm but not least recently used
(LRU) page fault algorithm.
(D) Both (A) and (C)
Q.3 Consider a standard Circular Queue ‘q’ implementation (which has the same condition for Queue Full and
Queue Empty) whose size is 11 and the elements of the queue are q[0], q[1], q[2]…..,q[10].
The front and rear pointers are initialized to point at q[2] . In which position will the ninth element be
added?
(A) q[0] (B) q[1] (C) q[9] (D) q[10]
Q.4 Which one of the following is an application of Queue Data Structure?
(A) When a resource is shared among multiple consumers.
(B) When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes
(C) Load Balancing
(D) All of the above
Q.5 A priority queue can efficiently implemented using which of the following data structures? Assume that
the number of insert and peek (operation to see the current highest priority item) and extraction (remove
the highest priority item) operations are almost same.
(A) Array
(B) Linked List
(C) Heap Data Structures like Binary Heap, Fibonacci Heap
(D) None of the above
Q.6 A stack permutation is defined as the permutation that can be obtained by removing elements from a
stack with given order of insertion for ex- if the order of insertion is 1,2,3 then possible permutations are
:
(i) 1,2,3
(ii) 1,3,2
(iii) 2,1,3
(iv) 2,3,1
(v) 3,2,1
The number of stack permutations possible for n elements is :
(A) n! (B) 2n-1 (C) C(2n,n)/(n+1) (D) 2n + 3
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 22 GATE ACADEMY®
Q.7 Let X be the result when the postfix expression below is evaluated :
8 4 3+-242/+*3^2+
Let Y be the result of the following expression :
2X*7–
Then the value of Y1/3 is ___________
Q.8 Consider the following code
Void Fun(char * x)
{
if( (*x)!=’\0’)
{
Fun(x+1);
Fun(x+1);
Printf(“%c”,*x);
}
}
void main()
{
Fun(“RAM”);
}
The output is :
(A) MMAMMAR (B) MARRAM (C) RAMMRAM (D) RAMMAR
Q.9 An arithmetic tree is generated for the expression (-a + (b * c)) / (-d). Which operator is placed at the
root node ?
(A) + (B) – (C) * (D) /
Q.10 Consider the following pseudocode on stack1 and stack2, initially stack1 contains 4 elements and stack2
contains no elements.
Algo stackfun
while(stack1.pop(data))
stack2.push(data)
while(stack2.pop(data))
stack1.push(data)
(A) stack1 remains unchanged (B) Reversing the contents of stack1
(C) stack1 becomes empty (D) None of these
Q.11 Which one of the following is an application of Queue Data Structure?
(A) When a resource is shared among multiple consumers.
(B) When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes
(C) Load Balancing
(D) All of the above
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 23 STACKS AND QUEUES
Q.12 In linked list implementation of queue, if only front pointer is maintained, which of the following
operation take worst case linear time?
(A) Insertion (B) Deletion
(C) To empty a queue (D) Both Insertion and to empty a queue
Q.13 If the MAX_SIZE is the size of the array used in the implementation of circular queue. How is rear
manipulated while inserting an element in the queue?
(A) rear=(rear%1)+MAX_SIZE (B) rear=rear%(MAX_SIZE+1)
(C) rear=(rear+1)%MAX_SIZE (D) rear=rear+(1%MAX_SIZE)
Answers
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
4 TREE
Q.6 State whether the following statements are TRUE or FALSE: it is possible to construct a binary tree
uniquely whose pre-order and post-order traversals are given? [1987 : 1 Mark]
Q.7 Construct a binary tree whose preorder traversal is K L N M P R Q S T and inorder traversal is N L K P
RMSQT [1987 : 1 mark]
Q.8 If the binary tree in figure is traversed in inorder, then the order in which the nodes will be visited is
______ [1991 : 2 marks]
A
B E
C D F
G
Q.9 Which of the following sequences denotes the post order traversal sequence of the given tree?
a
b e
c d f
(A) f e g c d b a
(B) g c b d a f e
(C) g c d b f e a
(D) f e d g c b a [1996 : 1 Mark]
Q.10 Consider the lebel sequences obtained by the following pairs of traversal on labeled binary tree. Which
of these pairs identify a tree uniquely?
(i) preorder and postorder
(ii) inorder and postorder
(iii) preorder and inorder
(iv) level order and postorder
(A) (i) only
(B) (ii) and (iii)
(C) (iii) only
(D) (iv) only [2004 : 1 Mark]
Q.11 Which one of the following binary trees has its inorder and preorder traversals as BCAD and ABCD,
respectively? BCAD ABCD [2004 : 2 Marks]
(A) A (B) A
B C C D
D B
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 26 GATE ACADEMY®
(C) A (D) A
B B D
C D C
Q.12 The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The
postorder traversal of the binary tree is:
(A) d e b f g c a
(B) e d b g f c a
(C) e d b f g c a
(D) d e f g b c a [2007 : 2 Marks]
Q.13 Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder, inorder and
preorder traversal, respectively, of a complete binary tree. Which of the following is always true?
(A) LASTIN = LASTPOST
(B) LASTIN = LASTPRE
(C) LASTPRE = LASTPOST
(D) None of the above [2000 : 2 Marks]
Q.14 The following three are known to be the preorder, inorder and postorder sequences of a binary tree. But
it is not known which is which.
I. MBCAFHPYK
II. KAMCBYPFH
III. MABCKYFPH
Pick the true statement from the following.
(A) I and II are preorder and inorder sequences, respectively
(B) I and III are preorder and postorder sequences, respectively
(C) II is the inorder sequence, but nothing more can be said about the other two sequences
(D) II and III are the preorder and inorder sequences, respectively [2008 : 2 Marks]
Q.15 Consider the following New-order strategy for traversing a binary tree:
• Visit the root
• Visit the right subtree using New-order
• Visit the left subtree using New-order
The New-order traversal of the expression tree corresponding to the reverse polish expression 3 4 * 5 – 2
ˆ 6 7 * 1 + – is given by:
(A) + – 1 6 7 * 2 ˆ 5 – 3 4 *
(B) - + 1 * 6 7 ˆ 2 – 5 * 3 4
(C) – + 1 * 7 6 ˆ 2 – 5 * 4 3
(D) 1 7 6 * + 2 5 4 3 * – ˆ – [2016 : 2Marks]
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 27 TREE
Q.16 A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8. When the tree is traversed in pre-order
and the values in each node printed out, the sequence of values obtained is 5, 3, 1, 2, 4, 6, 8, 7. If the tree
is traversed in post-order, the sequence obtained would be
(A) 8, 7, 6, 5, 4, 3, 2, 1
(B) 1, 2, 3, 4, 8, 7, 6, 5
(C) 2, 1, 4, 3, 6, 7, 8, 5
(D) 2, 1, 4, 3, 7, 8, 6, 5 [2005 : 2 Marks]
Q.17 Postorder traversal of a given binary search tree, T produces the following sequence of keys 10, 9, 23,
22, 27, 25, 15, 50, 95, 60, 40, 29 Which one of the following sequences of keys can be the result of an
in-order traversal of the tree T? [2005 : 2 Marks]
(A) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(B) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(C) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Q.18 The numbers 1, 2, …. n are inserted in a binary search tree in some order. In the resulting tree, the right
subtree of the root contains p nodes. The first number to be inserted in the tree must be
(A) p
(B) p + 1
(C) n – p
(D) n – p + 1 [2005 : 1 Mark]
Q.19 While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search tree (BST) in the sequence
shown, the element in the lowest level is
(A) 65
(B) 67
(C) 69
(D) 83 [2015 : 1 Mark]
Q.20 The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15,
12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node
from the root)?
(A) 2
(B) 3
(C) 4
(D) 6 [2004 : 1 Mark]
Q.21 Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the
number 55. Which of the following sequences CANNOT be the sequence of nodes examined? 2006
2marks
(A) {10, 75, 64, 43, 60, 57, 55}
(B) {90, 12, 68, 34, 62, 45, 55}
(C) {9, 85, 47, 68, 43, 57, 55}
(D) {79, 14, 72, 56, 16, 53, 55}
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 28 GATE ACADEMY®
Q.22 What is the worst case time complexity of inserting n2 elements into an AVL Tree which contains n
elements initially ? [2020 : 1 Mark]
(A) θ(n 2 ) (B) θ(n 2 log n)
(C) θ(n 4 ) (D) θ(n3 )
Q.23 In a balanced BST with n elements . What is the worst case time complexity of reporting all the
elements in range[a,b]? Assume that the number of elements reported is k. [2020 : 1 Mark]
(A) O(n logk) (B) O(k logn)
(C) O(logn) (D) O(logn + k)
Q.24 Which of the following is TRUE? [2008 : 2 Marks]
(A) The cost of searching an AVL tree is θ (log n) but that of a binary search tree is O(n)
(B) The cost of searching an AVL tree is θ (log n) but that of a complete binary tree is θ (n log n)
(C) The cost of searching a binary search tree is O (log n ) but that of an AVL tree is θ(n)
(D) The cost of searching an AVL tree is θ (n log n) but that of a binary search tree is O(n)
Q.25 What is the maximum and minimum number of elements in an AVL search tree of height 4 (Assume
height of leaf node is 0)
(A) 31, 12
(B) 15, 13
(C) 31, 13
(D) None of these
Q.26 What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a
single node is 0.
(A) 2
(B) 3
(C) 4
(D) 5
Q.27 A max-heap is a heap where the value of each parent is greater than or equal to the values of its children.
Which of the following is a max-heap? [2011 : 1 Mark]
(A) (B)
10 10
8 6 8 6
5 2 4 5 1 2
4
(C) (D)
10 5
5 6 2 8
4 8 2 1 1 4 6 10
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 29 TREE
Q.28 The number of possible min-heaps containing each value from {1,2,3,4,5,6,7} exactly once is
___________ [2018 : 2 Marks]
Q.29 The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The
resultant Max Heap is. [2004 : 2 Marks]
(A) (B)
32 32
30 25 25 30
15 12 20 16 12 15 20 16
(C) (D)
32 32
30 25 25 30
15 12 16 20 12 15 16 20
Q.3 A binary search tree is generated by inserting in order the following integers:
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
The number of nodes in the left subtree and right subtree of the root respectively is [1996 : 2 Marks]
(A) (4, 7)
(B) (7, 4)
(C) (8, 3)
(D) (3, 8)
Q.4 A binary search tree is used to locate the number 43. Which one of the following probe sequence is not
possible?
(A) 61, 52, 14, 17, 40, 43
(B) 10, 65, 31, 48, 37, 43
(C) 81, 61, 52, 14, 41, 43
(D) 17, 77, 27, 66, 18, 43 [1996 : 2 Marks]
Q.5 A binary search tree contains the values 1,2,3,4,5,6,7,8. The tree is traversed in pre-order and the values
are printed out. Which of the following sequences is a valid output?
(A) 5 3 1 2 4 7 8 6
(B) 5 3 1 2 6 4 8 7
(C) 5 3 2 4 1 6 7 8
(D) 5 3 1 2 4 7 6 8 [1997 : 2 Marks]
Q.6 Which of the following statement is false?
(A) A tree with n nodes has (n-1) edges.
(B) A labeled rooted binary tree can be uniquely constructed given its postorder and preorder traversal
results.
(C) A complete binary tree with n internal nodes has (n+1) leaves.
(D) The maximum number of nodes in a binary tree of height h is (2^(h+1) -1). [1998 : 1 Mark]
Q.7 Consider the following nested representation of binary trees: (X Y Z) indicates Y and Z are the left and
right sub stress, respectively, of node X. Note that Y and Z may be NULL, or further nested. Which of
the following represents a valid binary tree?
(A) (1 2 (4 5 6 7))
(B) (1 (2 3 4) 5 6) 7)
(C) (1 (2 3 4)(5 6 7))
(D) (1 (2 3 NULL) (4 5)) [2000 : 1 Mark]
Q.8 Let T(n) be the number of different binary search trees on n distinct elements. Then
n
T ( n) = T ( k − 1) T ( x ) , where x is [2003 : 1 Mark]
k =1
(A) n-k+1
(B) n-k
(C) n-k-1
(D) n-k-2
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 32 GATE ACADEMY®
Q.9 Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary
search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order
traversal sequence of the resultant tree?
(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7
(C) 0 1 2 3 4 5 6 7 8 9
(D) 9 8 6 4 2 3 0 1 5 7 [2003 : 1 Mark]
Q.10 Consider the following C program segment
struct CellNode
{
struct CelINode *leftchild;
int element;
struct CelINode *rightChild;
}
int Dosomething(struct CelINode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if (ptr->leftChild != NULL)
value = 1 + DoSomething(ptr->leftChild);
if (ptr->rightChild != NULL)
value = max(value, 1 + DoSomething(ptr->rightChild));
}
return (value);
}
The value returned by the function DoSomething when a pointer to the root of a non-empty tree is
passed as argument is
(A) The number of leaf nodes in the tree
(B) The number of nodes in the tree
(C) The number of internal nodes in the tree
(D) The height of the tree [2005 : 2 Marks]
Q.11 Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree.
Which of these pairs identify a tree uniquely ?
(i) preorder and postorder
(ii) inorder and postorder
(iii) preorder and inorder
(iv) level order and postorder
(A) (i) only
(B) (ii), (iii)
(C) (iii) only
(D) (iv) only [2004 : 2 Marks]
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 33 TREE
Q.12 In a binary tree, for every node the difference between the number of nodes in the left and right subtrees
is at most 2. If the height of the tree is h > 0, then the minimum number of nodes in the tree is:
(A) 2
(B) 2 +1
(C) 2 – 1
(D) 2 [2005 : 2 Marks]
Q.13 How many distinct binary search trees can be created out of 4 distinct keys?
(A) 5
(B) 14
(C) 24
(D) 35 [2005 : 2 Marks]
Q.14 The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum
number of nodes in a binary tree of height h is:
(A) 2
(B) 2 -1
(C) 2 -1
(D) 2 [2007 : 1 Mark]
Q.15 The maximum number of binary trees that can be formed with three unlabeled nodes is:
(A) 1
(B) 5
(C) 4
(D) 3 [2007 : 2 Mark]
Q.16 When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40,
50, 70 80, 90 are traversed, not necessarily in the order given. How many different orders are possible in
which these key values can occur on the search path from the root to the node containing the value 60?
(A) 35
(B) 64
(C) 128
(D) 5040 [2007 : 2 Marks]
Q.17 Consider the following C program segment where CellNode represents a node in a binary tree:
struct CellNode
{
struct CellNOde *leftChild;
int element;
struct CellNode *rightChild;
};
int GetValue(struct CellNode *ptr)
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 34 GATE ACADEMY®
{
int value = 0;
if (ptr != NULL)
{
if ((ptr->leftChild == NULL) && (ptr->rightChild == NULL))
value = 1;
else
value = value + GetValue(ptr->leftChild) + GetValue(ptr->rightChild);
} return(value);
}
The value returned by GetValue() when a pointer to the root of a binary tree is passed as its argument is:
(A) the number of nodes in the tree
(B) the number of internal nodes in the tree
(C) the number of leaf nodes in the tree
(D) the height of the tree [2007 : 2 Marks]
COMMON DATA QUESTION
A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of
keys.
I. 81, 537, 102, 439, 285, 376, 305
II. 52, 97, 121, 195, 242, 381, 472
III. 142, 248, 520, 386, 345, 270, 307
IV. 550, 149, 507, 395, 463, 402, 270
Q.18 Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list
nodes in the order in which we could have encountered them in the search? [2008 : 2 Marks]
(A) II and III only
(B) I and III only
(C) III and IV only
(D) III only
Q.19 Which of the following statements is TRUE? [2008 : 2 Marks]
(A) I, II and IV are inorder sequences of three different BSTs
(B) I is a preorder sequence of some BST with 439 as the root
(C) II is an inorder sequence of some BST where 121 is the root and 52 is a leaf
(D) IV is a postorder sequence of some BST with 149 as the root
Q.20 How many distinct BSTs can be constructed with 3 distinct keys?
(A) 4
(B) 5
(C) 6
(D) 9 [2008 : 2 Marks]
COMMON DATA QUESTION
A binary tree with n > 1 nodes has n1, n2 and n3 nodes of degree one, two and three respectively. The
degree of a node is defined as the number of its neighbors.
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 35 TREE
Q R
S T U V
W
The order in which the nodes are visited during in-order traversal is
(A) SQPTRWUV
(B) SQPTURWV
(C) SQPTWUVR
(D) SQPTRUWV [2014 : 1 Mark]
Q.28 Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the
root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the
tree is of type treeNode. [2014 : 2 Marks]
typedef struct treeNode* treeptr;
struct treeNode
{
treeptr leftMostChild, rightSibling;
};
int DoSomething (treeptr tree)
{
int value=0;
if (tree != NULL)
{
if (tree->leftMostChild == NULL)
value = 1;
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 37 TREE
else
value = DoSomething(tree->leftMostChild);
value = value + DoSomething(tree->rightSibling);
}
return(value);
}
When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by
the function corresponds to the
(A) number of internal nodes in the tree.
(B) height of the tree.
(C) number of nodes without a right sibling in the tree.
(D) number of leaf nodes in the tree.
Q.29 Which of the following is/are correct inorder traversal sequence(s) of binary search tree(s)?
1. 3, 5, 7, 8, 15, 19, 25
2. 5, 8, 9, 12, 10, 15, 25
3. 2, 7, 10, 8, 14, 16, 20
4. 4, 6, 7, 9, 18, 20, 25
(A) 1 and 4 only
(B) 2 and 3 only
(C) 2 and 4 only
(D) 2 only [2015 : 1 Mark]
Q.30 The height of a tree is the length of the longest root-to-leaf path in it. The maximum and minimum
number of nodes in a binary tree of height 5 are
(A) 63 and 6, respectively
(B) 64 and 5, respectively
(C) 32 and 6, respectively
(D) 31 and 5, respectively [2015 : 1 Mark]
Q.31 Consider a binary tree T has 200 leaf nodes. Then the number of nodes in T have exactly two children
are____ [2015 : 1 Mark]
Q.32 The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search
tree, such that the resulting tree has height 6, is _____________ Note: The height of a tree with a single
node is 0. [2016 : 2Marks]
Q.33 Let T be a binary search tree with 15 nodes. The minimum and maximum possible heights of T are:
Note : The height of a tree with a single node is 0.
(A) 4 and 15 respectively
(B) 3 and 14 respectively
(C) 4 and 14 respectively
(D) 3 and 15 respectively [2017 : 1 Mark]
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 38 GATE ACADEMY®
Q.34 The pre-order traversal of a binary search tree is given by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20. Then
the post-order traversal of this tree is: [2017 : 2 Marks]
(A) 2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20
(B) 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
(C) 7, 2, 6, 8, 9, 10, 20, 17, 19, 15, 16, 12
(D) 7, 6, 2, 10, 9, 8, 15, 16, 17, 20, 19, 12
Q.35 The postorder traversal of a binary tree is 8, 9, 6, 7, 4, 5, 2, 3, 1. The inorder traversal of the same tree is
8, 6, 9, 4, 7, 2, 5, 1, 3. The height of a tree is the length of the longest path from the root to any leaf. The
height of the binary tree above is ________ . [2018 : 1 Mark]
Q.36 Let T be a full binary tree with 8 leaves. (A full binary tree has every level full.) Suppose two leaves a and
b of T are chosen uniformly and independently at random. The expected value of the distance between a
and b in T (i.e., the number of edges in the unique path between a and b) is (rounded off to 2 decimal
places) ___________ . [2019 : 2 Marks]
Q.37 The preorder traversal of a binary search tree is 15, 10, 12, 11, 20, 18, 16, 19. Which one of the following
is the postorder traversal of the tree ? [2020 : 1 Mark]
(A) 10, 11, 12, 15, 16, 18, 19, 20
(B) 11, 12, 10, 16, 19, 18, 20, 15
(C) 20, 19, 18, 16, 15, 12, 11, 10
(D) 19, 16, 18, 20, 11, 12, 10, 15
Q.38 A priority queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of
the heap is: 10, 8, 5, 3, 2.
Two new elements 1 and 7 are inserted into the heap in that order. The level-order traversal of the heap
after the insertion of the elements is:
(A) 10, 8, 7, 3, 2, 1, 5
(B) 10, 8, 7, 2, 3, 1, 5
(C) 10, 8, 7, 1, 2, 3, 5
(D) 10, 8, 7, 5, 3, 2, 1 [2005 : 2 Marks]
Q.39 Consider the following array of elements.
89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100
The minimum number of interchanges needed to convert it into a max-heap is
(A) 4
(B) 5
(C) 2
(D) 3 [2015 : 1 Mark]
Q.40 How many min heaps can be constructed from 5 distinct keys.
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 39 TREE
Answers
L M
N P Q
R S T
8. GCBDAFE
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
5 GRAPH
e b f
h
g
Among the following sequences:
I. a b e g h f
II. a b f e h g
III. a b f h g e
IV. a f g h b e
Which are depth first traversals of the above graph?
(A) I, II and IV only
(B) I and IV only
(C) II, III and IV only
(D) I, III and IV only [2003 : 1Mark]
Q.2 In a depth-first traversal of a graph G with n vertices, k edges are marked as tree edges. The number of
connected components in G is
(A) k
(B) k + 1
(C) n – k – 1
(D) n – k [2005 : 1 Mark]
Q.3 Let T be a depth first search tree in an undirected graph G. Vertices u and n are leaves of this tree T. The
degrees of both u and v in G are at least 2. which one of the following statements is true?
[2006 : 1 Marks]
(A) There must exist a vertex w adjacent to both u and v in G
(B) There must exist a vertex w whose removal disconnects u and v in G
(C) There must exist a cycle in G containing u and v
(D) There must exist a cycle in G containing u and all its neighbours in G.
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 41 TREE
Q.4 The most efficient algorithm for finding the number of connected components in an undirected graph on
n vertices and m edges has time complexity
(A) Θ(n)
(B) Θ(m)
(C) Θ(m+n)
(D) Θ(mn) [2008 : 1 Mark]
Q.5 The Breadth First Search algorithm has been implemented using the queue data structure. One possible
order of visiting the nodes of the following graph is
M N O
R Q P
(A) MNOPQR
(B) NQMPOR
(C) QMNPRO
(D) QMNPOR [2008 : 1 Mark]
Q.6 Let G be a graph with n vertices and m edges.What is the tightest upper bound on the running time of
Depth First Search on G, when G is represented as an adjacency matrix?
(A) Θ(n)
(B) Θ(n+m)
(C) Θ(n2)
(D) Θ(m2) [2014 (Set-1) : 1 Mark]
Q.7 Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that
a recursive call to visit a vertex is made only after first checking that the vertex has not been visited earlier.
Then the maximum possible recursion depth (including the initial call) is ____________.
[2014 (Set-1) : 1 Mark]
Q.8 Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex t
at a distance four from the root. If t is the n-th vertex in this BFS traversal, then the maximum possible
value of n is ________ [2016 : 1 Mark]
Q.9 In an adjacency list representation of an undirected simple graph G = (V, E), each edge (u, v) has two
adjacency list entries: [v] in the adjacency list of u, and [u] in the adjacency list of v. These are called
twins of each other. A twin pointer is a pointer from an adjacency list entry to its twin. If |E|= m and |V |
= n, and the memory size is not a constraint, what is the time complexity of the most efficient algorithm
to set the twin pointer in each entry in each adjacency list?
(A) Θ(𝑛 )
(B) Θ(m+n)
(C) Θ(𝑚 )
(D) Θ(𝑛 ) [2016 (Set-2) :1Mark]
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 42 GATE ACADEMY®
Self-Practice Questions :
Q.1 The maximum number of edges possible an undirected graph with 5 nodes ,when Depth First Search
(DFS) call is made on any random node in the graph result in stack size 5 i.e. 5 function calls are present
in stack simultaneously are _________
Q.2 Consider the following graph :
M N O
R Q P
The number of back edges in the BFS of this graph starting from node M is ____________
Q.3 Consider the following graph :
M N O
R Q P
The number of forward edges in the BFS of this graph starting from node M is ____________
Q.4 The maximum possible height of BFS tree , if BFS is run on a complete bipartite graph K m,n is :
(A) 3
(B) 2
(C) 1
(D) None of these
Q.5 Which of the following algorithms can be used to most efficiently determine the presence of a cycle in a
given graph ?
(A) Depth First Search
(B) Breadth First Search
(C) Prim's Minimum Spanning Tree Algorithm
(D) Kruskal's Minimum Spanning Tree Algorithm
Q.6 Traversal of a graph is different from tree because
(A) There can be a loop in graph so we must maintain a visited flag for every vertex
(B) DFS of a graph uses stack, but inorrder traversal of a tree is recursive
(C) BFS of a graph uses queue, but a time efficient BFS of a tree is recursive.
(D) All of the above
Q.7 Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-
first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after
visiting u in the traversal. Which of the following statements is always true? (GATE CS 2000)
(A) {u,v} must be an edge in G, and u is a descendant of v in T
(B) {u,v} must be an edge in G, and v is a descendant of u in T
(C) If {u,v} is not an edge in G then u is a leaf in T
(D) If {u,v} is not an edge in G then u and v must have the same parent in T
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 43 TREE
Q.8 Given two vertices in a graph s and t, which of the two traversals (BFS and DFS) can be used to find if
there is path from s to t?
(A) Only BFS
(B) Only DFS
(C) Both BFS and DFS
(D) Neither BFS nor DFS
Q.9 Which of the following condition is sufficient to detect cycle in a directed graph?
(A) There is an edge from currently being visited node to an already visited node.
(B) There is an edge from currently being visited node to an ancestor of currently visited node in DFS
forest.
(C) Every node is seen twice in DFS.
(D) None of the above
Q.10 Consider the tree arcs of a BFS traversal from a source node W in an unweighted, connected,
undirected graph. The tree T formed by the tree arcs is a data structure for computing.
(A) the shortest path between every pair of vertices.
(B) the shortest path from W to every vertex in the graph.
(C) the shortest paths from W to only those nodes that are leaves of T.
(D) the longest path in the graph
Answers
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
6 HASHING
(A) i only
(B) ii only
(C) i and ii only
(D) iii or iv [2004 : 1 Mark]
Q.4 A hash table contains 10 buckets and uses linear probing to resolve collisions. The key values are
integers and the hash function used is key % 10. If the values 43, 165, 62, 123, 142 are inserted in the
table, in what location would the key value 142 be inserted?
(A) 2
(B) 3
(C) 4
(D) 6 [2005 : 1Mark]
Q.5 Which of the following statement(s) is TRUE?
I. A hash function takes a message of arbitrary length and generates a fixed length code.
II. A hash function takes a message of fixed length and generates a code of variable length.
III. A hash function may give the same hash value for distinct messages.
(A) I only
(B) II and III only
(C) I and III only
(D) II only [2006: 1Mark]
Q.6 Consider a hash table of size 11 that uses open addressing with linear probing. Let h(k)=k mod 11 be the
hash function used. A sequence of records with keys 43 36 92 87 11 4 71 13 14 is inserted into an
initially empty hash table, the bins of which are indexed from zero to ten. What is the index of the bin
into which the last record is inserted?
(A) 3
(B) 4
(C) 6
(D) 7 [2008: 2Marks]
Q.7 The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of length 10 using
open addressing with hash function h(k) = k mod 10 and linear probing. What is the resultant hash table?
[2009 : 2 Marks]
(A) (B) (C) (D)
0 0 0 0
1 1 1 1
2 2 2 12 2 12 2 12, 2
3 23 3 13 3 13 3 13, 3, 23
4 4 4 2 4
5 15 5 5 5 3 5 5, 15
6 6 6 23 6
7 7 7 5 7
8 18 8 18 8 18 8 18
9 9 9 15 9
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 46 GATE ACADEMY®
Q.8 Consider a hash table with 9 slots. The hash function is ℎ(k) = k mod 9. The collisions are resolved by
chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum,
minimum, and average chain lengths in the hash table, respectively, are
(A) 3, 0, and 1
(B) 3, 3, and 3
(C) 4, 0, and 1
(D) 3, 0, and 2 [2014 (Set-1): 2 Marks]
Q.9 Consider a hash table with 100 slots. Collisions are resolved using chaining. Assuming simple uniform
hashing, what is the probability that the first 3 slots are unfilled after the first 3 insertions?
(A) (97 × 97 × 97)/ 100
(B) (99 × 98 × 97)/ 100
(C) (97 × 96 × 95)/ 100
(D) (97 × 96 × 95)/(3! × 100 ) [2014 (Set-3): 2 Marks]
Q.10 Given a hash table T with 25 slots that stores 2000 elements, the load factor α for T is __________
[2015 (Set-3): 1 Mark]
Q.11 Consider a double hashing scheme in which the primary hash function is h1(k) = k mod 23, and the
secondary hash function is h2(k) = 1+(k mod 19). Assume that the table size is 23. Then the address
returned by probe 1 in the probe sequence (assume that the probe sequence begins at probe 0) for key
value k = 90 is ________ . [2020 : 1 Mark]
Self-Practice Questions :
Q.1 Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. Assuming
the hash table is initially empty, which of the following is the contents of the table when the sequence 1,
3, 8, 10 is inserted into the table using closed hashing? Note that ‗_‘ denotes an empty location in the
table.
(A) 8, _, _, _, _, _, 10
(B) 1, 8, 10, _, _, _, 3
(C) 1, _, _, _, _, _,3
(D) 1, 10, 8, _, _, _, 3 [2007:2Marks]
Q.2 Consider a hash function that distributes keys uniformly. The hash table size is 20. After hashing of how
many keys will the probability that any new key hashed collides with an existing one exceed 0.5.
[2007 : 2 marks]
(A) 5
(B) 6
(C) 7
(D) 10
Linked question Answer for 3 and 4
A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing.
After inserting 6 values into an empty hash table, the table is as shown below.
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
GATE ACADEMY® 47 TREE
0
1
2 42
3 23
4 34
5 52
6 46
7 33
8
9
Q.3 Which one of the following choices gives a possible order in which the key values could have been
inserted in the table?
1. 46, 42, 34, 52, 23, 33
2. 34, 42, 23, 52, 33, 46
3. 46, 34, 42, 23, 52, 33
4. 42, 46, 33, 23, 34, 52 [2010:2Marks]
Q.4 How many different insertion sequences of the key values using the same hash function and linear
probing will result in the hash table shown above? [2010:2Marks]
(A) 10
(B) 20
(C) 30
(D) 40
Q.5 Which one of the following hash functions on integers will distribute keys most uniformly over
10buckets numbered 0 to 9 for i ranging from 0 to 2020?
(A) h(i) =𝑖 mod 10
(B) h(i) =𝑖 mod 10
(C) h(i) = (11 ∗ 𝑖 ) mod 10
(D) h(i) = (12 ∗ i) mod 10 [2015 (Set-2): 2 Marks]
Q.6 Consider an open address hash table with a total of 10000 slots containing 9800 entries .What is the
expected number of probes in a successful search ?
(A) 3 (B) 4 (C) 5 (D) 6
Q.7 A hash function h defined h(key)=key mod 7, with linear probing, is used to insert the keys 44, 45, 79,
55, 91, 18, 63 into a table indexed from 0 to 6. What will be the location of key 18?
(A) 3 (B) 4 (C) 5 (D) 6
Q.8 Consider a hash table of size m = 10000, and the hash function h(K) = floor (m(KA mod 1)) for A = (
√(5) – 1)/2. The key 123456 is mapped to location ______.
(A) 46 (B) 41 (C) 43 (D) 48
Q.9 Consider a 13 element hash table for which f(key)=key mod 13 is used with integer keys. Assuming
linear probing is used for collision resolution, at which location would the key 103 be inserted, if the
keys 661, 182, 24 and 103 are inserted in that order?
(A) 0 (B) 1 (C) 11 (D) 12
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in
Data Structures [Workbook] 48 GATE ACADEMY®
Q.10 Consider a hash table of size m = 100 and the hash function h(k) = floor(m(kA mod 1))
( 5 − 1)
for A = = 0.618033
2
Compute the location to which the key k = 123456 is placed in hash table.
(A) 77 (B) 82 (C) 88 (D) 89
Answers
Gate Academy Shop Address : Street 04, Narsingh Vihar, Katulbod, Bhilai 490022 (C.G.), Contact : 97131-13156 Online Test Series
https://www.gateacademy.shop/ Live class room: https://play.google.com/store/apps/details?id=com.gateacademy1 http://onlinetestseries.gateacademy.co.in