Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Algorithms & Data Structures CS-IT Workbook

Uploaded by

yashpunasya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Algorithms & Data Structures CS-IT Workbook

Uploaded by

yashpunasya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Algorithms

&
Data Structures
Workbook
Computer Science & Engineering
Information Technology
GATE & ESE
Algorithms & Data Structure
Workbook

Computer Science & Engineering


Information Technology

GATE & ESE

unacademy
Algorithms & Data Structure
Workbook
Computer Science & Engineering (CSE)
Information Technology (IT)

Copyrights ©All Rights Reserved


Sorting HAT Technologies Pvt. Ltd.

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 : ALGO&DS-2024/2025

Sorting HAT Technologies Pvt. Ltd.


353, 12th 'A' cross, off double road, Opp. City Centre
Residency, Indira nagar, 1st stage Bangalore
Bangalore KA 560038 IN.
GATE Syllabus
Algorithms : Searching, sorting, hashing. Asymptotic worst case time and
space complexity. Algorithm design techniques: greedy, dynamic programming
and divide, and conquer. Graph search, minimum spanning trees, and shortest
paths.
Data Structure : Arrays, Stacks, Queues, Linked lists, Trees, Binary search
trees, Binary heaps, Graphs.
Contents
Algorithm :
Sr. Chapters Pages
1. Complexity Analysis and Asymptotic Notations 1-9

2. Divide and Conquer 10 - 12

3. Sorting 13 - 16

4. Greedy Algorithms 17 - 25

5. Dynamic Programming 26 – 29

Data Structure :
Sr. Chapters Pages
1. Array 1-5

2. List 6 - 14

3. Stacks and Queues 15 - 23

4. Tree 24 - 49

5. Graph 50 - 52

6. Hashing 53 - 56
Algorithm
1 Complexity Analysis & Asymptotic Notations

Q.1 Solve the recurrence equations (A) h(n) is O(f(n))


T(n)  T(n  1)  n (B) h(n) is O(g(n))
T(1)  1 (C) g(n) is not O(f(n))
[GATE 1987 : IIT Bombay] (D) f(n) is O(g(n))
[GATE 2000 : IIT Kharagpur]
Q.2  O(n), where O(n)
1k n
stands for order n
Q.6 Let f(n)  n logn and g(n)  n(logn)10 be
2

is : two positive functions of n. Which of


(A) O(n) (B) O(n2 ) the following statements is correct?
(A) f(n)  O(g(n)) and g(n)  O(f(n))
(C) O(n3 ) (D) O(3n2 )
(B) g(n)  O(f(n)) and f(n)  O(g(n))
[GATE 1993 : IIT Bombay]
(C) f(n)  O(g(n)) and g(n)  O(f(n))
Q.3 Consider the following two functions :
(D) f(n)  O(g(n)) and g(n)  O(f(n))
n3 for 0  n  10,000
g 1 (n)   2 [GATE 2001 : IIT Kanpur]
 n for n  10,000
Q.7 The running time of the following
n for 0  n  100 algorithm procedure A(n) .
g 2 (n)   3
 n for n  100
Which of the following is true?
 
If n  2 return (1) else return A  n  ; 
is best described by
(A) g 1 (n) is O(g 2 (n)) (B) g1 (n)isO(n3 )
(A) O(n) (B) O(log n)
(C) g 2 (n) is O(g 1 (n)) (D) g 2 (n) is O(n) (C) O(log log n) (D) O(1)
[GATE 1994 : IIT Kharagpur] [GATE 2002 : IISc Bangalore]
Q.4 Which of the following is false? Q.8 Consider the following three claims
 nlog n  I. (n  k)m  (nm ), where k and m are
(A) 100nlog n  O  
 100  constants
II. 2n1  O(2n )
(B) log n  O(log log n)
III. 22n  O(2n )
(C) If 0  x  y then nx  O(ny )
Which of these claims are correct?
(D) 2n  O(nk )
(A) I and II (B) I and III
[GATE 1996 : IISc Bangalore] (C) II and III (D) I, II and III
Q.5 Consider the following functions : [GATE 2003 : IIT Madras]
f(n)  3n n
Q.9 Consider the following recurrence
relation
g(n)  2 n log 2 n

T(1)  1
h(n)  n!
Which of the following is true? T(n  1)  T(n)   (n  1)  for all n  1

Algorithm 1
The value of T(m2 ) for m  1 is T(0)  T(1)  1
m Which one of the following is FALSE?
(A) (21m  39)  4 (A) T(n)  O(n2 )
6
m (B) T(n)   (nlog n)
(B) (4m2  3m  5)
6 (C) T(n)   (n2 )
m (D) T(n)  O(nlog n)
(C) (3m2.5  11m  20)  5
2 [GATE 2005 : IIT Bombay]
m 5 Q.14 Consider the following C-program
(D) (5m3  34m2  137m  104)  fragment in which i, j and n are integer
6 6
variables.
[GATE 2003 : IIT Madras]
for (i  n, j  0;i  0;i/  2, j  i);
Q.10 The time complexity of the following C
function is (assume n  0 ) Let val( j) denote the value stored in
int recursive (int n) the variable j after termination of the
for loop. Which one of the following is
{ if (n = = 1)
true?
return (1);
(A) val ( j)  (log n)
else
(B) val ( j)  ( n)
return (recursive(n-1)+recursive (n-
1)) (C) val ( j)  (n)

} (D) val ( j)  (nlog n)


[GATE 2006 : IIT Kharagpur]
(A) O(n) (B) O(nlog n)
Q.15 Consider the following recurrence :
(C) O(n2 ) (D) O(2n )
[GATE 2004 : IIT Delhi]  
T(n)  2T  n   1T(1)  1
Q.11 The recurrence equation Which one of the following is true?
T(1)  1 (A) T(n)  (log log n)
T(n)  2T(n  1)  n,n  2 (B) T(n)  ( log n)

Evaluated to (C) T(n)  ( n)


(A) 2n1  n  2 (B) 2n  n (D) T(n)  ( n)
[GATE 2006 : IIT Kharagpur]
(C) 2n1  2n  2 (D) 2n  n
Q.16 Consider the following segment of C-
[GATE 2004 : IIT Delhi] code:
Q.12 Let T(n) be a function defined by the int j, n;
recurrence T(n) = 2T(n/2) + n for n ≥ j=1;
2 and T(1) = 1 Which of the following while (j<=n)
statements is TRUE? j=j*2;
(A) T(n)   (log n) (B) T(n)   ( n) The number of comparisons made in
the execution of the loop for any n>0
(C) T(n)   (n) (D) T(n)   (nlog n)
is:
[GATE 2005 : IIT Bombay] (A) log2 n  1 (B) n
Q.13 Suppose :
(C) log2 n (D) log2 n  1
n
T(n)  2T    n [GATE 2007 : IIT Kanpur]
2
2 Algorithm
Q.17 What is the time complexity of the evaluates to :
following recursive function? (A) (n)(log n  1) (B) (n)(logn)
int DoSomething (int n)
(C) (n) log (n) (D) nlog (n)
{
if (n<=2) [GATE 2008 : IISc Bangalore]
return 1; Q.21 Consider the following functions:
else f(n)  2n
return (DoSomething g(n)  n!
(floor(squrt(n)))+n); h(n)  nlogn
} Which of the following statements
(A) (n2 ) (B) (nlog 2 n) about the asymptotic behavior of f(n) ,
(C) (log 2 n) (D) (log 2 log 2 n) g(n) , and h(n) is true?
[GATE 2007 : IIT Kanpur] (A) f(n)  O(g(n)); g(n)  O(h(n))
Q.18 Consider the following C code segment:
(B) f(n)  (g(n)); g(n)  O(h(n))
int is prime(n)
{ (C) g(n)  O(f(n));h(n)  O(f(n))
int i, n ; (D) h(n)  O(f(n)); g(n)  (f(n))
for (i=2; <=sqrt(n); i++) Q.22 The running time of an algorithm is
if (n%1 = = 0) represented by the following
{ recurrence relation :
printf (“Not prime\n”);  n, n3
return 0; 
T(n)    n 
} T  3   cn, otherwise
  
return 1;
Which one of the following represents
}
the time complexity of the algorithm?
Let T(n) denote the number of times
(A)  (n) (B)  (nlog n)
the for loop is executed by the program
(C)  (n2 ) (D)  (n2 logn)
on input n. which of the following is
true? [GATE 2009 : IIT Roorkee]
Q.23 Which of the given options provides the
(A) T(n)  O( n) and T(n)  ( n)
increasing order of asymptotic
(B) T(n)  O( n) and T(n)  (1) Complexity of functions f1 , f2 , f3 and
(C) T(n)  O(n) and T(n)  ( n) f4 ?
(D) None of the above f1 (n)  2n ; f2 (n)  n3/2 ;
[GATE 2007 : IIT Kanpur] f3 (n)  nlogn2 ; f4 (n)  n 2 ;
log n

Q.19 Arrange the following functions in


(A) f3 , f2 , f4 , f1 (B) f3 , f2 , f1 , f4
increasing asymptotic order :
A. n1/3 B. en (C) f2 , f3 , f1 , f4 (D) f2 , f3 , f4 , f1
C. n7/4 D. nlog9n [GATE 2011 : IIT Madras]
E. 1.0000001n Q.24 The recurrence relation capturing the
(A) A, D, C, E, B (B) D, A, C, E, B optimal execution time of the Towers
(C) A, C, D, E, B (D) A, C, D, B, E of Hanoi problem with n discs is
[GATE 2008 : IISc Bangalore] (A) T(n)  2T(n  2)  2
Q.20 When n  22k for some k  0, the (B) T(n)  2T(n  1)  n
recurrence relation (C) T(n)  2T(n/2)  1
n (D) T(n)  2T(n  1)  1
T(n)  (2) T    n, T(1)  1
 2 [GATE 2012 : IIT Delhi]

Algorithm 3
Q.25 Which one of the following correctly (A) only i (B) only ii
determines the solution of recurrence (C) Both i and ii (D) Neither i nor ii
relation with T(1)  1 ? [GATE 2015 : IIT Kanpur]
n Q.29 The given diagram shows the flowchart
T(n)  2T    log n for a recursive function A(n). Assume
2
(A) (n) (B) (nlog n) that all statements, except for the
recursive calls, have O(1) time
(C) (n2 ) (D) (log n)
complexity. If the worst case time
[GATE 2014 : IIT Kharagpur]
n
complexity of this function is O(n ) ,
Q.26 Consider the equality i 3
 X and the then the least possible value(accurate
i 0
up to two decimal positions) of 
following choices for X is______.
I. (n4 ) II. (n5 )
III. O(n5 ) IV. (n3 )
The equality above remains correct if X
is replaced by
(A) Only I
(B) Only II
(C) I or III or IV but not II
(D) II or III or IV but not I
[GATE 2015 : IIT Kanpur]
Q.27 Consider the following C function [GATE 2016 : IISc Bangalore]
int fun1 (int n) Q.30 Consider the following functions from
{ positive integers to real numbers :
int i, j, k, p, q = 0; 100
for (i  1; i  n;  i) 10, n,n,log2 n,
n
{ The correct arrangement of the above
p  0; functions in increasing order of
for (j = n; j > 1; j= j\2) asymptotic complexity is :
++p; 100
(A) log2 n, , 10, n,n
for (k=1; k<p; k = k* 2) n
++q ; 100
} (B) , 10,log2 n, n,n
n
return q; 100
} (C) 10, , n,log2 n,n
n
Which one of the following most
100
closely approximated the return value (D) ,log2 n, 10, n,n
of the function fun1? n
[GATE 2017 : IIT Roorkee]
(A) n3 (B) n(logn)2
Q.31 Consider the recurrence function
(C) n(log n) (D) nlog (log n)
2T( n)  1, n
[GATE 2015 : IIT Kanpur] T(n)  
 2, 022
Q.28 Let f(n)  n and g(n)  n(1sinn) , where n is
a positive integer. Which of the Then T(n) in terms of  notation is
following statements is /are correct? (A) (log log n) (B) (log n)
i. f(n)  O(g(n)) (C) ( n) (D) (n)
ii. f(n)   (g(n)) [GATE 2017 : IIT Roorkee]

4 Algorithm
Q.32 Consider the following C function. Q.33 For parameters a and b, both of which
int fun(int n) are ω(1), T(n) = T(n1/a)+1, and T(b)=1.
Then T(n) is
{
(A) Θ(logalogbn) (B) Θ(logabn)
int i, j;
for (i = 1; i <= n ; i++) (C) Θ(logblogan) (D) Θ(log2log2n)

{ [GATE 2020 : IIT Delhi]

for (j = 1; j < n; j += i) Q.34 Consider the following function :


{ nlog n
f1  n, f2  100nlog n, f3  ,
printf("%d %d", i, j); 100
n
} f4  log n, f5 
log n
}
Identify the functions in increasing
}
order of growth?
Time complexity of fun in terms of θ
notation is : (A) f4  f5  f1  f2  f3

(A) (n n) (B) (n2 ) (B) f5  f4  f1  f2  f3

(C) (nlog n) (D) (n2 logn) (C) f1  f4  f5  f2  f3

[GATE 2015 : IIT Kanpur] (D) f1  f5  f4  f2  f3

Q.1 The concatenation of two lists is to be (C) log 2 n  1 (D) n


performed in O(1) time. Which of the
Q.4 Consider the following algorithm for
following implementations of a list searching for a given number x in an
should be used? unsorted array A 1......n having n
(A) Singly linked list distinct values :
(B) Doubly linked list 1. Choose an i uniformly at random
from 1….n;
(C) Circular doubly linked list
2. If A i  x then stop else goto 1;
(D) Array implementation of list
Assuming that x is present on A, what
Q.2 If n is a power of 2, then the minimum is the expected number of comparisons
number of multiplications needed to made by the algorithm before it
compute a n is : terminates?
(A) n (B) n  1
(A) log 2 n (B) n
(C) 2n (D) n / 2
(C) n  1 (D) n Q.5 The cube root of a natural number n is
Q.3 In the worst case, the number of defined as the largest natural number
comparisons needed to search a singly m such that m3  n. The complexity of
linked list of length n for a given computing the cube root of n(n is
element is : represented in binary notation) is :
(A) log 2 n (B) n / 2 (A) O(n) but not O(n0.5 )

Algorithm 5
(B) O(n0.5 ) but not O (log n)k  for any }
print (x);
constant k  0
(A) log m (B) m2
(C) O (log n) k
 for some constant k  0,
(C) m1/2 (D) m1/3
but not O (log log n)
m
 for any Q.9 Let A[1,……n] be an array storing a bit (1
constant m  0 or 0) at each location, and f(m) is a
function whose time complexity is
(D) O (loglogn)k  for some constant O(m) . Consider the following program
k  0.5, but not O (loglogn)0.5  fragment written in a C like language:
counter  0 ;
Q.6 In the following C program fragment, j,
k n and TwoLog_n are interger for (i  1;i  n;i   )
variables, and A is an array of integers. {
The variable n is initialized to an integer if (A[i] = = 1) counter ++;
≥ 3, and TwoLog_n is initialized to the else
value of 2*⌈log2(n)⌉ {
for (k = 3; k < = n; k++) if (counter) ; counter = 0;
A[k] = 0; }
}
for (k = 2; k < = TwoLog_n; k++)
The complexity of this program
for (j = k + 1; j < = n; j++) fragment is
A[j] = A[j] || (j % k); (A)  (n2 )
for (j = 3; j < = n; j++) (B) (nlogn)andO(n2 )
if (!A[j]) printf("%d", j); (C) (n)
The set of numbers printed by this
(D) O(nlog n)
program fragment is
Q.10 Consider the following C program.
(A) {m | m ≤ n, (∃ i) [m = i!]} Here i! mean
main()
factorial of i
{
(B) {m | m ≤ n, (∃ i) [m = i2]}
int x, y, m, n;
(C) {m | m ≤ n, m is prime}
scanf ("%d %d", &x, &y);
(D) {}
/* Assume x > 0 and y > 0 */
Q.7 The tightest lower bound on the
m = x;
number of comparisons, in the worst
n = y;
case, for comparison in the worst case,
while (m! = n)
for comparison-based sorting is of the
{
order of
if (m > n)
(A) n (B) n2
m = m - n;
(C) nlog n (D) nlog2n else
Q.8 What does the following algorithm n = n - m;
approximate? (Assume m  1,  0). }
x  m; print f ("% d", n);
y  1; }
The program computes
while (x  y )
(A) x ÷ y using repeated subtraction
{ (B) x mod y using repeated subtraction
x  (x  y) / 2; (C) the greatest common divisor of x
y  m / x; and y

6 Algorithm
(D) the least common multiple of x and Q.14 The minimum number of comparisons
y required to determine if an integer
Q.11 Let f(n), g(n) and h(n) be functions appears more than n/2 times in a sorted
defined for positive inter such that array of n integers is
f(n)  O(g(n)), g(n)  O(f(n)), (A) (n) (B) (log* n)
(C) (log n) (D) (1)
g(n)  O(h(n)) and h(n)  O(g(n)) .
Q.15 Suppose we have a balanced binary
Which one of the following statements search tree T holding n numbers. We
is FALSE? are given two numbers L and H and
(A) f(n)  g(n)  O ((h(n)  h(n)) wish to sum up all the numbers in T
that lie between L and H. Suppose
(B) f(n)  O(h(n))
there are m such numbers in T. If the
(C) fh(n)  O(f(n)) tightest upper bound on the time to
compute the sum is O(nalogb n + mc logd
(D) f(n)h(n)  O(g(n)h(n))
n), the value of a + 10b + 100c + 1000d
Q.12 The time complexity of computing the is _______.
transitive closure of a binary relation on Q.16 Consider a complete binary tree where
a set of n elements is known to be : the left and the right subtrees of the
(A) O(n) (B) O(nlog n) root are maxheaps. The lower bound
for the number of operations to convert
(C) O(n3/2 ) (D) O(n3 )
the tree to a heap is:
Q.13 Consider the following C-function : (A)  log n (B)  n
double foo (int n)
(C)  nlogn (D)  n2 
{
Q.17 There are n unsorted arrays: A1, A2, ....,
int i;
An. Assume that n is odd. Each of A1, A2,
double sum; ...., An contains n distinct elements.
if (n = = 0) return 1.0; There are no common elements
else between any two arrays. The worst-
{ case time complexity of computing the
median of the medians of A1, A2, ...., An
sum = 0.0;
is ________.
for (1= 0; i < n; i++) (A) Ο(n log n) (B) Ο(n2)
sum + = foo(i); (C) Ο(n) (D) Ω(n2log n)
return sum; Q.18 Let P1, P2, …, Pn be n points in the xy-
} plane such that no three of them are
collinear. For every pair of points Pi and
}
Pj, let Lij be the line passing through
Suppose we modify the above function them. Let Lab be the line with the
foo() and store the values of foo (i), 0 steepest gradient among all n(n−1)/2
< = i < n, as and when they are lines. The time complexity of the best
computed. With this modification, the algorithm for finding Pa and Pb is
time complexity for function foo() is (A) Θ(n) (B) Θ(nlogn)
significantly reduced. The space (C) Θ(nlog2n) (D) Θ(n2)
complexity of the modified function [GATE 2007 : IIT Kanpur]
would be:
Q.19 An array of n numbers is given, where n
(A) O(1) (B) O(n!) is an even number. The maximum as
(C) O(n) (D) O(nn ) well as the minimum of these n
numbers needs to be determined.
Algorithm 7
Which of the following is TRUE about (D) Sorted doubly liked list
the number of comparisons needed? Q.24 N items are stored in sorted doubly
(A) At least 2n  c comparisons, for linked list. For a delete operation, a
some constant c, are needed. pointer is provided to the record to be
(B) At most 1.5  2 comparisons are deleted. For a decrease-key operation,
needed. a pointer is provided to the record on
(C) At least nlog 2 n comparisons are which the operation is to be performed.
needed. An algorithm performs the following
operations on the list in this order;
(D) None of these
Q.20 What is the number of swaps required (N) delete O(log N) insert, o(log N)
to sort elements using selection sort, in find, and (N) decrease key. What is the
the worst case? time complexity of all these operations
(A) (n) (B) (n log n) put together?
(C) (n2 ) (D) (n2 logn) (A) (log2 N) (B) (N)
Q.21 Let W(n) and A(n) denote respectively,
(C) (N2 ) (D) (N2 logN)
the worst case and average case
running time of an algorithm executed
on an input of size n. Which of the 
following is ALWAYS true?
(A) A(n)  (W(n)) (B) A(n)  (W(n))
(C) A(n)  O(W(n)) (D) A(n)  o(W(n))
Q.22 An unordered list contains n distinct
elements. The number of comparisons
to find an element in this list that is
neither maximum nor minimum is :
(A) (nlog n) (B) (n)
(C) (log n) (D) (1)
Q.23 An algorithm performs (logN)1/2 find
operations, N insert operations,
(logN)1/2 delete operations, and
(logN)1/2 decrease-key operations on a
set of data items with keys drawn form
a linearly ordered set. For a delete
operation, a pointer is provided to the
record that must be deleted. For the
decrease-key operation, a pointer is
provided to the record that has its key
decreased. Which one of the following
data structures is the most suited for
the algorithm to use, if the goal is to
achieve the best asymptotic
complexity considering all the
operations?
(A) Unsorted array
(B) Min-heap
(C) Sorted array

8 Algorithm
Classroom Practice Questions
1. 0(n2) 2. B,C 3. A 4. B 5. D
6. A 7. C 8. A 9. B 10. D
11. A 12. C 13. C 14. C 15. B
16. D 17. D 18. B 19. A 20. B
21. D 22. A 23. A 24. D 25. A
26. C 27. D 28. D 29. 2.32 30. B
31. B 32. C 33. A 34. A
Self-Practice Questions
1. C 2. A 3. D 4. A 5. C
6. D 7. C 8. C 9. C 10. C
11. D 12. D 13. B 14. B 15. 110
16. A 17. B 18. A 19. B 20. A
21. C 22. D 23. A 24. C



Algorithm 9
2 Divide & Conquer

Q.1 Suppose we have a O(n) time algorithm Let T(n) be the number of comparisons
that finds median of an unsorted array. required to sort n elements. Then
Now consider a Quick Sort (A) T(n) <= 2T(n/5) + n
implementation where we first find (B) T(n) <= T(n/5) + T(4n/5) + n
median using the above algorithm, then (C) T(n) <= 2T(4n/5) + n
use median as pivot. (D) T(n) <= 2T(n/2)
What will be the worst case time Q.6 Let P be a Quick Sort Program to sort
complexity of this modified Quick Sort. numbers in ascending order using the
(A) O(n2 log n) (B) O(n2) first element as pivot. Let t 1 and t 2 be
(C) O(n log n log n) (D) O(n log n)
the number of comparisons made by P
Q.2 Suppose we are sorting an array of for the inputs {1, 2, 3, 4, 5} and {4, 1, 5,
eight integers using quicksort, and we 3, 2} respectively.
have just finished the first partitioning
Which one of the following holds?
with the array looking like this: 2 5 1 7
9 12 11 10. Which statement is correct? (A) t 1  5 (B) t 1  t 2
(A) The pivot could be either the 7 or (C) t 1  t 2 (D) t 1  t 2
the 9. Q.7 Assume that a merge sort algorithm in
(B) The pivot could be the 7, but it is the worst case takes 30 seconds for an
not the 9 input of size 64.
(C) The pivot is not the 7, but it could Which of the following most closely
be the 9 approximates the maximum input size
(D) Neither the 7 nor the 9 is the pivot. of a problem that can be solved in 6
Q.3 In a modified merge sort, the input minutes?
array is splitted at a position one-third (A) 256 (B) 512
of the length(N) of the array. (C) 1024 (D) 2048
What is the worst case time complexity
Q.8 Randomized quicksort is an extension
of this merge sort?
of quicksort where the pivot is chosen
(A) N(log3 N) (B) N(log2/3 N) randomly.
(C) N(log1/3 N) (D) N(log3/2 N) What is the worst case complexity of
Q.4 In quick sort, for sorting n elements, sorting n numbers using randomized
the (n/4)th smallest element is quicksort?
selected as pivot using an O(n) time
(A) O(n) (B) O(n log n)
algorithm. What is the worst case time
(C) O(n )
2
(D) O(n!)
complexity of the quick sort?
Q.9 You have an array of n elements.
(A) (n) (B) (nlog n)
Suppose you implement quicksort by
(C) (n2 ) (D) (n2 logn) always choosing the central element of
Q.5 Consider the Quicksort algorithm. the array as the pivot.
Suppose there is a procedure for Then the tightest upper bound for the
finding a pivot element which splits the worst case performance is
list into two sub-lists each of which (A) O (n2 ) (B) O (nlog n)
contains at least one-fifth of the
elements. (C) (nlog n) (D) O (n3 )

10 Algorithm
n (A) 0 (Nlog 3 N) (B) 0(Nlog2/3 N)
Q.1 Suppose there are 4 sorted lists of
4
(C) 0(Nlog1/3 N) (D) 0(Nlog3/2 N)
elements each. If we merge these lists
into a single sorted list of n elements, Q.8 Which of the following is true about
how many key comparisons are needed merge-sort?
in the worst case using an efficient (A) Merge sort works better than Quick
algorithm? sort if data is accessed from slow
6 7 sequential memory.
(A) n  3 (B) n  3
4 4 (B) Merge sort is stable sort by nature.
8 9
(C) n  3 (D) n  3 (C) Merge sort outperforms heap sort in
4 4 most of the practical situations.
Q.2 Given log n sorted lists each of size
(D) All of the above
n
. The total time complexity Q.9 The time complexity of matrix
log n
multiplication using DAC approach
required to merge them into 1 single (strassen’s Algo) is ______.
sorted list is ______.
Q.3 Let we have n strings each of length n, (A) O(nlog 5 ) (B) O(nlog 6 )
then the time complexity to sort them (C) O(nlog 7 ) (D) None of these
using 2-way merge is ______.
Q.10 Find the number of comparisons that
Q.4 The median of n elements can be found
will be needed n worst case to merge
in 0 (n) time, which one of the following
the following sorted files into a single
is correct about complexity of Quick sorted file by merging together 2 files
sort algorithm in which median is at a times
selected as pivot element.
(A)  (nlog n) (B)  (n2 ) Files f1 f2 f3 f4 f5
(C)  (n) (D) None of these Number
of 10 20 30 40 50
Q.5 Assuming the time complexity of
records
partition procedure to be nlog n, the
time complexity of Quick sort algorithm Q.11 A machine reach a minimum of 100 sec
in best case is : to sort 1000 names by using Quicksort.
The minimum time needed to sort 100
(A) O (nlog log n) (B) O (n)
names will be approximately ______.
(C) O (nlog n) (D) O(n2 )
Q.12 A machine took 200 sec to sort 200
Q.6 You need to sort 1 GB of data with only names using Quicksort. In 800 sec, it
100 MB of available main memory. can sort (approximately)
Which sorting technique will be most
(A) 400 names (B) 200 names
appropriate?
(A) Heap sort (B) Merge sort (C) 300 names (D) 800 names
(C) Quick sort (D) Insertion sort Q.13 If someone uses conventional 2-way
Q.7 In a modified merge sort, the input merge sort algorithm to sort the
array is splitted at a position one-third following elements in ascending order :
of the length (N) of the array. Which of 2, 8, 5, 13, 7, 1, 18, 9, 3, 100
the following is the tightest opper
then the order of these elements after
bound on time complexity of this
the 2nd pass of the algorithm is :
modified merge sort.
Algorithm 11
Q.14 Given an array of elements
9, 5, 2, 3, 4, 6, 7, 8, 0, 1
The output after 2 passes of 3-way
merge-sort is :
Q.15 Given two sorted of size m and n
respectively. The number of
comparisons needed in worst case by
merge sort will be:
(A) mn (B) min(m,n)
(C) max (m,n) (D) m  n  1
Q.16 Of the following sorting algorithms,
which has a running time that is least
dependent on the initial ordering of the
input?
(A) Merge sort (B) Insertion sort
(C) Selection sort (D) Quick sort
Q.17 The average number of comparisions
performed by merge sort algorithm to
merge 2 sorted lists of length 2 is :
8 8
(A) (B)
3 5
11 11
(C) (D)
7 6
n2
Hint : Use the formula
n2
n
: Number of elements in both sorted
2
array.



Classroom Practice Questions


1. D 2. A 3. D 4. B 5. B
6. C 7. B 8. C 9. A
Self - Practice Questions
1. B 2. 0(nloglogn) 3. B 4. A 5. D
6. B 7. D 8. D 9. C 10. 326
11. 6.7 sec 12. A 13. * 14. * 15. D
16. A 17. A

*13. 2, 5, 8,13,1, 7, 9,18, 3,100


*14. 0, 2, 3,4,5, 6, 7,8, 9,1


12 Algorithm
3 Sorting

Q.1 Which of the following is not a stable The array now looks like this: 16 14 15
sorting algorithm in its typical 10 12 27 28. How many heapify
implementation. operations have been performed on
(A) Insertion Sort (B) Merge Sort root of heap?
(A) 1 (B) 2
(C) Quick Sort (D) Bubble Sort
(C) 3 or 4 (D) 5 or 6
Q.2 Which of the following sorting
Q.6 You have to sort 1 GB of data with only
algorithms in its typical
100 MB of available main memory.
implementation gives best
Which sorting technique will be most
performance when applied on an array appropriate?
which is sorted or almost sorted
(A) Heap sort (B) Merge sort
(maximum 1 or two elements are
(C) Quick sort (D) Insertion sort
misplaced).
Q.7 What is the worst case time complexity
(A) Quick Sort (B) Heap Sort of insertion sort where position of the
(C) Merge Sort (D) Insertion Sort data to be inserted is calculated using
Q.3 Consider a situation where swap binary search?
operation is very costly. (A) O(N) (B) O(N log N)
Which of the following sorting (C) O(N2) (D) O(N(log N)2)
algorithms should be preferred so that Q.8 Which of the following sorting
the number of swap operations are algorithms has the lowest worst-case
minimized in general? complexity?
(A) Merge Sort (B) Bubble Sort
(A) Heap Sort (B) Selection Sort
(C) Quick Sort (D) Selection Sort
(C) Insertion Sort (D) Merge Sort
Q.9 The number of elements that can be
Q.4 Which of the following is not true about sorted in (log n) time using heap sort
comparison based sorting algorithms? is
(A) The minimum possible time (A) (1)
complexity of a comparison based
(B) ( log n)
sorting algorithm is O(n log n) for a
random input array (C) (log n / (log log n))
(B) Any comparison based sorting (D) (log n)
algorithm can be made stable by Q.10 In a permutation a 1 ..... a n of n distinct
using position as a criteria when
integers, an inversion is a pair (ai ,a j )
two elements are compared
such that i  j and ai  a j .
(C) Counting Sort is not a comparison
based sorting algorithm What would be the worst case time
(D) Heap Sort is not a comparison complexity of the Insertion Sort
based sorting algorithm. algorithm, if the inputs are restricted to
permutations of 1.....n with at most n
Q.5 Suppose we are sorting an array of
inversions?
eight integers using heapsort, and we
have just finished some heapify (either (A) T(n2 ) (B) T(nlog n)
maxheapify or minheapify) operations. (C) T(n1.5 ) (D) T(n)

Algorithm 13
Q.11 Which of the following changes to I. Quicksort runs in T(n2) time
typical QuickSort improves its II. Bubblesort runs in T(n2) time
performance on average and are
III. Mergesort runs in T(n) time
generally done in practice.
1. Randomly picking up to make worst IV. Insertion sort runs in T(n) time
case less likely to occur. (A) I and II only (B) I and III only
2. Calling insertion sort for small sized
(C) II and IV only (D) I and IV only
arrays to reduce recursive calls.
3. QuickSort is tail recursive, so tail Q.16 Which is the correct order of the
call optimizations can be done. following algorithms with respect to
4. A linear time median searching their time Complexity in the best case?
algorithm is used to pick the (A) Merge sort > Quick sort >Insertion
median, so that the worst case time sort > selection sort
reduces to O(n log n) (B) Insertion sort < Quick sort < Merge
(A) 1 and 2 (B) 2, 3, and 4 sort < selection sort
(C) 1, 2 and 3 (D) 2, 3 and 4
(C) Merge sort > selection sort > quick
Q.12 What is recurrence for worst case of
sort > insertion sort
QuickSort and what is the time
complexity in Worst case? (D) Merge sort > Quick sort > selection
(A) Recurrence is T(n) = T(n – 2) + O(n) sort > insertion sort
and time complexity is O(n2) Q.17 Which of the following statements is
(B) Recurrence is T(n) = T(n – 1) + O(n) correct with respect to insertion sort?
and time complexity is O(n2)
* Online - can sort a list at runtime
(C) Recurrence is T(n) = 2T(n/2) + O(n)
and time complexity is O(n log n) * Stable - doesn't change the relative
(D) Recurrence is T(n) = T(n/10) + order of elements with equal keys.
T(9n/10) + O(n) and time complexity (A) Insertion sort is stable, online but
is O(n log n) not suited well for large number of
Q.13 Which one of the following is the elements.
recurrence equation for the worst case (B) Insertion sort is unstable and online
time complexity of the Quicksort
(C) Insertion sort is online and can be
algorithm for sorting n(= 2) numbers?
applied to more than 100 elements
In the recurrence equations given in the
options below, c is a constant. (D) Insertion sort is stable & online and
(A) T(n) = 2T (n/2) + cn can be applied to more than 100
(B) T(n) = T(n – 1) + T(0) + cn elements
(C) T(n) = 2T (n – 2) + cn
(D) T(n) = T(n/2) + cn
Q.14 The worst case running times of
Insertion sort, Merge sort and Quick
sort, respectively, are :
(A) T(n log n), T(n log n) and T(n2)
(B) T(n2), T(n2) and T(n log n)
(C) T(n2), T(n log n) and T(n log n)
(D) T(n2), T(n log n) and T(n2)
Q.15 Assume that the algorithms considered
here sort the input sequences in
ascending order.
If the input is already in ascending
order, which of the following are TRUE?

14 Algorithm
Q.1 Given an unsorted array. Q.7 Which of the following is true about
The array has this property that every merge sort?
element in array is at most k distance (A) Merge Sort works better than quick
from its position in sorted array where sort if data is accessed from slow
k is a positive integer smaller than size sequential memory.
of array. Which sorting algorithm can be (B) Merge Sort is stable sort by nature
easily modified for sorting this array (C) Merge sort outperforms heap sort in
and what is the obtainable time most of the practical situations.
complexity? (D) All of the above.
(A) Insertion Sort with time complexity Q.8 Given an array where numbers are in
O(kn) range from 1 to n, which sorting
(B) Heap Sort with time complexity O(n algorithm can be used to sort these
log k) number in linear time?
(C) Quick Sort with time complexity O(k (A) Not possible to sort in linear time
log k) (B) Radix Sort
(D) Merge Sort with time complexity (C) Counting Sort
O(k log k) (D) Quick Sort
Q.2 What is the best time complexity of Q.9 In quick sort, for sorting n elements,
bubble sort? the (n/4)th smallest element is
(A) N2 (B) N log N selected as pivot using an O(n) time
(C) N (D) N(log N)2 algorithm.
Q.3 The tightest lower bound on the What is the worst case time complexity
number of comparisons, in the worst of the quick sort?
case, for comparison-based sorting is (A) (n) (B) (nlog n)
of the order of (C) (n2 ) (D) (n2 logn)
(A) N (B) N2 Q.10 Consider the Quicksort algorithm.
(C) N log N (D) N(log N)2 Suppose there is a procedure for
Q.4 Which sorting algorithm will take least finding a pivot element which splits the
time when all elements of input array list into two sub-lists each of which
are identical? Consider typical contains at least one-fifth of the
implementations of sorting algorithms. elements.
(A) Insertion Sort (B) Heap Sort Let T(n) be the number of comparisons
(C) Merge Sort (D) Selection Sort required to sort n elements. Then
Q.5 A list of n string, each of length n, is (A) T(n) <= 2T(n/5) + n
sorted into lexicographic order using (B) T(n) <= T(n/5) + T(4n/5) + n
the merge-sort algorithm. (C) T(n) <= 2T(4n/5) + n
The worst case running time of this (D) T(n) <= 2T(n/2) + n
computation is Q.11 Assume that we use Bubble Sort to sort
(A) O (n log n) (B) O (n2 log n) n distinct elements in ascending order.
(C) O (n2 + log n) (D) O (n2) When does the best case of Bubble
Q.6 Which sorting algorithms is most Sort occur?
efficient to sort string consisting of (A) When elements are sorted in
ASCII characters? ascending order
(A) Quick sort (B) Heap sort (B) When elements are sorted in
(C) Merge sort (D) Counting sort descending order

Algorithm 15
(C) When elements are not sorted by Q.14 What is the best sorting algorithm to
any order use for the elements in array are more
(D) There is no best case for Bubble than 1 million in general?
Sort. It always takes O(n*n) time (A) Merge sort (B) Bubble sort
Q.12 Consider an array of elements arr[5]= (C) Quick sort (D) Insertion sort
{5,4,3,2,1} , what are the steps of
insertions done while doing insertion Q.15 Which of the below given sorting
sort in the array. techniques has highest best-case
runtime complexity.
(A) 4 5 3 2 1 3 4 5 2 1 2 3 4 5 1 1 2 3 4 5 (A) Quick sort (B) Selection sort
(B) 5 4 3 1 2 5 4 1 2 3 5 1 2 3 4 1 2 3 4 5 (C) Insertion sort (D) Bubble sort
(C) 4 3 2 1 5 3 2 1 5 4 2 1 5 4 3 1 5 4 3 2

(D) 4 5 3 2 1 2 3 4 5 1 3 4 5 2 1 1 2 3 4 5
Q.13 The auxiliary space of insertion sort is
O(1), what does O(1) mean?
(A) The memory (space) required to
process the data is not constant.
(B) It means the amount of extra
memory Insertion Sort consumes
doesn't depend on the input. The
algorithm should use the same
amount of memory for all inputs.
(C) It takes only 1 kb of memory.
(D) It is the speed at which the
elements are traversed.

Classroom Practice Questions


1. C 2. D 3. B 4. D 5. B
6. B 7. C 8. A 9. C 10. D
11. C 12. B 13. B 14. D 15. D
16. B 17. A
Sell-Practice Questions
1. B 2. C 3. C 4. A 5. B
6. D 7. D 8. C 9. B 10. B
11. A 12. A 13. B 14. C 15. B



16 Algorithm
4 Greedy Algorithms

Q.1 The weighted external path length of Q.5 What is the weight of a minimum
the binary tree in figure is : spanning tree of the following graph?

[GATE 1991 : IIT Madras]


Q.2 Complexity of Kruskal’s algorithm for
finding the minimum spanning tree of (A) 29(B) 31
an undirected graph containing n (C) 38(D) 41
vertices and m edges if the edges are [GATE 2003 : IIT Madras]
sorted is _______. Q.6 Suppose we run Dijkstra’s single source
[GATE 1992 : IIT Delhi] shortest-path algorithm on the
following edge-weighted directed
Q.3 The minimum number of record
graph with vertex P as the source.
movements required to merge five files
A (with 10 records), B (with 20 records),
C (with 15 records), D (with 5 records)
and E (with 25 records) is :
(A) 165 (B) 90
(C) 75 (D) 65
[GATE 1999 : IIT Bombay]
Q.4 Let G be an undirected connected
graph with distinct edge weight. Let In what order do the nodes get
emax be the edge with maximum weight included into the set of vertices for
and emin the edge with minimum which the shortest path distances are
finalized?
weight. Which of the following
(A) P, Q, R, S, T, U (B) P, Q, R, U, S, T
statements is false?
(C) P, Q, R, U, T, S (D) P, Q, T, R, U, S
(A) Every minimum spanning tree of G
[GATE 2004 : IIT Delhi]
must contain emin Q.7 Consider the undirected graph below :
(B) If emax is in a minimum spanning
tree, then its removal must
disconnect G
(C) No minimum spanning tree contains
emax
(D) G has a unique minimum spanning
tree
[GATE 2000 : IIT Kharagpur]

Algorithm 17
Using Prim's algorithm to construct a (C) Graph G has multiple distinct MSTs,
minimum spanning tree starting with each of cost n-1.
node A, which one of the following (D) Graph G has multiple spanning trees
sequences of edges represents a of different costs.
possible order in which the edges
would be added to construct the [GATE 2005 : IIT Bombay]
minimum spanning tree? Q.10 Let G(V, E) be an undirected graph with
(A) (E, G), (C, F), (F, G), (A, D), (A, B), (A, positive edge weights. Dijkstra’s single-
C) source shortest path algorithm can be
implemented using the binary heap
(B) (A, D), (A, B), (A, C), (C, F), (G, E), (F,
data structure with time complexity :
G)
2
(C) (A, B), (A, D), (D, F), (F, G), (G, E), (F, (A) O ( V )
C)
(B) O (E  V log V )
(D) (A, D), (A, B), (D, F), (F, C), (F, G), (G,
E) (C) O ( V log V )
[GATE 2004 : IIT Delhi]
(D) O ((E  V )log V )
Q.8 In the following table, the left column
contains the names of standard graph [GATE 2005 : IIT Bombay]
algorithms and the right column
Q.11 Consider the polynomial
contains the time complexities of the
p(x)  a0  a1x  a2x  a3x
2 3
where
algorithms. Match each algorithm with
its time complexity. ai  0 i . The minimum number of
multiplications needed to evaluate p on
Bellman-Ford
1. A. O(m log n) an input x is :
algorithm
Kruskal’s (A) 3 (B) 4
2. B. O(n3)
algorithm (C) 6 (D) 9
Floyd-Warshall [GATE 2006 : IIT Kharagpur]
3. C. O(nm)
algorithm
Q.12 Consider a weighted complete graph G
4.
Topological
D. O(n + m) on the vertex set v 1 , v 2 ,....... vn such
sorting
(A) 1→ C, 2 → A, 3 → B, 4 → D
that the weight of the edge v , v 
i j is

(B) 1→ B, 2 → D, 3 → C, 4 → A 2i j. The weight of a minimum


(C) 1→ C, 2 → D, 3 → A, 4 → B spanning tree of G is:
(D) 1→ B, 2 → A, 3 → C, 4 → D (A) n  1 (B) 2n  2
[GATE 2005 : IIT Bombay] n
Q.9 An undirected graph G has n nodes. Its (C)   (D) n2
 2
adjacency matrix is given by n  n
square matrix whose [GATE 2006 : IIT Kharagpur]
i. diagonal elements are 0’s and Q.13 To implement Dijkstra’s shortest path
ii. Non-diagonal elements are 1’s. algorithm on unweighted graphs so
Which one of the following is TRUE? that it runs in linear time, the data
structure to be used is :
(A) Graph G has no minimum spanning
tree (MST). (A) Queue (B) Stack
(B) Graph G has a unique MST of cost (C) Heap (D) B-Tree
n-1 [GATE 2006 : IIT Kharagpur]

18 Algorithm
Q.14 Consider the following graph : Q.17 In an unweighted, undirected
connected graph, the shortest path
from a node S to every other node is
computed most efficiently, in terms of
time complexity by
(A) Dijkstra’s algorithm starting from S.
(B) Warshall’s algorithm
(C) Performing a DFS starting from S.
(D) Performing a BFS starting from S.
[GATE 2007 : IIT Kanpur]
Q.18 Let w be the minimum weight among
all edge weights in an undirected
Which one of the following cannot be
connected graph. Let e be a specific
the sequence of edges added, in that
edge of weight w. Which of the
order, to a minimum spanning tree
following is FALSE?
using Kruskal’s algorithm?
(A) (a-b), (d-f), (b-f), (d-c), (d-e) (A) There is a minimum spanning tree
containing e.
(B) (a-b), (d-f), (d-c), (b-f), (d-e)
(C) (d-f), (a-b), (d-c), (b-f), (d-e) (B) If e is not in a minimum spanning
tree T, then in the cycle formed by
(D) (d-f), (a-b), (b-f), (d-e), (d-c)
adding e to T, all edges have the same
[GATE 2006 : IIT Kharagpur]
weight.
Q.15 The characters a to h have the set of
(C) Every minimum spanning tree has
frequencies based on the first 8
an edge of weight w.
Fibonacci numbers as follows
a : 1, b : 1, c : 2, d : 3, e : 5, f : 8, (D) e is present in every minimum
spanning tree.
g : 13, h : 21
A Huffman code is used to represent [GATE 2007 : IIT Kanpur]
the characters. What is the sequence of .Common Data for Q.19 & Q.20:
characters corresponding to the
Suppose the letters a, b, c, d, e, f have
following code?
probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32
110111100111010 respectively.
(A) fdheg (B) ecgdf
Q.19 Which of the following is the Huffman
(C) dchfg (D) fehdg code for the letter a, b, c, d, e, f?
[GATE 2006 : IIT Kharagpur]
(A) 0, 10, 110, 1110, 11110, 11111
Q.16 Consider a weighted undirected graph
(B) 11, 10, 011, 010, 001, 000
with positive edge weights and let uv
be an edge in the graph. It is known (C) 11, 10, 01, 001, 0001, 0000
that the shortest path from the source (D) 110, 100, 010, 000, 001, 111
vertex s to u has weight 53 and the [GATE 2007 : IIT Kanpur]
shortest path from s to v has weight 65.
Q.20 What is the average length of Huffman
Which one of the following statements
codes?
is always true?
(A) 3 (B) 2.1875
(A) weight (u, v) ≤ 12
(B) weight (u, v) = 12 (C) 2.25 (D) 1.9375
(C) weight (u, v) > 12 [GATE 2007 : IIT Kanpur]
(D) weight (u, v) ≥ 12 Q.21 Consider the DAG with
[GATE 2007 : IIT Kanpur] V  1, 2, 3, 4, 5, 6 , shown below.

Algorithm 19
Which of the following is NOT a
topological ordering? (A) only vertex a
(A) 1 2 3 4 5 6 (B) 1 3 2 4 5 6 (B) only vertices a, e, f, g, h
(C) only vertices a, b, c, d
(C) 1 3 2 4 6 5 (B) 3 2 4 1 6 5
(D) all the vertices
[GATE 2007 : IIT Kanpur]
[GATE 2008 : IISc Bangalore]
Q.22 The most efficient algorithm for Q.25 Consider the following graph :
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)
[GATE 2008 : IISc Bangalore] Which one of the following is NOT the
Q.23 For the undirected, weighted graph sequence of edges added to the
given below, which of the following minimum spanning tree using Kruskal’s
sequences of edges represents a algorithm?
correct execution of Prim's algorithm to (A) (b, e) (e, f) (a, c) (b, c) (f, g) (c, d)
construct a Minimum Spanning Tree? (B) (b, e) (e, f) (a, c) (f, g) (b, c) (c, d)
(C) (b, e) (a, c) (e, f) (b, c) (f, g) (c, d)
(D) (b, e) (e, f) (b, c) (a, c) (f, g) (c, d)
[GATE 2009 : IIT Roorkee]
.Common Data for Q.26 & Q.27:
Consider a complete undirected graph with
vertex set {0, 1, 2, 3, 4}. Entry Wi, j in the
matrix W below is the weight of the edge {i,
j}.
(A) (a, b), (d, f), (f, c), (g, i), (d, a), (g, h), 0 1 8 1 4 
(c, e), (f, h)  1 0 12 4 9 
(B) (c, e), (c, f), (f, d), (d, a), (a, b), (g, h),  
W  8 12 0 7 3 
(h, f), (g, i)  
(C) (d, f), (f, c), (d, a), (a, b), (c, e), (f, h),  1 4 7 0 2
4 9 3 2 0 
(g, h), (g, i)
(D) (h, g), (g, i), (h, f), (f, c), (f, d), (d, a), Q.26 What is the minimum possible weight
(a, b), (c, e) of a spanning tree T in this graph such
[GATE 2008 : IISc Bangalore] that vertex 0 is a leaf node in the tree
T?
Q.24 Dijkstra’s single source shortest path
(A) 7 (B) 8
algorithm when run from vertex a in the
(C) 9 (D) 10
below graph, computes the correct
shortest path distance to [GATE 2010 : IIT Guwahati]

20 Algorithm
Q.27 What is the minimum possible weight shortest path to a vertex v is updated
of a path P from vertex 1 to vertex 2 in only when a strictly path to v
this graph such that P contains at most discovered.
3 edges?
(A) 7 (B) 8
(C) 9 (D) 10
[GATE 2010 : IIT Guwahati]
.Common Data for Q.28 & Q.29:
An undirected graph G  V,E  contains
n n  2 nodes named v 1 , v 2 , ...... vn . Two
nodes vi , v j are connected if and only if (A) SDT (B) SBDT
0  i  j  2. Each edge  v i , v j  is assigned a (C) SACDT (D) SACET
[GATE 2012 : IIT Delhi]
weight i  j. A sample graph with n  4 is
Q.31 Let G be a weighted graph with edge
shown below.
weights greater than one and G' be the
graph constructed by squaring the
weights of edge in G. Let T and T ' be
the minimum spanning trees of G and
G' respectively, with total weights t
and t ' . Which of the following
Q.28 What will be the cost of the minimum
statements is TRUE?
spanning tree (MST) of such a graph
with n nodes? (A) T '  T with total weight t '  t 2
1 (B) T '  T with total weight t '  t 2
(A)
12
 11n2  5n (B) n2  n  1
(C) T '  T but total weight t '  t 2
(C) 6n  11 (D) 2n  1 (D) None of the above
[GATE 2011 : IIT Madras] [GATE 2012 : IIT Delhi]
Q.29 The length of the path from V5 to V6 in Q.32 The number of distinct minimum
the MST of previous question with spanning trees for the weighted graph
n  10 is : below is _______.
(A) 11 (B) 25
(C) 31 (D) 41
[GATE 2011 : IIT Madras]
Q.30 Consider the directed graph shown in
the figure below. There are multiple
shortest paths between vertices S and
T . Which one will be reported by
Dijkstra’s shortest path algorithm?
[GATE 2014 : IIT Kharagpur]
Assume that, in any iteration, the

Q.1 Kruskal’s algorithm for finding a (C) O(m  n) (D) O(mlog n)


minimum spanning tree of a weighted Q.2 Let G be a weighted undirected graph
graph G with n vertices and m edges and e be an edge with maximum weight
has the time-complexity of : in G. Suppose there is a minimum
(A) O(n2 ) (B) O(mn) weight spanning tree in G containing
the edge e.
Algorithm 21
Which of the following statements is the maximum of the congestions on the
always TRUE? edges of the path. We wish to find the
(A) There exists a cutset in G having all path from s to t having minimum
edges of maximum weight. congestion. Which one of the following
(B) There exists a cycle in G having all paths is always such a path of
edges of maximum weight minimum congestion?
(C) Edge e cannot be contained in a (A) a path from s to t in the minimum
cycle. weighted spanning tree
(D) All edges in G have the same weight (B) a weighted shortest path from s to
t
.Common Data for Q.3 & Q.4: (C) an Euler walk from s to t
We are given 9 tasks T1, T2.... T9. The (D) a Hamiltonian path from s to t
execution of each task requires one unit of Q.7 A sink in a directed graph is a vertex i
time. We can execute one task at a time. such that there is an edge from every
Each task Ti has a profit Pi and a deadline di vertex j ≠ i to i and there is no edge
Profit Pi is earned if the task is completed
from i to any other vertex. A directed
before the end of the dth1 unit of time. graph G with n vertices is represented
Task T1 T2 T3 T4 T5 T6 T7 T8 T9 by its adjacency matrix A, where A[i] [j]
Profit 15 20 30 18 18 10 23 16 25 = 1 if there is an edge directed from
Deadline 7 2 5 3 4 5 2 7 3 vertex i to j and 0 otherwise. The
Q.3 Are all tasks completed in the schedule following algorithm determines
that gives maximum profit? whether there is a sink in the graph G.
(A) All tasks are completed i=0
(B) T1 and T6 are left out do {
(C) T1 and T8 are left out j = i + 1;
(D) T4 and T6 are left out while ((j < n) && E1)
Q.4 What is the maximum profit earned? j++;
(A) 147 (B) 165 if (j < n) E2;
(C) 167 (D) 175 } while (j < n);
.Common Data for Q.5 & Q.6: flag = 1;
Let s and t be two vertices in a undirected for (j = 0; j < n; j++)
graph G  (V, E) having distinct positive edge if ((j! = i) && E3)
flag = 0;
weights. Let [X, Y] be a partition of V such
if (flag)
that s  X and t  Y . Consider the edge e
printf("Sink exists");
having the minimum weight amongst all
else
those edges that have one vertex in X and
printf ("Sink does not exist");
one vertex in Y.
Choose the correct expressions for E1
Q.5 The edge e must definitely belong to :
and E2
(A) The minimum weighted spanning
tree of G (A) E1 : A[i][j] and E2 : i = j;
(B) The weighted shortest path from s (B) E1 : !A[i][j] and E2 : i = j + 1;
to t. (C) E1: !A[i][j] and E2 : i = j;
(C) Each path from s to t. (D) E1 : A[i][j] and E2 : i = j + 1;
(D) The weighted longest path from s to Q.8 G is a graph on n vertices and 2n – 2
t. edges. The edges of G can be
Q.6 Let the weight of an edge e denote the partitioned into two edge-disjoint
congestion on that edge. The spanning trees. Which of the following
congestion on a path is defined to be is NOT true for G?

22 Algorithm
(A) For every subset of k vertices, the The edge weights of only those edges
induced subgraph has at most 2k-2 which are in the MST are given in the
edges figure shown below. The minimum
(B) The minimum cut in G has at least possible sum of weights of all 8 edges
two edges of this graph is _______.

(C) There are two edge-disjoint paths


between every pair to vertices
(D) There are two vertex-disjoint paths
between every pair of vertices
Q.9 Which of the following statement (s)
is/are correct regarding Bellman-Ford
Shortest path algorithm? Q.13 Let G be a weighted connected
P. Always finds a negative weighted undirected graph with distinct positive
cycle, if one exists. edge weights. If every edge weight is
Q. Finds whether any negative increased by the same value, then
which of the following statements is
weighted cycle is reachable from the
/are TRUE?
source.
P : Minimum spanning tree of G does
(A) P only (B) Q only not change
(C) Both P and Q (D) Neither P nor Q Q : Shortest path between any pair of
Q.10 Suppose P, Q, R, S, T are sorted vertices does not change
sequences having lengths 20, 24, 30, 35, (A) P only (B) Q only
50 respectively. They are to be merged (C) Neither P nor Q (D) Both P and Q
into a single sequence by merging Q.14 Let G be a complete undirected graph
together two sequences at a time. The on 4 vertices, having 6 edges with
number of comparisons that will be weights being 1, 2, 3, 4, 5 and 6. The
needed in the worst case by the maximum possible weight that a
optimal algorithm for doing this is minimum weight spanning tree of G
_______. can have is _______.
Q.11 Let G  (V,E) be a simple undirected Q.15 G  (V,E) is an undirected simple graph
graph, and s be a particular vertex in it in which each edge has distinct weight,
called the source. For x  V, let d(x) and e is a particular edge of G. Which
denote the shortest distance in G from of the following statements about the
s to x . A breadth first search (BFS) is minimum spanning trees (MSTs) of G
is/are TRUE?
performed starting at s . Let T be
I. If e is the lightest edge of some
resultant BFS tree. If (u, v) is an edge of
cycle in G , then every MST of G
G that is not in T , then which one of
includes e
the following CANNOT be the value of
II. If e is the heaviest edge of some
d(u)  d(v) ?
cycle in G , then every MST of G
(A) 1 (B) 0 excludes e
(C) 1 (D) 2 (A) I only (B) II only
Q.12 The graph shown below has 8 edges (C) Both I and II (D) Neither I nor II
with distinct integer edge weights. The Q.16 Let G  (V,E) be any connected
minimum spanning tree (MST) is of undirected edge-weighted graph. The
weight 36 and contains the edges: weights of the edges in E are positive
 A, C , B, C  , B, E  , E, F  , D, F  . and distinct.

Algorithm 23
Consider the following statements : Which one of the options completes
(I) Minimum Spanning tree of G is the following sentence so that it is
always unique. TRUE?
(II) Shortest path between any two “The shortest paths in G under w are
vertices of G is always unique. shortest paths under w’ too, _______”.
Which of the above statements is/are (A) for every f : V 
necessarily true?
(B) if and only if u  V, f(u) is positive
(A) (I) only
(B) (II) only (C) if and only if u  V, f(u) is negative
(C) Both (I) and (II) (D) if and only if f(u) is the distance
(D) Neither (I) nor (II) from s to u in the graph obtained by
Q.17 Consider the following undirected adding a new vertex s to G and edges
graph G: of zero weight from s to every vertex of
G
Q.21 Consider string aabbccddeee. Each
letter of a string must be assigned a
binary code satisfying the following
properties.
1. For any two letters the code to one
letter must be a prefix of code assigned
Choose a value for x that will maximize to another letter.
the number of minimum weight
2. For any two letters of the same
spanning trees (MWSTs) of G. The
frequency, the letter which occurs
number of MWSTs of G for this value of
earlier in the dictionary order is
x is _______.
assigned a code whose length is at
Q.18 Let G  (V,E) be a weighted undirected most the length of code assigned to
graph and let T be a Minimum Spanning another letter.
Tree (MST) of G maintained using Among a set of all binary code
adjacency lists. Suppose a new assignments which satisfy above two
weighted edge (u, v)  V  V is added to properties. What is the length of the
G. The worst case time complexity of encoded string?
determining if T is still an MST of the
(A) 25 (B) 23
resultant graph is
(C) 21 (D) 30
(A)   E  V  (B)   E V 
Q.22 Let G be a connected undirected
(C)   E log V  (D)   V  weighted graph consider the following
Q.19 Consider a graph G  (V,E), where to statements.

V  v 1 , v2 , , v 100  , S1. There exists a minimum edge weight


in G which is present in every MST of G.
E  (v i , v j ) | 1  i  j  100 , and weight of
S2. If every edge in G has distinct
the edge (vi , v j ) is i  j . The weight of weights, then G has a unique MST.
minimum spanning tree of G is _______. Which of the following is true?
Q.20 Let G  (V,E) be a directed, weighted (A) S1 is true and S2 is false
graph with weight function w : E  . (B) S1 is false and S2 is true
For some function f : V  , for each (C) Both S1 and II are true
edge (u, v)  E, define w ' (u, v) as (D) Both S1 and II are false
w (u, v)  f(u)  f(v). 

24 Algorithm
Classroom Practice Questions
1. 144 2. O(E) 3. 165 4. C 5. B
6. B 7. D 8. A 9. C 10. D
11. A 12. B 13. A 14. D 15. A
16. D 17. D 18. A 19. D 20. D
21. D 22. C 23. C 24. D 25. D
26. D 27. B 28. B 29. C 30. D
31. D 32. 6
Sell-Practice Questions
1. D 2. A 3. D 4. A 5. A
6. A 7. C 8. A 9. B 10. 358
11. D 12. B 13. A 14. 7 15. B
16. A 17. 4 18. D 19. 99 20. A
21. B 22. C



Algorithm 25
5 Dynamic Programming

Q.1 The number of scalar element A : 5 3 B : 3 4


multiplication required to multiply the C : 44 D : 42
following matrices (with given E : 25 F : 5 3
dimensions)
The number of scaler multiplication
A  2 3 operations required for (((AB)(CD))(EF))
B  3  100 is ________.
C  100  4 Q.6 The problem of multiplying two long
D  4  25 integers of size ‘n’ digits can be solved
is ________. with which of the following method
Q.2 Which of the following in not a possible algorithm?
longest common subsequence for the (A) Kasturba’s algorithm
following pair of strings: (B) Divide and conquer
XYXYXYX (C) Toom algorithm
YXZ2YXXYX (D) Kadane’s algorithm
(A) YXXYXX (B) XYXXYX Q.7 Consider two springs P = “ababc” and
(C) YXYXYX (D) XYXYXZ   ”bababbac”. Let, ‘x’ be the LCS of P
Q.3 The optimal solution for the 0/1 and Q and ‘y’ is the number of such LCS
knapsack problem with the below then x + y is ________.
items consider the capacity of the Q.8 The minimum number of scalar
knapsack to be 22 units will give a multiplication for matrix- chain
profit of ________. product whose sequence of
Item Profit Weight dimensions is 5, 10, 3, 12, 5, 50 and 6
1 30 7 ________.
2 20 9 Q.9 Which one of the following algorithm
design techniques is used in finding all
3 18 7
pairs of shortest distance in a graph?
4 17 4
Q.4 Consider the following statements: (A) Dynamic programming
S1: The time complicity for Floyd (B) Backtracking
warshall’s algorithm for all pair
(C) Greedy
shortest path is given by O(v3 )
(D) Divide and Conquer
S2: The time complexity to determine [GATE 1998 : IIT Delhi]
LCS of two strings of length n using
.Common Data for Q.10 & Q.11:
dynamic programming is O(n2 )
The subset-sum problem is defined as
Which of the following is correct?
follows. Given a set of n positive integers,
(A) S1 is Ture, S2 is False. S  a1 , a2 , a3 ,....,an  , and positive integer W
(B) S1 is False, S2 is True. is here a subset of S whose elements sum
(C) Both S1 and S2 are True to W? A dynamic program for solving this
(D) None problem uses a 2-dimensional Boolean
Q.5 Consider the matices with the array, X with n rows and W  1 column
dimensions: X i, j , 1  i  n, 0  j  W, is TRUE if and only if

26 Algorithm
there is a subset of a1 , a2 , ......,ai  whose Q.13 The values of l i, j could be obtained
elements sum to j. by dynamic programming based on the
Q.10 Which of the following is valid for correct recursive definition of l i, j of
2  i  n and ai  j  W ? the form given above, using an array
(A) X i, j  X i  1, j  X i, j  ai  L M, N , where M  m  1 and N  n  1,

(B) X i, j  X i  1, j  X i  1, j  ai  such that L i, j  l i, j . Which one of


the following statements would be
(C) X i, j  X i  1, j  X i, j  ai 
TRUE regarding the dynamic
(D) X i, j  X i  1, j  X i  1, j  ai  programming solution for the recursive
[GATE 2008 : IISc Bangalore] definition of l i, j ?
Q.11 Which entry of the array X, if TRUE, (A) All element L should be initialized to
implies that there is a subset whose 0 for the values of l i, j to be
elements sum to W? properly computed.
(A) X  1, w (B) X n, 0 (B) The values of l i, j may be
(C) X n, W (D) X n  1, n computed in a row major order or
column major order of L M,N .
[GATE 2008 : IISc Bangalore]
(C) The value of l i, j cannot be
.Common Data for Q.11 & Q.12:
computed in either row major order
A sub-sequence of a given sequence is just
the given sequence with some elements or column major order of L M,N .
(possibly none or all) left out. We are given (D) L p,q needs to be computed
two sequences X m and Y n of lengths m
before L r, s if either p  r or q  s.
and n, respectively, with indexes of X and Y
[GATE 2009 : IIT Roorkee]
starting from 0
Q.14 What is the time complexity of
Q.12 We wish to find the length of the
Bellman-ford single-source shortest
longest common sub-sequences (LCS)
path algorithm on a complete graph of
of X m and Y n as l m, n , where an n vertices?
incomplete recursive definition for the (A) (n2 ) (B) (n2 logn)
function l i, j to compute the length
(C) (n3 ) (D) (n3 logn)
of the LCS of X m and Y n is given [GATE 2013 : IIT Bombay]
below; Q.15 Consider two strings A  " qpqrr " and
l i, j  0, if either i  1 or j  0 B  "pqprqrp" . Let x be the length of
 expr 1,if i, j  0and x i  1  y  j  1 the longest common subsequence (not
necessarily contiguous) between A and
 expr 2,if i, j  0and x i  1  y  j  1 B and let y be the number of such
Which one of the following options is longest common subsequences
correct? between A and B. Then x  10y 
(A) exprl  l i  1, j  1 _______.
[GATE 2016 : IISc Bangalore]
(B) exprl  l i, j  1 Q.16 The Floyd-Warshall algorithm for all-
(C) expr 2  max l i  1, j ,l i, j  1  pair shortest paths computation is
based on :
(D) expr 2  max l i  1, j  1 ,l i, j  (A) Greedy paradigm.
[GATE 2009 : IIT Roorkee] (B) Divide-and-conquer paradigm.
Algorithm 27
(C) Dynamic programming paradigm. 3  16, 16  1 and 1  1000, respectively.
(D) neither greedy nor divide-and- In the parenthesization of FF 1 2F3F4F5 that
conquer nor dynamic programming minimizes the total number of scalar
paradigm. multiplications, the explicitly
[GATE 2016 : IISc Bangalore] computed pairs is/are
Q.17 Let A1, A2, A3, and A4 be four matrices (A) FF
1 2 and F3F4 only
of dimensions 10  5, 5  20, 20  10 and
(B) F2F3 only
10  5, respectively. The minimum
number of scalar multiplications (C) F3F4 only
required to find the product A1A2A3A4 (D) FF
1 2 and F4F5 only

using the basic matrix multiplication [GATE 2018 : IIT Guwahati]


method is _______. Q.19 Define Rn to be the maximum amount
[GATE 2016 : IISc Bangalore] earned by cutting a rod of length n
Q.18 Assume that multiplying a matrix G 1 of meters into one or more pieces of
dimension p  q with another matrix G2 integer length and selling them. For i >
of dimension q  r requires pqr scalar 0, let p [i] denote the selling price of a
rod whose length is i meters. Consider
multiplications. Computing the product
the array of prices:
of n matrices G1 G2 G3 Gn can be done
p[l] = l,p[2] = 5, p[3] = 8,p[4] = 9. p[5] =
by parenthesizing in different ways. 10, p[6] = I7,p[7] = 13
Define Gi Gi 1 as an explicitly computed
Which of the following statements
pair for a given paranthesization if they is/are correct about R7 ?
are directly multiplied. For example, in Options :
the matrix multiplication chain
(A) R7 cannot be achieved by a solution
G1G2G3G4G5G6 using parenthesization
consisting of three pieces.
(G1 (G2G3 )) (G4 (G5G6 )), G2G3 and G5G6 are (B) R7 is achieved by three different
the only explicitly computed pairs. solutions.
Consider a matrix multiplication chain (C) R7 = 19
1 2F3F4F5 , where matrices F1 ,F2 ,F3 ,F4 and
FF (D) R7 = 18
F5 are of dimensions 2  25, 25  3, [GATE 2021 : IIT Bombay]

Q.1 Consider the following statements S1 Q.2 The number of ways to parenthesize
and S2: matrix- chain multiplication containing
S1: If we use a max-queue instead of a 5 matrices i.e., A 1A 2 A 3 A 4 A 5 is _______.
min-queue in kruskal’s MST algorithm, Q.3 Match the pairs in the following :
it will return the spanning tree of List-I
maximum total cost. (a) Strassen’s matrix multiplication
S2: If a directed graph G is cyclic but algorithm
can be made acyclic by removing one (b) Kruskal’s minimum spanning tree
edge, the a DFS in G will encounter algorithm
exactly one back edge. (c) Biconnected components algorithm
Which of the following is correct? (d) Floyd’s shortest path algorithm
(A) S1 is True, S2 is False. List-II
(B) S1 is False, S2 is True. (p) Greedy method
(C) Both S1 and S2 are True. (q) Dynamic programming
(r) Divide and conquer
(D) Both S1 and S2 are False.
(s) Depth first search
28 Algorithm
(A) a-r, b-p, c-s, d-q (D) Subproblems where resources are
(B) a-r, b-p, c-q, d-s shared
(C) a-r, b-s, c-p, d-q Q.7 Dynamic programming does not work if
(D) a-q, b-p, c-s, d-r the subproblems:
Q.4 Which of the following is True? (A) Share resources and thus are not
(A) Any problem that can be solved independent
with a greedy algorithm can also be (B) Cannot be divided in half
solved with dynamic programming (C) Overlap
(B) A dynamic programming algorithm (D) Have to be divided too many times
always uses some type of to fit into memory
recurrence relation. Q.8 The difference between Divide and
(C) Both A and B Conquer and Dynamic Programming is:
(D) None of these (A) Whether the subproblems overlap
Q.5 What are 2 things required in order to or not
successfully use the dynamic (B) The division of problems and
programming technique? combination of subproblems
(A) Optimal substructure and (C) The way we solve the base case
overlapping subproblems (D) The depth of recurrence
(B) A problem that can’t be subdivided Q.9 Dynamic programming divides
and is complex problems into a number of :
(C) Non‐overlapping subproblems and (A) objective functions.
intervals (B) decision stages.
(D) Recursion and a problem that is (C) unrelated constraints.
complex (D) none of the above
Q.6 Dynamic Programming is often used for Q.10 In dynamic programming, the output to
(choose all that apply): stage n become the input to :
(A) Optimization problems that involve (A) Stage n-1
making a choice that leave one or (B) Stage n+1
more subproblems to be solved.
(C) Stage n itself
(B) Problems previously solved using
(D) None of these
divide and conquer that have
overlapping subproblems
(C) Non‐polynomial solution problems 

Classroom Practice Questions


1. 1424 2. A, D 3. 67 4. C 5. 192
6. A 7. 7 8. 2010 9. A 10. B
11. C 12. C 13. B 14. C 15. 34
16. C 17. 1500 18. C 19. B,D
Sell-Practice Questions
1. A 2. 14 3. A 4. C 5. A
6. A, B 7. A 8. A 9. B 10. B



Algorithm 29
Data Structure
1 Array

Q.1 Let A[1..8,-5..5,-10..5] be a 3D array ? What is the number of elements in the array ?
(A) 1200
(B) 1408
(C) 33
(D) 1050
Q.2 A One Dimensional array A has indices 1..75. Each element is a string and takes up 3
memory words. The array is stored at location 1120. The starting address of A[49] is :
(A) 1267
(B) 1164
(C) 1264
(D) 1169
Q.3 Let A[-5..5,-10..10] be an array of integers where each element takes 4 bytes in memory
and the base address of the array is 1000 .The starting address of A[0,0] is _________?
Q.4 Let A[-5..5,-20..20] be an array of integers where each element takes 4 bytes in
memory and the base address of the array is 1000 .The starting address of A[2,3] is
_________?
Q.5 Let A[-5..5,-5..5,-10..10] be an array of integers where each element takes 4 bytes in
memory and the base address of the array is 1000 .The starting address of A[1,1,1] is
_________?
Q.6 Let A[-5..5,-10..10,-10..10,-5..5] be an array of integers where each element takes 4
bytes in memory and the base address of the array is 1000 .The starting address of
A[0][0][0][0] is _________?
Q.7 Let A[-25..25,-20..20,-10..10] be an array of integers where each element takes 2 bytes
in memory and the base address of the array is 1062 .The starting address of A[0][0][0]
is _________?
Q.8 Let A be a two dimensional array declared as follows : [GATE 1998]
A: array [1 ... 10] [1 ... 15] of integer;
Assuming that each integer takes one memory location, the array is stored in row-
major order and the first element of the array is stored at location 100, what is the
address of the element a[i][j] ?
(A) 15i+ j+ 84
(B) 15j+ i+ 84
(C) 10i+ j+ 89
(D) 10j+ i+ 89
Q.9 Consider the following declaration of a two-dimensional array in C :
char a[100][100] ;
Assuming that the main memory is byte-addressable and that the array is stored starting
from memory address 0, the address of a[40][50] is:
(A) 4040
(B) 4050
(C) 5040
(D) 5050
Data Structures 1
Q.10 Let A[-5..5,-5..5] be an array of integers where each element takes 4 bytes in memory
and the base address of the array is 1000 .The starting address of A[2,3] (assuming that
array is stored in Column Major order) is _________?
Q.11 Let A[-5..5,-10..10,-10..10] be an array of integers where each element takes 4 bytes in
memory and the base address of the array is 1000 .The starting address of A[0,0,0]
(assuming that array is stored in Column Major order) is _________?
Q.12 Let A[-5..5,-10..10,-10..10,-5..5] be an array of integers where each element takes 2 bytes
in memory and the base address of the array is 1000 .The starting address of A[1,1,1,1]
(assuming that array is stored in Column Major order) is _________?
Q.13 In a compact single dimensional array representation for lower triangular matrices (i.e
all the elements above the diagonal are zero) of size n×n, non-zero elements, (i.e
elements of lower triangle) of each row are stored one after another, starting from the
first row, the index of the (i,j)th element of the lower triangular matrix in this new
representation is: 1994 2 marks
(A) i+j
(B) i+j−1
(C) (j−1)+i(i−1)/2
(D) i+j(j−1)/2
Q.14 A tridiagonal matrix [-2..2,5..9] is stored in row major order with base address 301.what
is the address of data [0][8] if nonzero elements are stored?
Q.15 If the address of A[1][1] and A[2][1] are 1000 and 1010 and each element occupies 2 bytes
, then the array has been stored in ______________ order .
(A) Row Major order
(B) Column major order
(C) Matrix Order
(D) Simple
Q.16 Array A[8,15] is stored in row major order with first element A[1,1] stored as location A0
. Assuming that each element of the array is of size s bytes , element A[I,j] can be
accessed at :
(A) A0 + 15s(i-1) + s(j-1)
(B) A0 + 8s(i-1) + s(j-1)
(C) A0 + 15s(j-1) + s(i-1)
(D) A0 + 8s(j-1) + s(i-1)
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
______________

2 Data Structures
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: [GATE 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
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);

Data Structures 3
(A) Rotates s left by k positions
(B) Leaves s unchanged
(C) Reverses all elements of s
(D) None of the above

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
______________
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 ______________



4 Data Structures
Classroom Practice Questions
1. B 2. C 3. 1600 4. 1340 5. 7092
6. 107720 7. 44972 8. A 9. B 10. 1380
11. 10700 12. 63548 13. C 14. 308 15. A
16. A 17. B 18. 1361 19. 1252 20. 1061
21. 26 22. 23699 23. A 24. n2(n+1) 25. 2n3
26. A
Self-Practice Questions
1. 9042 2. 5150 3. 5150 4. 530 5. 39
6. 111 7. 1420 8. 1174 9. 221637 10. 1144



Data Structures 5
2 List

Q.1 In a circular linked list organization, insertion of a record involves modification of


(A) One pointer.
(B) Two pointers.
(C) Multiple pointers.
(D) No pointer. [1987 : 2 Marks]
Q.2 What is the functionality of the following piece of code?
int fun(int x)
{
node *temp=head;
int y=0;
while(temp!=NULL)
{
if(temp->data==x)
return y;
y++;
temp=temp->next;
}
return -10000;
}
Assuming node template as :
struct node
{
int data;
struct node *next;
};
(A) Find and delete a given element in the list if it exists otherwise returns -10000.
(B) Find and return the given element in the list otherwise return -10000.
(C) Find and return the position of the given element in the list otherwise return -
10000.
(D) Find and insert a new element in the 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 2 𝑛
(B) n/2
(C) log 2 𝑛 – 1
(D) n [2002 : 1 Mark]
Q.4 Consider the function f defined below.
struct item
{
int data;
struct item * next;
};

6 Data Structures
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
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;
}

Data Structures 7
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;
}
/*ADD A STATEMENT HERE*/
}
What should be added in place of “/*ADD A STATEMENT HERE*/”, so that the
function correctly reverses a linked list.
(A) *head_ref = prev;
(B) *head_ref = current;
(C) *head_ref = next;
(D) *head_ref = NULL;
Q.9 Which among the following segment of code counts the number of elements in a
linked list ,if it is assumed that ptr is pointing to the first node and cntr is the variable
which counts the number of elements in the list ?
(A) for(count=1;ptr!=NULL;count++) ptr=ptr->next
(B) for(count=0;ptr->next!=NULL;count++) ptr=ptr->next
(C) for(count=1;ptr->next!=NULL;count++) ptr=ptr->next
(D) for(count=0;ptr!=NULL;count++) ptr=ptr->next
Q.10 In a circular linked list
(A) Components are linked together in some sequential manner.
(B) There is no beginning and no end.
(C) Components are arranged hierarchically.
(D) Forward and backward traversal within the list is permitted.

8 Data Structures
Q.11 Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to
head node is not given, can we delete the node X from given linked list?
(A) Possible if X is not last node. Use following two steps (a) Copy the data of next of
X to X. (b) Delete next of X.
(B) Possible if size of linked list is even.
(C) Possible if size of linked list is odd
(D) Possible if X is not first node. Use following two steps (a) Copy the data of next of
X to X. (b) Delete next of X.
Q.12 You are given pointers to first and last nodes of a singly linked list, which of the
following operations are dependent on the length of the linked list?
(A) Delete the first element
(B) Insert a new element as a first element
(C) Delete the last element of the list
(D) Add a new element at the end of the list
Q.13 In a doubly linked list, the number of pointers affected for an insertion operation will
be : [ISRO CS 2017]
(A) 4
(B) 0
(C) 1
(D) None of these
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

Data Structures 9
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)
{
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);
10 Data Structures
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)
{
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]

Data Structures 11
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)
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:

void changelist (struct node * x)


{
struct node *p, *q;
p = x  next;
q = p  next;
p  next = q  next;
x  next = q;
q  next = p;
}
What is the output displayed if you traverse the list after invoking changelist(first)?
(A) 4 3 2 1 (B) 4 2 3 1
(C) 13 2 4 (D) 2 1 3 4

12 Data Structures
Q.8

What is the output after the following sequence of steps?


struct Node * P ;
(i) P = first  link  link  link  link;
(ii) P  link  link = first;
(iii) first  link  link  link = P  link;
(iv) printf ("%C', first  link  link  link  link  link  data):
(A) A (B) B
(C) C (D) D
Q.9 What is the output after the following steps executed?

Firs
t
(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



Data Structures 13
Classroom Practice Questions
1. B 2. C 3. B 4. B 5. E
6. E 7. C 8. A 9. C 10. B
11. A 12. C 13. D 14. D 15. C
16. A 17. B 18. A 19. D 20. B
Self-Practice Questions
1. A 2. C 3. A 4. C 5. D
6. C 7. C 8. B 9. C 10. D



14 Data Structures
3 Stacks and Queues

Q.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
[1991 : 2 Marks]
Q.2 Which of the following permutations can be obtained in the output (in the same order)
using a stack assuming that the input is 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
[1994 : 2Marks]
Q.3 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
[2004 : 2Marks]
Q.4 The postfix expression for the infix expression A+B∗(C+D)/F+D∗E is:
(A) AB+CD+∗F/D+E∗
(B) ABCD+∗F/+DE∗+
(C) A∗B+CD/F∗DE++
(D) A+∗BCD/F∗DE++
[1995 : 2Marks]
Q.5 Which of the following is essential for converting an infix expression 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
[1997 : 1Mark]
Q.6 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
(A) abc × + def ^ ^ –
(B) abc × + de ^ f ^ –
(C) ab + c × d – e ^ f ^
(D) – + a × bc ^ ^ def
[2004 : 2Marks]
Data Structures 15
Q.7 Compute the post fix equivalent of the following expression
3*log(x+1)-a/2
[1998 : 2Marks]
Q.8 The following postfix expression with single digit operands is evaluated using a stack:
823^/23*+51*-
Note that ^ is the exponentiation operator. The top two elements of the stack after the
first * is evaluated are :
(A) 6, 1 (B) 5, 7
(C) 3, 2 (D) 1, 5
[2007 : 2Marks]
Q.9 The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is
(A) 284 (B) 213
(C) 142 (D) 71
[2015 : 1Mark]
Q.10 The attributes of three arithmetic operators in some programming language are given
below.
[2016 : 2Marks]
Operator Precedence Associativity Arity
+ High Left Binary
- Medium Right Binary
* Low Left Binary
The value of the expression 2 - 5 + 1 - 7 * 3 in this language is __________
Q.11 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
[2004 : 1Mark]
Q.12 The best data structure to check whether an arithmetic expression has balanced
parentheses is a
(A) queue (B) stack
(C) tree (D) list
[2004 : 1Mark]
Q.13 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 implementing
QUEUES 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?

16 Electronic Devices & Circuits


(A) (ii) and (iii) are true
(B) (i) and (ii) are true
(C) (iii) and (iv) are true
(D) (ii) and (iv) are true
[1996 : 1Mark]
Q.14 A priority queue Q is used to implement a stack S that stores characters. PUSH(C) is
implemented as INSERT(Q, C, K) where K is an appropriate integer key chosen by the
implementation. POP is implemented as DELETEMIN(Q). For a sequence of operations,
the keys chosen are in
(A) Non-increasing order
(B) Non-decreasing order
(C) Strictly increasing order
(D) Strictly decreasing order
[1997 : 2Marks]
Q.15 What is the minimum number of stacks of size n required to implement a queue of size
n? [2001 : 2 marks]
(A) One (B) Two
(C) Three (D) Four
Q.16 Let S be a stack of size n ≥ 1. Starting with the empty stack, suppose we push the first
n natural numbers in sequence, and then perform n pop operations. Assume that Push
and pop operation take X seconds each, and Y seconds elapse between the end of one
such stack operation and the start of the next operation. For m ≥ 1, define the stack-
life of m as the time elapsed from the end of Push(m) to the start of the pop operation
that removes m from S. The average stack-life of an element of this stack is
(A) n (X + Y) (B) 3Y + 2X
(C) n (X + Y) – X (D) Y + 2X
[2003 : 2Marks]
Q.17 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?

Front Rear

P ?

(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

Data Structures 17
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
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?

18 Electronic Devices & Circuits


(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
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
algorithm given below. 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]
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?

Data Structures 19
(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. Empty (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]

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)

20 Electronic Devices & Circuits


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

Data Structures 21
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
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)



22 Electronic Devices & Circuits


Classroom Practice Questions
1. B 2. B 3. D 4. B 5. A
6. A 7. * 8. A 9. C 10. 9
11. D 12. B 13. A 14. D 15. B
16. C 17. A 18. C 19. B 20. A
21. C 22. 256 23. B 24. B 25. B
Self-Practice Questions
1. A 2. C 3. A 4. D 5. C
6. C 7. 5 8. A 9. D 10. D
11. D 12. D 13. C



Data Structures 23
4 Tree

Q.1 A complete n-ary tree is one in which every node has 0 or n sons. If x is the number of
internal nodes of a complete n-ary tree, the number of leaves in it is given by
(A) x(n−1)+1 (B) xn−1
(C) xn+1 (D) x(n+1)
[1998 : 2 Marks]
Q.2 The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
(A) n/2 (B) (n-1)/3
(C) (n-1)/2 (D) ⌈(2𝑛 + 1)/3 ⌉
[2002 : 2 Marks]
Q.3 In a complete k-ary tree, every internal node has exactly k children or no child. The
number of leaves in such a tree with n internal nodes is:
(A) nk (B) (n – 1) k+ 1
(C) n( k – 1) + 1 (D) n(k – 1)
[2005 : 2 Marks]
Q.4 A complete n-ary tree is a tree in which each node has n children or no children. Let I
be the number of internal nodes and L be the number of leaves in a complete n-ary
tree. If L = 41, and I = 10, what is the value of n?
(A) 3 (B) 4
(C) 5 D) 6
[2007 : 2 Marks]
Q.5 In a binary tree, the number of internal nodes of degree 1 is 5, and the number of internal
nodes of degree 2 is 10. The number of leaf nodes in the binary tree is
(A) 10 (B) 11
(C) 12 (D) 15
[2006 : 1 Mark]
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 R M S Q T
[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

24 Data Structures
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 C

(B) A

C D

(C) A

C D

Data Structures 25
(D) A

B D

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]

26 Data Structures
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}

Data Structures 27
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) (n2 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)
10

8 6

5 2
4

1
(B)
10

8 6

4 5 1 2

28 Data Structures
(C)
10

5 6

4 8 2 1

(D)
5

2 8

1 4 6 10

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)
32

30 25

15 12 20 16

(B)
32

25 30

12 15 20 16

(C)
32

30 25

15 12 16 20

(D)
32

25 30

12 15 16 20

Data Structures 29
Q.30 Which of the following sequences of array elements forms a heap?
(A) {23, 17, 14, 6, 13, 10, 1, 12, 7, 5}
(B) {23, 17, 14,6, 13, 10, 1,5,7, 12}
(C) {23, 17, 14, 7. 13, 10, 1, 5, 6, 12}
(D) {23, 17, 14, 7, 13, 10, 1, 12. 5, 7}
[2006 : 2 Marks]
Linked Question for 31 & 32.
Q.31 Consider a binary max-heap implemented using an array. Which one of the following
array represents a binary max-heap?
(A) 25,12,16,13,10,8,14
(B) 25,14,13,16,10,8,12
(C) 25,14,16,13,10,8,12
(D) 25,14,12,13,10,8,16
[2009 : 2 Marks]
Q.32 What is the content of the array after two delete operations on the correct answer to
the previous question?
(A) 14,13,12,10,8 (B) 14,12,13,8,10
(C) 14,13,8,12,10 (D) 14,13,12,8,10
[2009 : 2 Marks]
Q.33 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]
Common Data for Questions 34 & 35.
A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children.
A 3-ary heap can be represented by an array as follows: The root is stored in the first location,
a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the
second level of the tree from left to right are stored from a[4] location onward. An item x
can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and
pushing it up the tree to satisfy the heap property.
Q.34 Which one of the following is a valid sequence of elements in an array representing 3-
ary max heap?
(A) 1, 3, 5, 6, 8, 9 (B) 9, 6, 3, 1, 8, 5
(C) 9, 3, 6, 8, 5, 1 (D) 9, 5, 6, 8, 3, 1
[2006: 2 Marks]
Q.35 Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3- ary max
heap found in the above question, Which one of the following is the sequence of items
in the array representing the resultant heap?

30 Data Structures
(A) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4
(B) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
(C) 10, 9, 4, 5, 7, 6, 8, 2, 1, 3
(D)10, 8, 6, 9, 7, 2, 3, 4, 1, 5
[2006 : 2 Marks]
Q.36 Consider a max heap, represented by the array:
40, 30, 20, 10, 15, 16, 17, 8, 4.
Now consider that a value 35 is inserted into this heap. After insertion, the new heap i
(A) 40, 30, 20, 10, 15, 16, 17, 8, 4, 35
(B) 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
(C) 40, 30, 20, 10, 35, 16, 17, 8, 4, 15
(D) 40, 35, 20, 10, 15, 16, 17, 8, 4, 30
[2015 : 2 Marks]
Q.37 Consider the array representation of a binary min-heap containing 1023 elements. The
minimum number of comparisons required to find the maximum in the heap is _________
[2020 : 2 Marks]
Q.38 A complete binary min-heap is made by including each integer in [1, 1023] exactly once.
The depth of a node in the heap is the length of the path from the root of the heap to
that node. Thus, the root is at depth 0. The maximum depth at which integer 9 can
appear is ___________ .
[2016 : 2 Marks]

Q.1 State whether the following statements are TRUE or FALSE:


If the number of leaves in a tree is not a power of 2, then the tree is not a binary tree.
[1987 : 1 mark]
Q.2 A binary tree T has n leaf nodes. The number of nodes of degree 2 in T is
(A) log 2 𝑛 (B) n−1
(C) n (D) 2𝑛
[1995 : 1 Mark]
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]

Data Structures 31
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
k 1

[2003 : 1 Mark]
(A) n-k+1 (B) n-k
(C) n-k-1 (D) n-k-2
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;
}

32 Data Structures
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]
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ℎ−1 (B) 2ℎ−1 + 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ℎ−1 (B) 2ℎ−1 -1
(C) 2ℎ+1 -1 (D) 2ℎ+1
[2007 : 1 Mark]

Data Structures 33
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)
{
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:
(N) the number of nodes in the tree
(O) the number of internal nodes in the tree
(P) the number of leaf nodes in the tree
(Q) the height of the tree
[2007 : 2 Marks]
Common Data for Q.18 & 19.
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

34 Data Structures
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 for Q.21 & 22.
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.
Q.21 n3 can be expressed as
[2008 : 2 Marks]
(A) n1 + n2 – 1 (B) n1 - 2
(C) [((n1 + n2)/2)] (D) n2 - 1
Q.22 Starting with the above tree, while there remains a node v of degree two in the tree,
add an edge between the two neighbors of v and then remove v from the tree. How
many edges will remain at the end of the process?
[2008 : 2 Marks]
(A) 2 * n1 – 3 (B) n2 + 2 * n1 - 2
(C) n3 - n2 (D) n2 + n1 - 2
Q.23 In a binary tree with n nodes, every node has an odd number of descendants. Every node
is considered to be its own descendant. What is the number of nodes in the tree that
have exactly one child?
(A) 0 (B) 1
(C) (n-1)/2 (D) n-1
[2010 : 1 Mark]
Q.24 We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In
how many ways can we populate the tree with the given set so that it becomes a binary
search tree?
(A) 0 (B) 1
(C) n! (D) 2nCn /n+1
[2011 : 2 Marks]
Q.25 The height of a tree is defined as the number of edges on the longest path in the tree.
The function shown in the pseudocode below is invoked as height (root) to compute the
height of a binary tree rooted at the tree pointer root.
[2012 : 2 Marks]

Data Structures 35
int height (treeptr n)
{
if (n = = NULL) return -1;
if (n  left == NULL)
if (n  right = NULL) return 0;
else return BI ; // Box 1
else
{
hi = height (n  left);
if (n  right = NULL) return (1
else
{
h2 = height (n  right);
return B2 ; // Box 2
}
}
}
The appropriate expression for the two boxes B1 and B2 are
(A) B1 : (1 + height(n->right)), B2 : (1 + max(h1,h2))
(B) B1 : (height(n->right)), B2 : (1 + max(h1,h2))
(C) B1 : height(n->right), B2 : max(h1,h2)
(D) B1 : (1 + height(n->right)), B2 : max(h1,h2)
Q.26 The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35,
42. Which one of the following is the postorder traversal sequence of the same tree?
[2013 : 2 Marks]
(A) 10, 20, 15, 23, 25, 35, 42, 39, 30
(B) 15, 10, 25, 23, 20, 42, 35, 39, 30
(C) 15, 20, 10, 23, 25, 42, 35, 39, 30
(D15, 10, 23, 25, 20, 35, 42, 39, 30
Q.27 Consider the following rooted tree with the vertex P labeled as root
P

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]

36 Data Structures
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;
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]

Data Structures 37
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]
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:

38 Data Structures
(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.39Consider 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.



Classroom Practice Questions


1. A 2. D 3. C 4. C 5. B
6. False 7. * 8. * 9. C 10. B
11. D 12. A 13. D 14. D 15. C
16. D 17. A 18. C 19. B 20. A
21. C 22. B 23. D 24. A 25. A
26. B 27. B 28. 80 29. A 30. C
31. C 32. D 33. D 34. D 35. A
36. B 37. 511 38. 8
Self-Practice Questions
1. False 2. B 3. B 4. D 5. D
6. B, C 7. C 8. B 9. C 10. D
11. B 12. B 13. B 14. C 15. B
16. A 17. C 18. D 19. C 20. B
21. B 22. A 23. A 24. B 25. A
26. D 27. A 28. D 29. A 30. A
31. 199 32. 64 33. B 34. B 35. 4
36. 4.25 37. B 38. B 39. D 40. 8



Data Structures 39
5 Graph

Q.1 Consider the following graph: (D) There must exist a cycle in G
a containing u and all its neighbours
in G.
e b f Q.4 The most efficient algorithm for finding
the number of connected components
h
in an undirected graph on n vertices
and m edges has time complexity
g (A) Θ(n) (B) Θ(m)
Among the following sequences: (C) Θ(m+n) (D) Θ(mn)
I. a b e g h f
II. a b f e h g [2008 : 1 Mark]
III. a b f h g e Q.5 The Breadth First Search algorithm has
IV. a f g h b e been implemented using the queue
Which are depth first traversals of the data structure. One possible order of
above graph? visiting the nodes of the following
(A) I, II and IV only graph is
(B) I and IV only M N O
(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 R Q P
with n vertices, k edges are marked as (A)MNOPQR (B)NQMPOR
tree edges. The number of connected (C)QMNPRO (D)QMNPOR
components in G is [2008 : 1 Mark]
(A) k Q.6 Let G be a graph with n vertices and m
(B) k + 1 edges. What is the tightest upper
(C) n – k – 1 bound on the running time of Depth
(D) n – k First Search on G, when G is
[2005 : 1 Mark] represented as an adjacency matrix?
Q.3 Let T be a depth first search tree in an (A) Θ(n) (B) Θ(n+m)
undirected graph G. Vertices u and n (C) Θ(n )
2
(D) Θ(m2)
are leaves of this tree T. The degrees of [2014 (Set-1) : 1 Mark]
both u and v in G are at least 2. which Q.7 Suppose depth first search is executed
one of the following statements is on the graph below starting at some
true? unknown vertex. Assume that a
[2006 : 1 Marks] recursive call to visit a vertex is made
(A) There must exist a vertex w
only after first checking that the vertex
adjacent to both u and v in G
has not been visited earlier. Then the
(B) There must exist a vertex w whose
maximum possible recursion depth
removal disconnects u and v in G
(C) There must exist a cycle in G (including the initial call) is
containing u and v ____________.
[2014 (Set-1) : 1 Mark]
50 Data Structures
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
Q.8 Breadth First Search (BFS) is started on list entry to its twin. If |E|= m and |V | =
a binary tree beginning from the root n, and the memory size is not a
vertex. There is a vertex t at a distance constraint, what is the time complexity
four from the root. If t is the n-th vertex of the most efficient algorithm to set
in this BFS traversal, then the the twin pointer in each entry in each
maximum possible value of n is adjacency list?
________ (A) Θ(𝑛2 ) (B) Θ(m+n)
[2016 : 1 Mark]
(C) Θ(𝑚 )
2
(D) Θ(𝑛4 )
Q.9 In an adjacency list representation of
[2016 (Set-2) :1Mark]
an undirected simple graph G = (V, E),

Q.1 The maximum number of edges (A) 3 (B) 2


possible an undirected graph with 5 (C) 1 (D) None of these
nodes, when Depth First Search (DFS)
call is made on any random node in the Q.5 Which of the following algorithms can
graph result in stack size 5 i.e. 5 be used to most efficiently determine
function calls are present in stack the presence of a cycle in a given
simultaneously are _________ graph?

Q.2 Consider the following graph : (A) Depth First Search


(B) Breadth First Search
M N O
(C) Prim's Minimum Spanning Tree
Algorithm
(D) Kruskal's Minimum Spanning Tree
R Q P Algorithm
The number of back edges in the BFS Q.6 Traversal of a graph is different from
of this graph starting from node M is tree because
____________ (A) There can be a loop in graph so we
Q.3 Consider the following graph : must maintain a visited flag for
every vertex
M N O
(B) DFS of a graph uses stack, but
inorrder traversal of a tree is
recursive
R Q P (C) BFS of a graph uses queue, but a
time efficient BFS of a tree is
The number of forward edges in the recursive.
BFS of this graph starting from node M
is ____________ (D) All of the above

Q.4 The maximum possible height of BFS Q.7 Let G be an undirected graph. Consider
tree, if BFS is run on a complete a depth-first traversal of G, and let T
bipartite graph K m, n is : be the resulting depth-first search
tree. Let u be a vertex in G and let v be
Data Structures 51
the first new (unvisited) vertex visited (A) There is an edge from currently
after visiting u in the traversal. Which being visited node to an already
of the following statements is always visited node.
true? (GATE CS 2000) (B) There is an edge from currently
(A) {u,v} must be an edge in G, and u is being visited node to an ancestor of
a descendant of v in T currently visited node in DFS
(B) {u,v} must be an edge in G, and v is forest.
a descendant of u in T (C) Every node is seen twice in DFS.
(C) If {u,v} is not an edge in G then u is (D) None of the above
a leaf in T Q.10 Consider the tree arcs of a BFS
(D) If {u,v} is not an edge in G then u traversal from a source node W in an
and v must have the same parent in unweighted, connected, undirected
T graph. The tree T formed by the tree
Q.8 Given two vertices in a graph s and t, arcs is a data structure for
which of the two traversals (BFS and computing.
DFS) can be used to find if there is (A) the shortest path between every
path from s to t? pair of vertices.
(A) Only BFS (B) the shortest path from W to every
(B) Only DFS vertex in the graph.
(C) Both BFS and DFS (C) the shortest paths from W to only
(D) Neither BFS nor DFS those nodes that are leaves of T.
Q.9 Which of the following condition is (D) the longest path in the graph
sufficient to detect cycle in a directed
graph?


Classroom Practice Questions


1. D 2. D 3. D 4. C 5. C
6. B 7. 19 8. 31 9. B
Self-Practice Questions
1. 10 2. 0 3. 0 4. B 5. A
6. A 7. C 8. C 9. B 10. B



52 Data Structures
6 HASHING

Q.1 A hash table with ten buckets with one (A) i only (B) ii only
slot per bucket is shown in the (C) i and ii only (D) iii or iv
following figure. The symbols S1 to [2004 : 1 Mark]
S7initially entered using a hashing
Q.4 A hash table contains 10 buckets and
function with linear probing. The
uses linear probing to resolve
maximum number of comparisons
collisions. The key values are integers
needed in searching an item that is not
and the hash function used is key % 10.
present is
If the values 43, 165, 62, 123, 142 are
0 S7
inserted in the table, in what location
1 S1
would the key value 142 be inserted?
2
3 S4 (A) 2 (B) 3
4 S2 (C) 4 (D) 6
5 [2005 : 1Mark]
6 S5
Q.5 Which of the following statement(s) is
7
TRUE?
8 S6
9 S3 I. A hash function takes a message of
arbitrary length and generates a
(A) 4 (B) 5 fixed length code.
(C) 6 (D) 3 II. A hash function takes a message of
[1989 : 2 Marks] fixed length and generates a code of
Q.2 An advantage of chained hash table variable length.
(external hashing) over the open III. A hash function may give the same
addressing scheme is hash value for distinct messages.
(A) Worst case complexity of search (A) I only (B) II and III only
operations is less (C) I and III only (D) II only
(B) Space used is less [2006: 1Mark]
(C) Deletion is easier Q.6 Consider a hash table of size 11 that
(D) None of the above uses open addressing with linear
[1996 : 1 Mark] probing. Let h(k)=k mod 11 be the hash
Q.3 Given the following input (4322, 1334, function used. A sequence of records
1471, 9679, 1989, 6171, 6173, 4199) and with keys 43 36 92 87 11 4 71 13 14 is
the hash function x mod 10, which of inserted into an initially empty hash
the following statements are true? table, the bins of which are indexed
i. 9679, 1989, 4199 hash to the same from zero to ten. What is the index of
value the bin into which the last record is
ii. 1471, 6171 hash to the same value inserted?
iii. All elements hash to the same (A) 3 (B) 4
value
(C) 6 (D) 7
iv. Each element hashes to a different
[2008: 2Marks]
value
Data Structures 53
Q.7 The keys 12, 18, 13, 2, 3, 23, 5 and 15 are (A) 3, 0, and 1 (B) 3, 3, and 3
inserted into an initially empty hash (C) 4, 0, and 1 (D) 3, 0, and 2
table of length 10 using open
[2014 (Set-1): 2 Marks]
addressing with hash function h(k) = k
Q.9 Consider a hash table with 100 slots.
mod 10 and linear probing. What is the
Collisions are resolved using chaining.
resultant hash table?
Assuming simple uniform hashing, what
[2009 : 2 Marks]
is the probability that the first 3 slots
(A) (B) (C) (D) are unfilled after the first 3 insertions?
0 0 0 0
(A) (97 × 97 × 97)/ 1003
1 1 1 1
(B) (99 × 98 × 97)/ 1003
12,
2 2 2 12 2 12 2
2 (C) (97 × 96 × 95)/ 1003
13, (D) (97 × 96 × 95)/(3! × 1003 )
3 23 3 13 3 13 3 3,
23 [2014 (Set-3): 2 Marks]
4 4 4 2 4 Q.10 Given a hash table T with 25 slots that
5, stores 2000 elements, the load factor α
5 15 5 5 5 3 5 for T is __________
15
6 6 6 23 6 [2015 (Set-3): 1 Mark]
7 7 7 5 7 Q.11 Consider a double hashing scheme in
8 18 8 18 8 18 8 18 which the primary hash function is h1(k)
9 9 9 15 9 = k mod 23, and the secondary hash
Q.8 Consider a hash table with 9 slots. The function is h2(k) = 1+(k mod 19). Assume
hash function is ℎ(k) = k mod 9. The that the table size is 23. Then the
collisions are resolved by chaining. The address returned by probe 1 in the
following 9 keys are inserted in the probe sequence (assume that the
order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The probe sequence begins at probe 0) for
maximum, minimum, and average chain key value k = 90 is ________ .
lengths in the hash table, respectively, [2020 : 1 Mark]
are

Q.1 Consider a hash table of size seven, Q.2 Consider a hash function that
with starting index zero, and a hash distributes keys uniformly. The hash
function (3x + 4)mod7. Assuming the table size is 20. After hashing of how
hash table is initially empty, which of many keys will the probability that any
the following is the contents of the new key hashed collides with an
table when the sequence 1, 3, 8, 10 is existing one exceed 0.5.
inserted into the table using closed [2007 : 2 marks]
hashing? Note that ‗_‘ denotes an (A) 5 (B) 6
empty location in the table. (C) 7 (D) 10
(A) 8, _, _, _, _, _, 10 Linked Question Answer for 3 and 4.
(B) 1, 8, 10, _, _, _, 3 A hash table of length 10 uses open
(C) 1, _, _, _, _, _,3 addressing with hash function h(k)=k mod
(D) 1, 10, 8, _, _, _, 3 10, and linear probing. After inserting 6
values into an empty hash table, the table is
[2007:2Marks]
as shown below.
54 Data Structures
0 Q.7 A hash function h defined h(key)=key
1 mod 7, with linear probing, is used to
2 42 insert the keys 44, 45, 79, 55, 91, 18, 63
3 23 into a table indexed from 0 to 6. What
will be the location of key 18?
4 34
5 52 (A) 3 (B) 4
6 46 (C) 5 (D) 6
7 33 Q.8 Consider a hash table of size m =
8 10000, and the hash function h(K) =
floor (m(KA mod 1)) for A = ( √(5) – 1)/2.
9
The key 123456 is mapped to location
Q.3 Which one of the following choices ______.
gives a possible order in which the key (A) 46 (B) 41
values could have been inserted in the (C) 43 (D) 48
table? Q.9 Consider a 13 element hash table for
1. 46, 42, 34, 52, 23, 33 which f(key)=key mod 13 is used with
2. 34, 42, 23, 52, 33, 46 integer keys. Assuming linear probing
is used for collision resolution, at
3. 46, 34, 42, 23, 52, 33
which location would the key 103 be
4. 42, 46, 33, 23, 34, 52 inserted, if the keys 661, 182, 24 and
[2010:2Marks] 103 are inserted in that order?
Q.4 How many different insertion (A) 0 (B) 1
sequences of the key values using the (C) 11 (D) 12
same hash function and linear probing Q.10 Consider a hash table of size m = 100
will result in the hash table shown and the hash function h(k) = floor
above? (m(kA mod 1)) for
[2010:2Marks] ( 5  1)
(A) 10 (B) 20 A  0.618033
2
(C) 30 (D) 40 Compute the location to which the key
Q.5 Which one of the following hash k = 123456 is placed in hash table.
functions on integers will distribute (A) 77 (B) 82
keys most uniformly over 10buckets (C) 88 (D) 89
numbered 0 to 9 for i ranging from 0 to
2020?

(A) h(i) =𝑖 2 mod 10
(B) h(i) =𝑖 3 mod 10
(C) h(i) = (11 ∗ 𝑖 2 ) 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

Data Structures 55
Classroom Practice Questions
1. B 2. D 3. C 4. D 5. C
6. D 7. C 8. A 9. A 10. 80
11. 13
Self-Practice Questions
1. B 2. D 3. C 4. C 5. B
6. B 7. C 8. B 9. B 10. C



56 Data Structures

You might also like