0 Stimmen dafür0 Stimmen dagegen

103 Aufrufe50 Seitenwork_book_of_DS_ada legal file

Nov 17, 2013

© Attribution Non-Commercial (BY-NC)

DOCX, PDF, TXT oder online auf Scribd lesen

work_book_of_DS_ada legal file

Attribution Non-Commercial (BY-NC)

Als DOCX, PDF, TXT **herunterladen** oder online auf Scribd lesen

103 Aufrufe

work_book_of_DS_ada legal file

Attribution Non-Commercial (BY-NC)

Als DOCX, PDF, TXT **herunterladen** oder online auf Scribd lesen

- Modern GPU
- An empirical comparison of the runtime of five sorting algorithms
- Shell Sort
- 10 Sorting 1
- Review on Sorting Algorithms A Comparative Study
- CSC112-COURSE-HANDBOOK+Spring+2015--+Sana+Rizwan
- Ch13
- Chapter 10 - Sorting
- BucketSort Paper
- COS211 Sorting
- Chapter 08
- External Sorting
- 05
- lecture09_4on1
- 02 Sort Hash
- Sample Copy
- Unit I ppt.pptx
- Sorting Algorithm
- Sorting-Algorithm.pdf
- 1-s2.0-S1877050916315381-main

Sie sind auf Seite 1von 50

www.gaterevolution. Page 1

CONTENTS

Chapter Page No

1. Asymptotic Notation & Recurrence Relation 1

2. Sorting 11

3. Data Structures

STACK and QUEUE 19

Linked List 21

TREE 34

4. Recursion 43

5. Program Designing Techniques 48

(A) Practice Test 1 54

(B) Practice Test 2 57

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 2

Asymptotic Notation & Recurrence Relation

For next three questions, each of the three pairs of functions given below, rank the functions by increasing order

of growth; that is, find any arrangement g1; g2; g3 of the functions satisfying

g1 = O(g2), g2 = O(g3). lg n represents logarithm of n to the base 2.

Q1. f1(n) = 8n; f2(n) = 25

1000

; f3(n) = (3)lg n

(A) f2(n); f1(n); f3(n) (B) f2(n); f3(n); f1(n)

(C) f3(n); f2(n); f1(n) (D) f1(n); f2(n); f3(n)

Q2. f1(n) = 1/ 100 ; f2(n) = 1/n; f3(n) = lgn/n

(A) f2(n); f1(n); f3(n) (B)f2(n); f3(n); f1(n)

(C) f3(n); f2(n); f1(n) (D) f1(n); f2(n); f3(n)

Q3. f1(n) = ; f2(n) = n

lg n

; f3(n) = (lgn)!

(A) f2(n); f1(n); f3(n) (B) f2(n); f3(n); f1(n)

(C) f3(n); f2(n); f1(n) (D) f1(n); f2(n); f3(n)

Q4. Which of the following is FALSE?

(A) If f(n) = (g(n)) and g(n) = (h(n)), then h(n) = (f(n))

(B) If f(n) = O(g(n)) and g(n) = O(h(n)), then h(n) = (f(n))

(C) If f(n) = O(g(n)) and g(n) = O(f(n)) then f(n) = g(n)

(D) n/100 = (n)

Q5. Let f(n) = n and g(n) = log n, then

(A) f = O(g) (B) f = (g) (C) f = (g) (D) None of the above

Q6. Let f(n) = 1 and g(n) = 2, then

(A) f = O(g) (B) f = (g) (C) f = (g) (D) All of the above

Q7. Let f(n) = 1000 . 2

n

and

g(n) = 3

n

, then

(A) f = O(g) (B) f = (g) (C) f = (g) (D) None of the above

Q8. Let f(n) = 5nlog n and g(n) = n log

5

n, then

(A) f = O(g) (B) f = (g) (C) f = (g) (D) All of the above

Q9. Order the following functions in increasing order by their asymptotic growth rate

f1 = n

2

f2 = n! /n log n f3 = (1 + 1/n)

n

f4 = (log n)

log n

f5 = n

log log n

f6 = 3

log2

(A) f6, f1, f4, f5, f3, f2 (B) f6, f4, f1, f2, f5, f3

(C) f5, f6, f4, f1, f3, f2 (D) f3, f1, f5, f6, f4, f2

Q10. Rank the following functions by increasing order of growth

f1 = (n!)

1/n

f2 = log n

n

f3 = n

\n

f4 = nlogn loglogn

(A) f1, f2, f4, f3 (B) f1, f3, f2, f4 (C) f4, f2, f3, f1 (D) f2, f4, f3, f1

Q11. Which is the correct order of these functions in increasing order of asymptotic runtime:

(A) n

3

; n

logn

; (log n)

n

; 2

2 loglogn

; 4

logn

(B) 2

2 loglogn

; 4

logn

; n

3

; n

logn

; (log n)

n

(C) n

logn

; (log n)

n

; n

3

; 4

logn

; 2

2 loglogn

(D) 4

logn

; 2

2 loglogn

; n

3

; (log n)

n

; n

logn

Q12. What is the order of the functions d(n) = n

n

, f(n) = e

n

, g(n) = (log n)

n

, and h(n) = n! When ranked in

increasing order of asymptotic growth?

(A) f, g, d, h (B) f, g, h, d (C) g, f, d, h (D) g, h, f, d

Q13. Which of the following are true?

(i) (log n)

k

= O(n

c

) (ii) n

c

= O(k

n

) (iii) 2

n

= O(2

n/2

) (iv) (log n)

log n

= O(2

log n

)

(A) (i), (ii) only (B) (ii), (iii) only (C) (i), (iv) only (D) All are true

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 3

Q14. For the table given below find for each row whether

(a) f(n) = (g(n)) (b) f(n) = O(g(n)) (c) f(n) = (g(n))

(A) (I) - b, (II) - a, (III) - c, (IV) - a, (V) - c, (VI) - b, (VII) - b, (VIII) - b

(B) (I) - b, (II) - a, (III) - b, (IV) - c, (V) - c, (VI) - b, (VII) - a, (VIII) - c

(C) (I) - c, (II) - b, (III) - c, (IV) - a, (V) - a, (VI) - a, (VII) - a, (VIII) - b

(D) (I) - b, (II) - a, (III) - a, (IV) - a, (V) - c, (VI) - b, (VII) - b, (VIII) c

Q15. What does it mean to say that an operation takes constant time?

(A) The operation takes the same time on every computer

(B) The operation takes an amount of time that is independent of the size of its input

(C) The operation takes an amount of time equal to the size of its input plus some constant

(D) We cannot conclude any meaning from the given information

Q16. Which of the following is not O (n

2

)

(A) n + 10000n (B) n

1.9999

(C) 10

5

n + 2

6

n (D) n

3

/n

Q17. Suppose you run a O(log n) algorithm with an input size of 1000 and the algorithm requires 110

operations. When you double the input size to 2000, the algorithm now requires 120 operations. What is

your best guess for the number of operations required when you again double the input size to 4000?

(A) 130 (B) 140 (C) 150 (D) 160

Q18. Suppose that you are considering running two algorithms under the same conditions and on the same

computer, on problems of size n. Furthermore, suppose that Algorithm A has a worst-case runtime

complexity proportional to log n while algorithm B has a worst-case runtime complexity proportional to n.

For large enough values of n,

(A) Algorithm A is always faster than algorithm B, in the worst-case scenario.

(B) Algorithm A is always slower than algorithm B, in the worst-case scenario.

(C) Algorithm A may be faster or slower than algorithm B, in the worst-case scenario, depending on those

constants which we typically ignore when we use O-notation.

(D) We cannot conclude anything about this situation when n is large; we should be looking at the case

when n is small.

Q19. Three programs P1,P2 and P3 have time complexities f1(n), f2(n) and f3(n) respectively, such that is

f1(n) is O(f2(n)), f2(n) is O(f1(n)), f1(n) is O(f3(n)) and f3(n) is not O(f1(n)). Which of the following

statements must be true?

(A) Program P3 is always faster than P1 and P2.

(B) Program P1 is faster than P2 and P3 very large size inputs.

(C) Program P2 is faster than P1and P3 for very large size inputs.

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 4

(D) Program P3 is slower than P1and P2 For very large input

Q20. Which one of the following sorting algorithms always has the running time recurrence equation

T(n) = T(n 1) + n?

(A) Insertion sort (B) Bubble sort (C) Heap sort (D) Quick sort

Q21. Running time complexity for

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q22. Running time complexity for

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q23. How many bits do you need to write 10

n

in binary?

(A) n+1 (B) n-1 (C) n (D) 10

Q24. What is the solution of the recurrence relation T(n) = 9T(n/3) + n?

(A) O(log n) (B) O(n) (C) O(n log n) (D) O(n

2

)

Q25. If f(n) = n + n log n, which of the following are TRUE?

(i) O(n) (ii) O(n

2

) (iii) (n

2

) (iv) (n log n)

(v) (n log n) (vi) (n)

(A) (i) and (iv) only (B) (ii), (v), (vi) only

(C) (ii) & (iii) only (D) (ii), (iv) and (v) only

Q26. Recall that f(n) = O(g(n)) means that - c>0 and n

0

, such that nn

0

, |f(n)| c*g(n) Which of the following

choices of n

0

and c would work in the above definition to show that n

2

+ 3n = O(n

2

) ?

(A) n

0

= 1, c = 4 (B) n

0

= 3, c = 2 (C) (A) & (B) both (D) None of the above

Q27. If f(n) = O(g(n)), then lim

n

|f(n)/g(n)| can possibly be any element in:

(A) {0} (B) R+ (C) {0}R+ (D) R+ {}

Q28. If f(n) = O(g(n)), then there exist positive constants c and n

0

such that f(n) ____ c*g(n), when nn

0

. What

relational operator should go in the blank?

(A) < (B) s (C) = (D)

Q29. Which of the following equations is incorrect?

(A) f (n) = O(f (n)) (B) O(f (n)) + O(f (n)) = O(f (n))

(C) O(f (n))O(g(n)) = O(f (n)g(n)) (D) None of the above

Q30. Which of the following is true of the function T(n) ?

(A) T (n) is O (B) T (n) is

(C) T (n) is O (D) Both (B) and (C)

Q31. Which of the following is TRUE for the function T(n) = 3

n + c

where c is an integer constant as c > 0?

(A) T (n) is O (2

n

) (B) T (n) is O (c

n

) (C) T (n) is O (n

c

) (D) T (n) is O (3

n

)

Q32. A function f(n) = O(n) + u(n) + O(n). Which one of the following is a correct asymptotic bound for f(n)?

(A) O (n) (B) O (n) (C) u (n) (D) f(n) is unbounded

Q33. To what order (s) does 57*n

2

+ 3*n +8000 belong?

(A) O (n

2

) (B) O ((log n)

2

) (C) O (n

3

) (D) Both (A) and (C)

Q34. Which of the following functions are in the same order class?

(A) 17*n

2

+ 45*n + 46, (n + 1) (n- 1) (B) n

2n

, n

3n

(C) log

2

n, (logn)

2

(D) None of the above

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 5

Q35. log((\n)!) is

(A) O (\n loglogn) (B) O(\n logn) (C) O(n loglogn) (D) O(n logn)

Q36. Which of the following statement is correct?

(A) n

n

= O(2

n

) (B) n! = O(n

n

) (C) n

2

log n = u (n

2

) (D) n

2

/ log n = u(n

2

)

Q37. Suppose T

1

(n) = O(F(n)) and T

2

(n) = O(F(n)).which of the following are true?

(A) T

1

(n) + T

2

(n) = O(F(n)) (B) T

1

(n)*T

2

(n) = O(F(n))

(C) T

1

(n)/T

2

(n) = O(1) (D) T

1

(n) = O(T

2

(n))

Q38. Which of the following is the correct solution for T(n)= 3T(n/2) + nlgn ?

(A) (n

2

lgn) (B) (nlgn) (C) (nlg

2

n) (D) (n

lg3

)

Q39. Solve the following recurrence T(n) = 2*T(n/2) + n given that T(1) = 1.

(A) O (nlog

2

n) (B) O (log

2

n) (C) O (n) (D) O (1)

Q40. What is the time complexity of the recurrence relation T (n) = T (n 1) + 1, consider T(1) =1.

(A) O (logn) (B) O (1) (C) O (n) (D) O (n

2

)

Q41. What is the time complexity of the recurrence relation T (n) = T (n / 2) + 1 consider T(1) = 1.

(A) O (logn) (B) O (1) (C) O (n) (D) O (n

2

)

Q42. What is the time complexity of the recurrence relation T (n) = T (\n) + 1.

(A) O (logn) (B) O (loglogn) (C) O (n) (D) O (1)

Q43. Time complexity for recurrence relation T(n) = 2T(\n) + 1 is:

(A) O (logn) (B) O (n

2

) (C) O (nlogn) (D) O (n)

Q44. The recurrence relation f(n) = 4f(n/2) 4f(n/4) + n log n with f(1) = f(2) = 1 has for solution

(A) f(n) = O(n log n) (B) f(n) = O(n(log n)

2

)

(C) f(n) = O(n(log n)

4

) (D) f(n) = O(n

2

log n)

Q45. Which of the following closed forms is equivalent to the recurrence relation:

T(1) = 1

T(n) = 3T(n/3 ) + 2n

(A) n + 3n log

2

n (B) 3n + n log

2

n (C) 2n + n log

3

n (D) n + 2n log

3

n

Q46. Which correctly expresses the running time of the following function, assuming that i has a value of at

least 1 in the initial call?

void foo(int i){

if (i <= 1)

return;

else{

printl("foo");

foo(i / 2); }}

(A) O n) (B) O (logn) (C) O (n logn) (D) O (n)

Q47. What is the time complexity of the following recurrence relation?

T(n) = T(n-1) + O(n)

(A) O (n) (B) O (logn) (C) O (n

2

) (D) O(1)

Q48. What is the time complexity of given code fragment?

for (i = 1; i < n; i=i+2) {

printf(Hello); }

(A) O (n) (B) O (logn) (C) O (nlogn) (D) O(1)

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 6

Q49. for (i = 1; i < n; i=i*2) {

printf(Hello); }

(A) O (n) (B) O (logn) (C) O (nlogn) (D) O(1)

Q50. for (i = 0; i < n; i++) {

for (j = 1; j < n; j=j+2) {

printf(Hello); }}

(A) O (n

2

logn) (B) O (logn) (C) O (nlogn) (D) O(n

2

)

Q51. for (i = 0; i < n; i=i*2) {

for (j = 1; j < n; j++) {

System.out.println(Hello); }}

(A) O (n) (B) O (logn) (C) O (n

2

) (D) O(1)

Q52. The time complexity of an algorithm T(n), where n is the input size is given by

The order of this algorithm is

(A) O (n) (B) O (logn) (C) O (n

2

) (D) O(n

n

)

For next two question, consider the following recursive method:

int method (int n) {

if (n = = 0 || n= = 1) return 1;

return method (n/2)+method (1); }

Q53. The tightest timecomplexity of the method is:

(A) O (log n) (B) O (n) (C) O (n

2

) (D) O (2

n

)

Q54. Spacecomplexity of the method is:

(A) O (log n) (B) O (n) (C) O (n

2

) (D) O (2

n

)

Q55. Given a program, find its time complexity.

for(i =1; i s n; i = i +1) {

for(j = 1; j s n; j = j * 2) {

}}

(A) O (nlogn) (B) O (logn) (C) O (n) (D) O (n

2

)

Q56. Given a program, find its recurrence relation.

int P(int n) {

if (ns1)

return 1;

else

return 2 * P (n/2) + n; }

(A) T (n) = T (n / 2) + n (B) T (n) = T (n - 2) + 1

(C) T (n) = T (n - 2) + n (D) T (n) = T (n / 2) + 1

Q57. Given a program, find its recurrence relation.

int P (int n){

if(n s 1)

return 1;

else{

P(n 1);

for (i = 1; i s n; ++i);

return i;

}}

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 7

(A) T(n) = T(n 1) + 1 (B) T(n) = T(n 1) + n

(C) T(n) = T(n) + n (D) T(n) = T(n 1) + n

2

Q58. Given a program, find its recurrence relation.

void P(int n) {

if (n s 1)

return;

else {

P(n 1); }}

(A) T (n) = T (n 1) + 1 (B) T (n) = T (n 1) + n

(C) T (n) = T (n) + n (D) T (n) = T (n 1) + logn

Q59. The recurrence relation for following recursive function is

procedure Rec-G(n)

if (n = 0): return 0

if (n = 1): return 1

if (n = 2): return 1

return Rec-G(n - 1) + Rec-G(n - 3)

(A) T (n) = T (n-1) + T (n-3) + 1 (B) T (n) = T (n-1) + T (n-3) + n

(C) T (n) = T (n) + T (n/3) + 1 (D) T (n) = T (n-2) + T (n-3) + 1

Q60. Consider the following pseudo-code.

x = 0;

for i = 1 to n

for j = 1 to 4*i-3

x = x + 1;

Find the exact number of times the innermost statement (increment x) is executed. That is, what is the

final value of x?

(A) 2n

3

(B) 2n

2

n (C) 2n

3

n (D) n

Q61. Suppose we wish to compute x

n

for integer values of n. We can do this recursively as follows:

if n = 0, return 1

if n = 1, return x

if n > 1 and n is even, recursively compute y = x

n/2

and return y * y

if n > 1 and n is odd, recursively compute y = x

n1

and return x * y.

Assuming addition and multiplication are O(1) operations, what is the worst-case asymptotic complexity

of computing x

n

this way?

(A) O(log n) (B) O(n) (C) O(n

2

) (D) O(nlog n)

For the next three questions consider the following program:

func factorial(n) {

if (n = = 0)

return 1

else

return n * factorial(n - 1) }

Q62. What is the recurrence relation for the program?

(A) T(n) = T(n-1) + n (B) T(n) = T(n-1) + n!

(C) T(n) = T(n!) + 1 (D) T(n) = T(n-1) + 1

Q63. What is the time complexity for the program?

(A) O (n!) (B) O (log

2

n) (C) O (n) (D) O (2

n

)

Q64. What is the space complexity for the program?

(A) O (n!) (B) O (log

2

n) (C) O (n) (D) O (2

n

)

Solve the following recurrences and find an expression for the runtime T(n).

Q65. T (n) = 3T (n/2) + n

2

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 8

(A) (n

2

) (B) (nlogn) (C) (n

2

logn) (D) (n

3

)

Q66. T (n) = 4T (n/2) + n

2

(A) (nlogn) (B) (logn) (C) (n

3

) (D) (n

2

logn)

Q67. T (n) = T (n/2) + 2

n

(A) (nlogn) (B) (logn) (C) (2

n

) (D) (2

n

logn)

Q68. T (n) = 16T (n/4) + n

(A) (n

2

) (B) (n

4

) (C) (nlogn) (D) (n)

Q69. T (n) = 2T (n/2) + n log n

(A) (n

2

logn) (B) (n(log n)

2

) (C) (nlogn) (D) (n

2

(log n)

2

)

Q70. T (n) = 2T (n/4) + n

0.51

(A) (n

2

) (B) (n

0.51

logn) (C) (n

0.51

) (D) (n)

Q71. T (n) = 16T (n/4) + n!

(A) (n

4

) (B) (n

n

) (C) (n

2

) (D) (n!)

Q72. T (n) = \2T (n/2) + log n

(A) (n) (B) (2

n

logn) (C) (nlogn) (D) (nlogn)

Q73. T (n) = 3T (n/2) + n

(A) (n) (B) (nlogn) (C) (n

log

2

3

) (D) (n

log

3

2

)

Q74. T (n) = 3T (n/3) + \n

(A) (\n) (B) (\nlogn) (C) (logn) (D) (n)

Q75. T (n) = 4T (n/2) + cn

(A) (n) (B) (n

2

) (C) (1) (D) (n

c

)

Q76. T (n) = 3T (n/4) + n log n

(A) (n logn) (B) (logn) (C) (n

log

4

3

) (D) (n)

Q77. T (n) = 3T (n/3) + n/2

(A) (n/2) (B) (n) (C) (n logn) (D) (logn)

Q78. T (n) = 6T (n/3) + n

2

logn

(A) (n

2

) (B) (n

2

(logn)

2

) (C) (nlogn) (D) (n

2

log n)

Q79. T (n) = 7T (n/3) + n

2

(A) (n

2

) (B) (n

log

3

7

) (C) (n) (D) (n logn)

Q80. T (n) = 4T (n/2) + log n

(A) (n logn) (B) (logn) (C) (n

2

logn) (D) (n

2

)

Q81. T (n) = 3T (n/5) + lg

2

n

(A) (n

log

5

3

) (B) (lg

2

n) (C) (n logn) (D) (n

2

)

Q82. T (n) = 2T (n/3) + nlogn

(A) (n

log

3

2

) (B) (n) (C) (nlogn) (D) (n

2

logn)

Q83. T (n) = T (n/5) + log

2

n

(A) (log

2

n) (B) (log

3

n) (C) (n logn) (D) (logn)

Q84. T (n) = 8T (n/2) + n

3

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 9

(A) (n

3

) (B) (n

2

) (C) (n

2

logn) (D) (n

3

logn)

Q85. T (n) = 7T (n/2) + n

3

(A) (n

3

) (B) (n

log

2

7

) (C) (n

3

logn) (D) (n

2

)

Q86. T (n) = 81T (n/9) + n

4

lgn

(A) (n

2

) (B) (n

4

logn) (C) (n

4

) (D) (n

2

logn)

Q87. T (n) = 4T (n/3) + n

log

3

4

(A) (n

log

3

4

) (B) (n) (C) (n

log

3

4

log n) (D) (logn)

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 10

Sorting

Q1. Consider method selectionSort below. Assume that method swap exists, which swaps the array elements

of intArray according to the provided index parameters.

void selectionSort(int intArray[], int size){

for (int p = 0; p < size; p++){

int temp = p;

for (int q = p+1; q < size; q++)

if (intArray[q] < intArray[temp])

temp = q;

if (intArray[p] != intArray[temp])

swap(p,temp); }}

Assume that intArray stores a random list of integers prior to calling selectionSort. How will the Integers

be arranged after a call to selectionSort?

(A) Data will be arranged in ascending order.

(B) Data will be arranged in descending order.

(C) The largest integer will be stored in last array element.

(D) The smallest integer will be stored in the first array element

Q2. Consider method selectionSort below. Assume that method swap exists, which swaps the array elements

of intArray according to the provided index parameters.

public void selectionSort(int intArray , int size ){

for (int p = 0; p <size; p++){

int temp = p;

for (int q = p+1; q <size; q++)

if (intArray[q] > intArray[temp])

temp = q;

if (intArray[p] != intArray[temp])

swap(p,temp); }}

Assume that intArray stores a random list of integers prior to calling selectionSort. How will the Integers

be arranged after a call to selectionSort?

(A) Data will be arranged in ascending order.

(B) Data will be arranged in descending order.

(C) The largest integer will be stored in last array element.

(D) The smallest integer will be stored in the first array element.

Q3. A sorting algorithms is considered stable if it

(A) Preserves the relative order of any two equal elements in its input

(B) Preserves the relative order of most two equal elements in its input

(C) Perform its operations mostly in the same memory used by its input elements

(D) Can change the relative order of equal elements in its input

Q4. Which sort algorithm(s) have a quadratic worst case efficiency

(A) Bubble sort (B) Heap sort (C) Quicksort (D) Both A and C

Q5. Which sorting algorithm choice will require the fewest number of comparisons to re-sort an array?

Assume, the number of items is large and that the array was sorted. A new item is added to the next

available location in the array, and then the array is re-sorted.

(A) Bubble sort starting from last index (B) Bubble sort starting from first index

(C) Selection sort (D) Merge sort

Q6. Which sort algorithm would produce the following intermediate integer values in an array while being

sorted?

Original order: 2 9 4 8 6 3 5 1 7

At some intermediate stage: 1 2 3 9 4 8 6 5 7

(A) Selection sort (B) Insertion sort (C) Merge sort (D) Quick sort

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 11

Q7. Which list of values represents an intermediate stage of a merge sort of an array with these initial values?

5 19 15 7 4 9 12 4 1 16 13 6 3 11 8 5

(A) 1 3 4 4 5 5 6 7 15 16 13 12 19 11 8 9 (B) 1 4 4 5 7 9 12 15 19 16 13 6 3 11 8 5

(C) 4 4 1 5 7 6 3 9 8 5 11 12 13 15 16 19 (D) 4 4 5 7 9 12 15 19 1 6 13 16 3 11 8 5

Q8. Which statement is TRUE regarding sorted lists? Assume the list must always remain sorted?

(A) Add operation is more efficient in the linked node than in the array implementation.

(B) Remove operation is more efficient in the linked node than in the array implementation.

(C) Contains operation is more efficient in the linked node than in the array implementation.

(D) The items in the list must be comparable.

Q9. Insertion sort fits in a category of sorting algorithms. What is this category?

(A) Stable (B) Non-Stable

(C) O (nlog n) (D) Whose best case time is the same as the worst case time

Q10. Which sorting algorithm is most likely to produce this arrangement of values at an intermediate iteration

of the algorithm? The height of the bars indicates the value of the item at that position and the values are

in the process of being sorted in ascending order.

(A) Selection sort (B) Insertion sort (C) Merge sort (D) Quick sort

Q11. When is insertion sort a good choice for sorting an array?

(A) Each component of the array requires a large amount of memory.

(B) Each component of the array requires a small amount of memory.

(C) The array has only a few items out of place.

(D) The processor speed is fast.

Q12. The best case, in terms of the asymptotic running time, for Quick Sort happens when:

(A) The array is already sorted

(B) The array is sorted in reverse

(C) All elements of the array are equal

(D) The number of elements to the right of the pivot is approximately equivalent to the

number of elements to the left of the pivot

Q13. Suppose we are sorting an array of eight integers using quick sort, and we have just finished the first

partitioning with the array looking like this:

2 5 1 7 9 12 11 10

Which statement is correct?

(A) The pivot could be either the 7 or the 9

(B) The pivot could be the7, but it is not the 9

(C) The pivot is not the7, but it could be the 9

(D) Neither the 7 nor the 9 is the pivot.

Q14. If one uses the bubble sort algorithm in ascending order, in which of the following pairs will the data

values not interchange during the first pass? {20,60,50,37,16,92,70,35}.

(A) 20,60 (B) 60,50 (C) 60,37 (D) 50,37

Q15. Suppose that you have an array of 128 elements. Which number is closest to the minimum number of

binary that element comparisons would be necessary to sort the elements if they are originally in random

order?

(A) 10 (B) 4075 (C) 389 (D) 891

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 12

Q16. For a d-heap (where d is 4 and addressing begins at 0), a parent of i is located at

(A) i/4 (B) (i-1)>>2 (C) (i-1)>>4 (D) i*4+1

Q17. Which is true of a Mergesort and a Quicksort?

(A) Both have the same average case complexity

(B) Both are recursive

(C) Both take O(n) to divide the problem or combine the pieces

(D) All of the above

Q18. Here is an array which has just been partitioned by the first step of quicksort:

3, 0, 2, 4, 5, 8, 7, 6, 9

Which of these elements could be the pivot?

(A) 3 (B) 4 (C) 5 (D) (B) or (C)

Q19, Q20 & Q21 deal with the binary heap H below.

H = [20 15 17 10 12 5 16 7 3 11]

Q19. What will the heap look after inserting 25

(A) [20 15 17 10 12 5 16 7 3 11] (B) [25 20 17 10 15 5 16 7 3 11 12]

(C) [20 15 17 10 12 5 16 7 3 11 25] (D) [25 15 17 10 12 5 16 7 3 11 20]

Q20. What would H look like if we'd inserted 14 and 18 instead?

(A) [20 15 17 10 14 5 16 7 3 11 12 18] (B) [20 15 18 10 12 17 16 7 3 11 14 5]

(C) [20 15 17 10 12 5 16 7 3 11 14 18] (D) [20 15 18 10 14 17 16 7 3 11 12 5]

Q21. What would H look like if instead of inserting anything, we called DeleteMax()?

(A) [17 15 16 10 12 5 11 7 3] (B) [17 15 16 10 12 11 5 7 3]

(C) [17 16 15 10 12 5 11 7 3] (D) [20 15 17 10 12 5 16 7 3]

Q22. What is the time complexity of heapsort?

(A) O (1) (B) O (n) (C) O (n logn) (D) O (n

2

)

Q23. What is the worst-case time for mergesort to sort an array of n elements?

(A) O (logn) (B) O (n) (C) O (n logn) (D) O (n)

Q24. What is the worst-case time for quicksort to sort an array of n elements?

(A) O (log n) (B) O (n) (C) O (n log n) (D) O (n)

Q25. Suppose we are sorting an array of eight integers using a quadratic sorting algorithm. After four iterations

of the algorithm's main loop, the array elements are ordered as shown here:

1 3 6 7 14 11 9 12

Which statement is correct?

(A) The algorithm is a Heap sort (B) The algorithm is a Mergesort.

(C) The algorithm is an Insertion sort (D) The algorithm is a Quicksort.

Q26. N elements are inserted one by one into an initially empty binary heap. The total running time is

(A) O (N) worst case and O (logN) average case

(B) O (N) worst case and O (NlogN) average case

(C) O (NlogN) worst case and O (N) average case

(D) O (NlogN) worst case and O (NlogN) average case

Q27. Suppose we consider a variation of Mergesort in which we divide the list to be sorted into four sublists of

equal size, recursively sort each sublist, and then merge the four lists to find the final sorted list. What

recurrence would we obtain for the number of comparisons used by this algorithm in the worst case?

(Assume that the number of elements to be sorted is a power of 4, so that all of the divisions into four

sublists work out evenly.)

(A) T(n) = 4 T(n) + (n) (B) T(n) = T(n/4) + (n)

(C) T(n) = 4 T(n/2) + (n) (D) T(n) = 4 T(n/4) + (n)

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 13

Q28. Mergesort and Quicksort could be viewed as applications of a technique called Divide-and-Conquer. In

this technique we solve a problem by

(i) dividing it into subproblems,

(ii) recursively solving the subproblems, and then

(iii) combining the solutions to the subproblems to get a solution to the original problem.

Outside of the time spent in recursive calls, where did Quicksort do its comparisons of keys?

(A) In stage (i) (B) In stage (iii)

(C) In both stage (i) and stage (iii) (D) None of the above

Q29. Suppose we are storing a heap in an array A of 13 elements numbered from 1 to 13. At present the heap

has 13 elements, and the contents of the array, in order from A[1] to A[13], are

17, 27, 22, 29, 31, 42, 86, 43, 56, 71, 34, 67, 53

This heap is being used to represent a priority queue in which the value in a node is always less than or

equal to the value in any of its children. Suppose we delete the minimum element using the priority queue

deletion algorithm. Where will the element 53 wind up afterwards?

(A) A[3] (B) A[4] (C) A[5] (D) A[6]

Q30. Starting with the initial heap of the previous problem, with 13 elements, suppose we insert a new element

with key 81, use the priority queue insertion algorithm. Where will the newly inserted element with key 81

wind up in the array?

(A) A[14] (B) A[7] (C) A[6] (D) A[3]

Q31. Which of the following problems could we solve in O(n) time by using Radixsort on the base-n

representation of the keys?

(A) Sort n numbers in the range 0 to n

2

1 (B) Sort n numbers in the range 0 to n

3

- 1

(C) Sort n numbers in the range 0 to n

4

1 (D) All of the above

Q32. If an array of six integers is already perfectly sorted in ascending order and you invoke the Insertion Sort

to sort it in ascending order, how many comparisons will be made during the execution of the sort?

(A) 0 (B) 5 (C) 6 (D) 15

Q33. If an array of six integers is already perfectly sorted in ascending order and you invoke the Bubble Sort to

sort it in ascending order, how many comparisons will be made during the execution of the sort?

(A) 0 (B) 5 (C) 6 (D) 15

Q34. Given an array of seven elements:

36, 58, 29, 31, 15, 2, 17

After two successful iteration of bubble sort, the array will look like:

(A) 31, 29, 15, 17, 2, 36, 58 (B) 2, 15, 31, 29, 17, 36, 58

(C) 29, 31, 15, 2, 17, 36, 58 (D) 2, 15, 17, 29, 31, 36, 58

Q35. Consider an array of integers: 4 10 1 2 6 7 3 5

If Selection sort is used to order the array from smallest to larges values, which of the following represents

a possible state of the array at some point during the Selection Sort process?

(A) 1 4 10 2 3 6 7 5 (B) 1 2 4 6 10 7 3 5 (C) 1 2 3 10 6 7 4 5 (D) 4 3 1 2 6 7 10 5

Q36. Consider the task of sorting the elements of an array in ascending order. Which of the following

statement(s) is/are true?

I. Selection Sort always requires more comparisons than Insertion Sort.

II. Insertion Sort always requires more swaps than Selection Sort.

III. Insertion Sort, on average, requires more moves than Selection Sort.

(A) I only (B) II only (C) III only (D) I and II

Q37. Which of the following best describes the return value for the method propertyX below?

// precondition: v.length >= 2

boolean propertyX(int []v){

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 14

boolean flag = false;

int i;

for (i = 0; i < v.length 1; i++){

flag = flag || (v[i] == v[i+1]);

}

return flag;

}

(A) Returns true if the elements of v are sorted in ascending order, false otherwise

(B) Returns true if the elements of v are sorted in descending order, false otherwise

(C) Returns true if v has two adjacent elements with the same value, false otherwise

(D) Returns true if v has two elements with the same value, false otherwise

Q38. How many element comparisons does straight Quicksort do if the input array of 7 elements is already

sorted? (A) 06 (B) 713 (C) 1421 (D) 2227

Q39. From fastest to slowest, the correct order for the following algorithms is:

(A) Linear search, Binary search, Bubble Sort, Selection Sort, finding the maximum element of an

unsorted array, finding the maximum element of a sorted array.

(B) finding the maximum element of an unsorted array, finding the maximum element of a sorted array,

Linear search, Binary search, Selection Sort, Bubble Sort.

(C) finding the maximum element of a sorted array, Binary search, Linear search, finding the

maximum element of an unsorted array, Selection Sort, Bubble Sort.

(D) Bubble Sort, Selection Sort, Binary search, Linear search, finding the maximum element of a sorted

array, finding the maximum element of an unsorted array.

Q40. Is the Binary Search always preferred over the Linear Search, and why?

(A) Yes, because the Binary Search is always faster than the Linear Search.

(B) Yes, because the Binary Search can search any type of data.

(C) No, because the Linear Search is faster than the Binary Search with sorted data.

(D) No, because the Binary Search only works with sorted data, unlike the Linear Search.

Q41.

Q42. Assume that intArray contains the following set of sorted integers.

{100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200}

How many comparisons are made by a Binary Search method to find number 130?

(A) 2 (B) 3 (C) 4 (D) 5

Q43. What is the best-case runtime complexity of searching for an item in a sorted array using linear search.

(A) O (N lgN) (B) O (N) (C) O (1) (D) O (lg N)

Q44. Suppose we have three sorted lists of lengths 3, 4, 5 respectively. If we merge them by first merging the 4

and 5 length lists and then merge the resultant 9 length list with the 3 length list then the number of

comparisons in the worst case is 19. What is the minimum number of comparisons required in the worst

case to merge six sorted lists of length 3,4,4,6,7,11 respectively?

(A) 030 (B) 3160 (C) 6190 (D) 91120

Q45. Given an array of eight elements

57, 20, 35, 44, 37, 19, 7, 59

After three successful iterations of selection sort the array will be:

(A) 7, 19, 20, 57, 44, 37, 35, 59 (B) 7, 19, 20, 44, 37, 35, 57, 59

(C) 7, 19, 35, 44, 37, 20, 57, 59 (D) 7, 19, 20, 44, 35, 37, 57, 59

Q46. What is the best searching algorithm to use if the list of data is not sorted?

(A) First sort the data then do a binary search using an array

(B) Linear search

(C) Binary search using an array

(D) Binary search using tree

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 15

Q47. Which of the following can be performed in expected O(n) time (assuming O(n) auxiliary storage)?

(i) Sort n integers each having a fixed maximum number of digits

(ii) Sort n real numbers each stored in a finite representation on a computer

(iii) Merge two sorted arrays of n elements

(A) (i) (B) (iii) (C) (i) and (ii) (D) (i), (ii), and (iii)

Q48. Which of the following is/are true of a binary max-heap with n nodes?

(ii) All leaves appear only at the bottom level

(iii) The smallest key must be in the leftmost position of its level

(A) (i) (B) (iii) (C) (i) and (iii) (D) (i) and (ii)

Q49. Which of the following is/are true of Mergesort?

(i) The worst-case time complexity is O (n log n).

(ii) The average -case time complexity is O (n log n).

(iii) Before the final call to Merge, the elements are partitioned into two sub-arrays of equal length such

that all elements in the first sub-array are less than or equal to those in the second half.

(A) (i) and (ii) (B) (i) and (iii) (C) (ii) and (iii) (D) (i), (ii) and (iii)

Q50. Suppose that we are representing a heap in memory using the array representation The elements of the

heap are stored in A[1],A[2], : : : ,A[23]; A[1] contains the root value. Where could we find the left child

of the right child of the left child of the root?

(A) A[14] (B) A[13] (C) A[12] (D) A[10]

Q51. How many comparisons will be needed using a sequential search to find the value 69 in the following

array?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

14 27 29 38 42 55 57 61 64 69 77 79 84

(A) 1 (B) 2 (C) 9 (D) 10

Q52. How many comparisons will be needed using a binary search to find the value 69 in the following array?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

14 27 29 38 42 55 57 61 64 69 77 79 84

(A) 1 (B) 2 (C) 9 (D) 10

Q53. The minimum number of comparisons required to find the largest number from 4 different numbers are

(A) 4 (B) 3 (C) 5 (D) 6

Q54. Given an array of seven elements, 2, 4, 6, 1, 8, 7, 5

After how many iterations of insertion sort the array will look like this: 1, 2, 4, 6, 8, 7, 5

(A) 2 (B) 3 (C) 4 (D) 5

Q55. What is the worst-case time for Merge-Sort on n distinct elements?

(A) O (1) (B) O (n) (C) O (nlog n) (D) u (nlog n)

Q56. What is the running time of Quick-Sort on n identical elements?

(A) O (1) (B) O (n) (C) O (nlog n) (D) O (n

2

)

Q57. What is the worst-case time for Radix-Sort on n distinct elements, each in the range 1 to lgn?

(A) O (1) (B) O (n) (C) O (nlog n) (D) O (n

2

)

Q58. What is the worst-case time for Radix-Sort on n distinct elements, each in the range 1 to n

2

?

(A) O (1) (B) O (n) (C) O (nlog n) (D) O (n

2

)

Q59. What is the best-case time for Heap-Sort on n distinct elements?

(A) O (n) (B) O (lg*n) (C) u (nlog n) (D) O (n

2

)

Q60. What is the amount of extra storage used by Merge-Sort on n distinct elements?

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 16

(A) O (1) (B) O (n) (C) u (n) (D) O (nlogn)

Q61. What is the worst-case time for Build-Max-Heap on an unordered array of n distinct elements?

(A) O (1) (B) O (n) (C) u (n) (D) O (n)

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 17

STACK & QUEUE

Q1. Which of the following best represents the Big-Oh runtime for pushing an item onto an array implemented

stack

(A) O(N lgN) (B) O(N) (C) O(1) (D) O(lgN)

Q2. If the variables are suitably initialized and if i remains within appropriated bounds, then the following

code implements stack operations Push and Pop when the stack is represented as a vector V [1; ::N] and a

pointer i.

Push: begin V[i] = x; i = i + 1; end

Pop: begin i = i - 1; x = V[i]; end

Which of the following gives the correct initialization for this stack implementation?

(A) i = 0 (B) i = 1 (C) i = N 1 (D) i = N

Q3. If it is assumed that suitable changes in the initialization code were also made, which of the following

changes to Push and Pop would yield a correct implementation of stacks?

I. Replace the code for Push with that for Pop and vice versa.

II. Make Push decrement i and Pop increment i.

III. Reverse the order of the statements in both Push and Pop.

(A) I only (B) II only (C) III only (D) I and II

Q4. The result of evaluating the following postfix expression is

5, 7, 9, *, +, 4, 9, 3, /, +, -

(A) 50 (B) 65 (C) 61 (D) 69

Q5. The prefix form of an infix expression A+BC*D is

(A) +AB*CD (B) +A B C * D (C) +A B * C D (D) + *ABCD

Q6. The postfix form of A ^ B * C - D + E/ F/ (G + H)

(A) AB^C*D-EF/GH+/+ (B) AB^CD-EP/GH+/+*

(C) ABCDEFGH+//+-*^ (D) AB^CD +EFGH +//*+

Q7. The data structure required to evaluate a postfix expression is

(A) queue (B) stack (C) array (D) linked-list

Q8. The data structure required to check whether an expression contains balanced parenthesis is

(A) Stack (B) Queue (C) Tree (D) Array

Q9. The postfix form of A*B+C/D is

(A) *AB/CD+ (B) AB*CD/+ (C) A*BC+/D (D) ABCD+/*

Q10. What is the postfix form of the following prefix *+abcd

(A) ab+cd* (B) abc+* (C) ab+*cd (D) ab+*cd

Q11. The prefix form of AB/(C * D ^ E) is,

(A) /*^ACBDE (B) ABCD*^DE (C) A/B*C^DE (D) A/BC*^DE

Q12. Suppose we have implemented the Stack ADT as a singly-linked-list with head and tail pointers and no

sentinels. Which of the following best describe the running times for the functions push and pop,

assuming there are O(n) items in the list, and that the bottom of the stack is at the head of the list (all

pushing and popping occurs at the tail)?

(A) O(1) for both functions (B) O(n) for both functions

(C) O(1) for push and O(n) for pop (D) O(n) for push and O(1) for pop

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 18

Q13. Suppose that a client performs an intermixed sequence of stack push and pop operations. The push

operations push the integers 0 through 9 in order on to the stack; the pop operations print out the return

value. Which of the following sequences could not occur?

(A) 4 3 2 1 0 9 8 7 6 5 (B) 2 1 4 3 6 5 8 7 9 0

(C) 0 4 6 5 3 8 1 7 2 9 (D) 4 6 8 7 5 3 2 9 1 0

Q14. In an array-based implementation of a stack we achieve ecient push and pop operations by.

(i) placing the top of the stack at the start of the array.

(ii) placing the bottom of the stack at the start of the array.

Make one of the following choices

(A) Only (i) can be used (B) Only (ii) can be used

(C) Either (i) or (ii) can be used (D) Neither (i) nor (ii) can be used

Q15. Which expression is true if and only if a circular array implementation of a queue of characters is full?

Assume that the front (head) and back (tail) are valid indices of the current array, one location is unused

when full, the reference to the array of size N is named data, and the enqueue operation puts the item in

the location given by tail and then advances the value of tail. This diagram represents one possible full

queue.

(A) head = = tail + 1 (B) (head + 1) % N = = tail

(C) head = = (tail + 1) % N (D) (head - 1) % N = = tail

Q16. n elements of a Queue are to be reversed using another queue. The number of ADD and REMOVE

operations required to do so is

(A) 2*n (B) 4*n (C) n (D) Task cannot be accomplished

Q17. Consider the implementation of the Queue using a circular array. What goes wrong if we try to keep all

the items at the front of a partially-filled array (so that data[0] is always the front).

(A) The remove method would require linear time.

(B) The insert method would require linear time.

(C) The isEmpty method would require linear time.

(D) None of the above

For next three questions, consider the following operations on a Queue data structure that stores integer values.

Queue q = new Queue( );

q.enqueue(3);

q.enqueue(5);

q.enqueue(9);

print(q.dequeue( )); // d1

q.enqueue(2);

q.enqueue(4);

print(q.dequeue( )); // d2

print(q.dequeue( )); // d3

q.enqueue(1);

q.enqueue(8);

Q18. After the code above executes, how many elements would remain in q?

(A) 0 (B) 4 (C) 5 (D) 6

Q19. What value is returned by the last dequeue operation (denoted above with a d3 in comments)?

(A) 3 (B) 5 (C) 9 (D) 2

Q20. If we replace the each print statements d1, d2 and d3 with the statement q.enqueue(q.dequeue( )); q would

contain which order of integer values after all instructions have executed?

(A) 3, 5, 9, 2, 4, 1, 8 (B) 3, 5, 9, 1, 8, 2, 4 (C) 5, 9, 2, 4, 1, 8, 3 (D) 3, 2, 4, 5, 9, 1, 8

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 19

Q21. Consider an instance of data structure illustrated below. It has an add() method to add an element and

remove() method to remove an element. The figure below shows an example of this data structure

(referred to as d) initially, after d.add(3) is called, and after d.remove( ) is called.

Which type of data structure best describes this data structure?

(A) Stack (B) Queue (C) Priority queue (D) Dequeue

Q22. In the circular array version of the Queue, which operations require linear time for their worst-case

behavior?

(A) remove (B) insert when the capacity has not yet been reached

(C) isEmpty (D) None of these operations require linear time.

Q23. A queue cannot be implemented using only____ for holding data.

(A) a stack (B) a linked list (C) an array (D) None of the above

Q24. Enqueue for a Queue implemented with a Singly Linked List with a tail pointer, where the front of the

queue is at the end (tail) of the list. (Enqueue occurs at the head.)

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q25. Dequeue for a Queue implemented with a Singly Linked List with a tail pointer, where the front of the

queue is at the end (tail) of the list. (Enqueue occurs at the head.)

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 20

LINKED LISTS

Q1. Which of the following best represents the Big-Oh runtime for dequeuing an item from a linked list

implemented queue

(A) O(N lgN) (B) O(N) (C) O(1) (D) O(lgN)

Q2. Lists may be either array-based or pointer-based (linked lists). Which of the following specify an

advantage of a pointer-based implementation of a list over an array-based implementation of a list:

(A) Less space for each item (B) Faster inserts and removes in the middle of the list

(C) No unused space (D) None of the above

Q3. Lists may be either array-based or pointer-based (linked lists). Which of the following specify an

advantage of an array-based implementation of a list over a pointer-based implementation of a list:

(A) Less space for each item (B) Faster inserts and removes in the middle of the list

(C) No unused space (D) None of the above

Q4. A doubly linked list is declared as:

struct Node {

itemtype item;

Node* next;

Node* prev;

};

Which of the following segments of code removes the element pointed to by X from the doubly linked list,

if it is assumed that X points to neither the first nor the last element in the list?

(A) Xprevnext = Xnext; Xnextprev = Xprev;

(B) Xprevnext = Xprev; Xnextprev = Xnext;

(C) Xprevprev = Xnext; Xnextnext = Xprev;

(D) Xprevprev = Xprev; Xnextnext = Xnext;

Q5. Let we are implementing a queue using linked list, and we have a item N to Enqueue, which of the

following code fragment correctly enqueues an item so that the dequeue operation will also be efficient?

(A) Nnext = head; head = N; (B) tailnext = N; Nnext = NULL;

(C) Nnext = tail; tailnext = NULL; (D) headnext = N; Nnext = head;

Q6. Which data structure can utilize a binary search to find the desired item?

(A) sorted list implemented with linked nodes

(B) sorted list implemented with an array

(C) dictionary implemented with a hash table

(D) dictionary implemented with a sorted list of linked list

Q7. Which of the following statements regarding singly linked lists is true?

(A) Insert at tail, remove at head and remove at tail can be performed in constant time.

(B) Insert at head, remove at head and remove at tail can be performed in constant time.

(C) Insert at head, insert at tail and remove at tail can be performed in constant time.

(D) Insert at head, insert at tail and remove at head can be performed in constant time.

Q8. Which of the following operations is performed more efficiently by doubly linked list than by singly

linked list

(A) Deleting a node whose location is given.

(B) Searching of an unsorted list for a given item.

(C) Inserting a new node after node whose location is given.

(D) Traversing the list to process each node.

Q9. Given the code fragment

struct NodeType {

int data;

NodeType *next;

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 21

};

NodeType *p;

NodeType *q;

p = new NodeType;

pdata = 12;

pnext = NULL;

q = new NodeType;

qdata = 5;

qnext = p;

Which of the following expressions has the value NULL?

(A) p (B) q (C) qnext (D) qnextnext

Q10. Given the declarations

struct ListNode {

float volume;

ListNode *next;

};

ListNode *headPtr;

ListNode *ptr;

float searchVal;

Assume that headPtr is the external pointer to a linked list of many nodes. Which code segment below

searches the list for the first occurrence of searchVal, leaving ptr pointing to the node where it was found?

(Assume searchVal is definitely in the list.)

(A) ptr = headPtr; (B) ptr = headPtr;

while (ptr.volume != searchVal) while (ptrvolume != searchVal)

ptr = ptr.next; ptr++;

(C) ptr = headPtr; (D) ptr = headPtrvolume;

while (ptrvolume != searchVal) while (ptr != searchVal)

ptr = ptrnext; ptr = ptrnext;

Q11. If we want to implement a particular data structure using a static array. We use the concept of circular

list to implement the data structure, because this allows us to efficiently use all fields of the array. Which

data structure are we implementing?

(A) A stack (B) A queue (C) Binary Tree (D) None of these

Q12. List1, List2, q and p are all pointers of type ListNode. Suppose List1 and List2 are references to the first

nodes in each of two linked lists and q references some node in the first list. The initial segment of the first

list, that is, all the nodes up to and including the one pointed to by q, is to be removed and this segment

put onto the beginning of the second list while the order of the nodes in the initial segment is maintained.

If neither q nor List1 is null, then this task is correctly performed by which of the following program

segments?

I. qnext = List2;

List2 = List1;

List1next = qnext ;

II. while (List1 != qnext){

p = List1;

List1 = List1next;

pnext = List2;

List2 = p;

}

III. p = qnext;

qnext = List2;

List2 = List1;

List1 = p;

(A) III only (B) I and II only (C) II and III only (D) I, II, and III

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 22

Q13. Consider a linked list of n elements. What is the time taken to insert an element after an element pointed

by some pointer?

(A) O (1) (B) O (log

2

n) (C) O (n) (D) O (nlog

2

n )

Q14. Two lists, A and B are implemented as singly linked lists. The address of the first and last node is stored in

variables firstA and lastA for list A and firstB and lastB for list B. Given the address of a node is given in

the variable node, the element stored in the node can be accessed by the statement nodedata and the

address to the next node can be accessed by

nodenext. In order to append list B at end of list A Which of the following statements should he use??

(A) lastB next = firstA (B) lastA = firstB

(C) lastAnext = firstB (D) lastB = firstA

Q15. What is the Big-O runtime (e.g., O(1), O(log n), or O(n)) for searching for an element in each sequential

data structure with n elements?

(A) ArrayList - O(n), LinkedList - O(n), OrderedArrayList - O(log n), OrderedLinkedList - O(n)

(B) ArrayList - O(1), LinkedList - O(n), OrderedArrayList - O(n), OrderedLinkedList - O(logn)

(C) ArrayList - O(n), LinkedList - O(logn), OrderedArrayList - O(log n), OrderedLinkedList - O(n)

(D) ArrayList - O(logn), LinkedList - O(n), OrderedArrayList - O(1), OrderedLinkedList - O(n)

Q16. What is the best Big-O runtime (e.g., O(1), O(log n), or O(n)) for inserting an element into each sequential

data structure with n elements?

(A) ArrayList - O(1), LinkedList - O(logn), OrderedArrayList - O(1), OrderedLinkedList - O(logn)

(B) ArrayList - O(n), LinkedList - O(n), OrderedArrayList - O(1), OrderedLinkedList - O(logn)

(C) ArrayList - O(1), LinkedList - O(1), OrderedArrayList - O(n), OrderedLinkedList - O(n)

(D) ArrayList - O(n), LinkedList - O(1), OrderedArrayList - O(n), OrderedLinkedList - O(n)

For the next five questions, consider a sorted circular doubly-linked list where the head element points to the

smallest element in the list.

Q17. What is the asymptoptic complexity of finding the smallest element in the list?

(A) O(1) (B) O(n) (C) O(logn) (D) O(n

2

)

Q18. What is the asymptoptic complexity of finding the largest element in the list?

(A) O(1) (B) O(n) (C) O(logn) (D) O(n

2

)

Q19. What is the asymptoptic complexity of determining whether a given element e appears in the list?

(A) O(1) (B) O(n) (C) O(logn) (D) O(n

2

)

Q20. What is the asymptotic complexity of finding the median element in the list?

(A) O(1) (B) O(n) (C) O(logn) (D) O(n

2

)

Q21. What is the asymptotic complexity of deleting a given element e in the list (not including the cost of

finding it)?

(A) O(1) (B) O(n) (C) O(logn) (D) O(n

2

)

Q22. Suppose we have implemented a queue as a singly linked list with a tail pointer modeled here: Which of

the following best describes the running time of the enqueue and dequeue operations if the rear of the

queue must be at the head of the linked memory structure? (n is the number of elements in the queue.)

(A) O(1) for both enqueue and dequeue (B) O(n) for both enqueue and dequeue

(C) O(n) for enqueue and O(1) for dequeue (D) O(1) for enqueue and O(n) for dequeue

Q23. Given an array-based implementation of a list of n elements, what is the worst case running time for

inserting an element to an arbitrary position in the list?

(A) O(1) (B) O(log n) (C) O(n) (D) O(n

2

)

Q24. Suppose we have implemented the Stack ADT as a singly-linked-list with head and tail pointers and no

sentinels. Which of the following best describe the running times for the functions push and pop,

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 23

assuming there are O(n) items in the list, and that the bottom of the stack is at the tail of the list (all

pushing and popping occurs at the head)?

(A) O(1) for both functions (B) O(n) for both functions.

(C) O(1) for push and O(n) for pop (D) O(n) for push and O (1) for pop

Q25. Consider the code given below

struct NODE *getnode() {

struct NODE *create;

create = (struct NODE *) (malloc(size of ( struct NODE)));

createnext = NULL;

return create ;

}

What does the above code do?

(A) Creates a node having NULL value

(B) Creates a node pointed to NULL

(C) Creates a node whose next node contains NULL value

(D) Update existing node create, change its value to NULL

Q26. struct NODE *operation( struct NODE *head, int num){

struct NODE *makenode;

makenode = (struct node *) malloc(sizeof(struct node));

if (head == NULL){

makenodedata = num;

head = makenode;

}

else {

makenodedata =num;

makenodenext =head ;

head =makenode ;

}

return head;

}

What does the above code do?

(A) Inserting a new node at the end of the Linked List

(B) Inserting a new node at the beginning of the Linked List

(C) Inserting a new node after the node containing value is equals to num in the Linked List

(D) Inserting a new node before the node containing value is equals to num in the Linked List

Q27. Node *operation(struct NODE *head, int num) {

struct NODE *makenode;

struct NODE *prev = NULL, *curr= NULL;

curr = head;

if (head = =NULL ) {

makenode = (struct NODE *) (malloc(size of ( struct NODE)));

makenodedata = num;

makenodenext = NULL;

head = makenode ;

return head ;

}

while(curr != NULL ) {

prev = curr;

curr = currnext;

}

makenode = (struct NODE *) (malloc(size of ( struct NODE)));

makenodedata = num;

Makenodenext = NULL;

prevnext = makenode ;

}

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 24

What does the above code do?

(A) Inserting a new node at the end of the Linked List

(B) Inserting a new node at the beginning of the Linked List

(C) Inserting a new node after the node containing value is equals to num in the Linked List

(D) Inserting a new node before the node containing value is equals to num in the Linked List

Q28. struct NODE * operation(struct NODE *head ) {

if(head == NULL) {

printf(\n operation not possible \n);

}

else

{

struct NODE *q;

q = head;

head =headnext;

free (q);

}

return head;

}

What does the above code do?

(A) Deleting a node at the end of the Linked List

(B) Deleting a node at the beginning of the Linked List

(C) Deleting a node at any random position in the Linked List

(D) Deletes the head node which makes linked list inaccessible

Q29. struct NODE *operation(struct NODE *head ) {

if(head==NULL) {

printf(/n operation not possible, List Empty/n);

}

else if (headnext == null){

free(head);

return NULL;

}

else{

Node *curr=head, *prev;

while(currnext!=NULL) {

Prev=curr;

curr=currnext;

}

prevnext = NULL;

free(curr);

}

return head;

}

What does the above code do?

(A) Deleting a node at the end of the Linked List

(B) Deleting a node at the beginning of the Linked List

(C) Deleting a node at any random position in the Linked List

(D) Deletes the head node which makes linked list inaccessible

Q30. struct NODE *operation(struct NODE *head, int node_num, int num) {

struct NODE *q;

int count=1;

struct NODE *curr, *prev;

curr=head;

q= (struct NODE *) (malloc(size of ( struct NODE)));;

qdata = num ;

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 25

while(count != node_num) {

prev = curr;

curr = currnext ;

count ++;

}

prevnext =q;

qnext=curr;

return head;

}

Consider the Linked list given below

L563112NULL

If we call operation(L, 4, 8); what will be the resulting Linked list

(A) L8563112NULL (B) L5631182NULL

(C) L5638112NULL (D) L56382NULL

Q31. void operation(struct NODE *head , int node_num , int num){

struct NODE *q;

int count = 1;

q = head;

struct NODE* makenode;

makenode = (struct NODE *) (malloc(size of ( struct NODE)));

makenodedata=num;

while(count != node_num) {

q = qnext ;

count ++ ;

}

makenodenext = qnext ;

qnext = makenode;

return head;

}

Consider the Linked list given below

L563112NULL

If we call operation(L, 4, 8); what will be the resulting Linked list

(A) L8563112NULL (B) L5631182NULL

(C) L5638112NULL (D) L56382NULL

Q32. void operation(struct NODE *head, int node_num) {

struct NODE *q, *next_q;

q=head;

int count=1;

while (count != node_num) {

count++;

q = qnext;

}

next_q = qnext;

qnext = next_qnext;

free(next_q);

}

Consider the Linked list given below

L5631182NULL

If we call operation(L, 4); what will be the resulting Linked list

(A) L561182NULL (B) L563112NULL

(C) L5638112NULL (D) L56382NULL

Q33. struct NODE *operation(struct NODE *head, int node_num) {

int count = 1;

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 26

struct NODE *curr, *prev;

curr = head;

while(count != node_num-1) {

prev = curr ;

count ++;

curr = currnext ;

}

prevnext = currnext ;

free(curr);

return head;

}

Consider the Linked list given below

L5631182NULL

If we call operation(L, 4); what will be the resulting Linked list

(A) L561182NULL (B) L563112NULL

(C) L5638112NULL (D) L56382NULL

Q34. int p(Struct nodes *list) {

int count = 0;

Struct node *p1;

p1 = list;

while(p1 != NULL) {

if(p1 data%2 == 0)

count++;

p1 = p1next;

}

return count;

}

What does the above code do?

(A) Returns the number of nodes in a singly Linked list

(B) Returns the number of nodes having even values in a singly Linked list

(A) Returns the number of nodes having odd values in a singly Linked list

(A) Returns 0 always

Q35. void p(Struct node *list) {

Struct node *p1, *p2;

p1 = list;

while(p1 next !=NULL) {

p2 =p1;

p1=p1 next ;

}

p2 next = list;

p1next = listnext;

listnext = NULL;

list = p1;

}

Consider the Linked list given below

List63118NULL

What will be the resulting Linked list if we pass List given above?

(A) L63118NULL (B) L63811NULL

(C) L83116NULL (D) L81163NULL

Q36. Consider the code given below for doubly Linked list

void p(Struct dlink *head, int x) {

Struct dlink *p1;

p1= head;

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 27

while(p1data != x && p1 != NULL)

p1 = p1next;

if(p1 != NULL) {

p1nextpre = p1pre;

p1prenext = p1next;

}}

What does the above code do?

(A) Removes a node from doubly Linked list, which is at position x

(B) Removes a node from doubly Linked list, which contains the value x

(C) Removes a node from doubly Linked list, which is at position x-1

(D) Removes a node from doubly Linked list, which is at position x+1

Q37. int p(Struct node *list1, Struct node *list2) {

static int flag;

if((list1 == NULL) && (list2 == NULL)) {

flag = 1;

}

else if(list1 == NULL || list2 == NULL) {

flag = 0;

}

else {

p(list1next, list2next);

}

return flag;

}

Consider the above code, it compares two linked list, what information it returns:

(A) Both are equal in length or not

(B) Returns 1 if list1 is greater in length than that of list2

(C) Returns 1 if list2 is greater in length than that of list1

(D) Always returns 0

Q38. Consider two code segment given below:

CODE I: {

Struct list *listptr, *nextptr

for(listptr = head; listptr != NULL; listptr = listptrnext) {

free(listptr);

}

CODE II: {

Struct list *listptr, *nextptr

for(listptr = head; listptr != NULL; listptr = nextptr) {

nextptr = listptrnext;

free(listptr);

}

head = NULL;

}

Which of the above CODE segment delete complete Linked list correctly?

(A) I only (B) II only (C) I & II both (D) Neither of I &II

TREE

Q1. Which tree could NOT result after deleting 10 from following simple BST

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 28

(A) (B) (C)

(D) They could all result from the deletion

Q2. Consider this binary search tree:

Suppose we remove the root, replacing it with its in-order successor. What will be the new root?

(A) 16 (B) 24 (C) 25 (D) 2

Q3. What does the following algorithm calculate when called with the root of a binary tree as the input?

int treeAlg(BinaryNode t) {

if (t.left != null || t.right != null)

return 1 + treeAlg(t.left) + treeAlg(t.right);

else

return 0;}

(A) the number of nodes in the tree

(B) the length of the shortest path from the root to a leaf

(C) the length of the longest path from the root to a leaf

(D) the number of internal (non-leaf) nodes

Q4. What is the time complexity of the tree algorithm in the previous question?

(A) O(log n) (B) O(n) (C) O(n log n) (D) O(n

2

)

Q5. Which of the following are true of the array implementation of a binary tree?

(i) The worst-case space complexity is u(|V|)

(ii) The node in a[i] has its children (if any) in a[2i] and a[2i+1], consider root to be at index 1.

(iii) If i < j, then a[i] necessarily corresponds to an ancestor of the node in a[j]

(A) (i) only (B) (ii) only (C) (i) and (ii) only (D) (ii) and (iii) only

Q6. Which of the following are necessarily true of the linked implementation of a binary tree?

(i) The worst-case space complexity is u(|V|)

(ii) Each internal vertex has neither a left pointer nor a right pointer of NIL

(iii) Each terminal vertex has both a left pointer and a right pointer of NIL

(A) (i) only (B) (i) and (ii) only (C) (i) and (iii) (D) (ii) and (iii)

Q7. AVL trees have LL, LR, RR, RL rotations to balance the tree to maintain the balance factor (LR : Insert

node in Right sub tree of Left sub tree of node A, etc). Among rotations the following are single and

double rotations

(A) LL, RL and LR, RR (B) LL, RR and LR, RL

(C) LR, RR and LL, RL (D) LR, RL and LR, RL

Q8. Which node is the deepest when the items 3, 6, 5, 2, 4, 7, 1 are inserted into an initially empty binary

search tree?

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 29

(A) 1 (B) 3 (C) 4 (D) 7

Q9. Suppose that we insert a 67 into the binary search tree shown below. In which position would the newly

inserted node appear?

(A) A (B) B (C) C (D) D

Q10. Suppose that we insert a 48 into the AVL tree (also known as height-balanced tree) shown below. Which

rotation will be performed?

(A) A single rotation at 23 (B) A single rotation at 46

(C) A double rotation at 23 (D) A double rotation at 46

Q11. Suppose that we have an AVL tree and then we perform an insertion that, before the rebalance operation,

increases the height of the tree. What are the possibilities for the balance of the root at this point (i.e.,

before the rebalance operation)?

(A) -1; 1 (B) -1; 0; 1 (C) -2,-1, 1, 2 (D) -2,-1, 0 1, 2

Q12. What would the recursive procedure below print if called on the root of the tree given below?

void traverse(x) {

if (x != null) {

print x.data;

traverse(x.left);

traverse(x.right);

print x.data;

}}

(A) ABCDEABCDE (B) ABBEEDDCCA (C) ABBCCDDEEA (D) ABBCDEEDCA

Q13. Suppose that we have a binary tree with fields left, right, and data as usual; the data fields contain integers.

An integer field value is also present in each node. The root of the tree is r. Assuming the procedure

mystery is as shown below, what would be the effect of the call mystery(r, 0)?

void mystery(x, i) {

if (x != null) {

x.value = x.data + i;

mystery(x.left, x.value);

mystery(x.right, x.value);

}}

(A) For all nodes x in the tree, set the value field to be equal to the sum of the data fields of the

descendants of x.

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 30

(B) For all nodes x in the tree, set the value field to be equal to the sum of the data fields of the

ancestors of x.

(C) For all nodes x in the tree (except the root), set the value field to be equal to the sum of the data fields

in x and its parent.

(D) For all nodes x in the tree, set the value field to be equal to the sum of the data fields of all nodes in

the tree.

Q14. Refer to method doSomething:

int doSomething (TreeNode *root) {

if (root != null)

if (rootRight() = = null)

return rootValue();

else

return doSomething(rootRight());

return null;}

Which best describes what doSomething does?

(A) It returns the largest element in a nonempty binary search tree.

(B) It returns the largest element in a nonempty tree.

(C) It returns an element at the highest level of a nonempty tree.

(D) It returns the smallest element in a nonempty binary search tree.

Q15. Each of the following lists of numbers is inserted, in the order given, into a binary search tree. Which list

produces the most balanced tree?

(A) 2, 4, 7, 5, 8, 10 (B) 9, 7, 2, 1, 4, 0 (C) 5, 1, 2, 6, 3, 4 (D) 6, 4, 1, 8, 10, 5

Q16. If you insert 1, 2 and 3 into an AVL tree. You can insert these numbers in any order. How many different

AVL trees can you obtain?

(A) 1 (B) 3 (C) 5 (D) 6

Q17. Refer to method whatsIt:

int whatsIt (TreeNode *tree) {

int x, y;

if (tree = = null)

return -1;

else {

x = 1 + whatIt(treeLeft());

y = 1 + whatIt(treeRight());

if (x > = y)

return x;

else

return y;

}}

Method whatsIt returns -1 for an empty tree. What does method whatsIt do when invoked for a nonempty

tree?

(A) It returns the largest value in the tree.

(B) It returns the number of nodes in the subtree that has the greatest number of nodes.

(C) It returns the level of the tree.

(D) It returns either the leftmost value or the rightmost value of a tree, whichever is larger.

Q18. How many AVL trees containing the keys 1, 2, 3, 4 can be built?

(A) 2 (B) 3 (C) 4 (D) 5

Q19. A full binary tree with 2n+1 nodes contain

(A) n leaf nodes (B) n non-leaf nodes

(C) n-1 leaf nodes (D) n-1 non-leaf nodes

Q20. If a node in a BST has two children, then its inorder predecessor has

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 31

(A) no left child (B) no right child (C) two children (D) no child

Q21. A full binary tree with n leaves contains

(A) n nodes (B) log

2

n nodes (C) 2n 1 nodes (D) 2

n

nodes

Q22. A BST is traversed in the following order recursively: Right, root, left The output sequence will be in

(A) Ascending order (B) Descending order

(C) Bitomic sequence (D) No specific order

Q23. The pre-order and post order traversal of a Binary Tree generates the same output. The tree can have

maximum

(A) Three nodes (B) Two nodes (C) One node (D) Any number of nodes

Q24. What is the maximum possible number of nodes in a binary tree at level 6?

(A) 16 (B) 32 (C) 64 (D) 128

Q25. How many nodes in a nonempty tree have no ancestors?

(A) 0 (B) 1 (C) 2 (D) n

For the next three questions, consider given binary search tree.

Q26. What is the preorder traversal of the tree?

(A) 45, 36, 23, 39, 41, 76, 56, 48, 69, 89, 115, 98

(B) 23, 36, 39, 41, 45, 48, 56, 69, 76, 89, 98, 115

(C) 23, 41, 39, 36, 48, 69, 56, 98, 115, 89, 76, 45

(D) None of the above

Q27. What is the inorder traversal of the tree?

(A) 115, 98, 89, 76, 69, 56, 48, 45, 41, 39, 36, 23

(B) 23, 36, 39, 41, 45, 48, 56, 69, 76, 89, 98, 115

(C) 23, 39, 36, 45, 41, 56, 48, 69, 76, 89, 115, 98

(D) 23, 45, 36, 39, 41, 48, 56, 69, 76, 89, 115, 98,

Q28. What is the postorder traversal of the tree?

(A) 23, 41, 39, 36, 48, 69, 56, 98, 115, 89, 76, 45

(B) 23, 39, 36, 45, 41, 56, 48, 69, 76, 89, 115, 98

(C) 45, 36, 23, 39, 41, 76, 56, 48, 69, 89, 115, 98

(D) None of the above

Q29. If values 1, 2, and 3 are added to a binary search tree in all possible orderings, how many unique trees can

be made?

(A) 4 (B) 5 (C) 6 (D) 8

Q30. nsert the following sequence of elements into an AVL tree, starting with an empty tree.

70, 40, 90, 50, 45, 60, 55, 52

What will be the resulting tree?

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 32

Q31. If we delete 40 from above tree what will be the resulting tree

Q32. Insert the following sequence of elements into an AVL tree, starting with an empty tree.

10, 20, 15, 25, 30, 16, 18, 19

What will be resulting the tree?

Q33. If we delete 30 from above tree what will be the resulting tree

Q34. What is the minimum and maximum numbers of node respectively in the AVL tree of depth 4,

considering depth of root is 1?

(A) 6 and 16 (B) 7 and 15 (C) 6 and 15 (D) 7 and 16

Q35. A binary search tree created by inserting these values in this order:

3 4 0 2 8 6 5 1 7 9

What will be the result of preorder traversal of that binary tree?

(A) 0 1 2 3 4 5 6 7 8 9 (B) 3 1 2 5 0 8 6 4 7 9

(C) 3 2 0 1 4 5 6 8 7 9 (D) 3 0 2 1 4 8 6 5 7 9

Q36. What is the best Big-O runtime (e.g., O(1), O(log n), O(h), or O(n)) for searching for an element in each

hierarchical data structure with n elements and tree height h?

(A) BinaryTree - O(n), BinarySearchTree - O(h), AVL Tree - O(log n)

(B) BinaryTree - O(logn), BinarySearchTree - O(n), AVL Tree - O(log n)

(C) BinaryTree - O(n), BinarySearchTree - O(n), AVL Tree - O(n)

(D) BinaryTree - O(h), BinarySearchTree - O(h), AVL Tree - O(log n)

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 33

Q37. What is the best Big-O runtime (e.g., O(1), O(log n), O(h), or O(n)) for inserting an element into each

hierarchical data structure with n elements and tree height h?

(A) BinaryTree - O(n), BinarySearchTree - O(h), AVL Tree - O(log n)

(B) BinaryTree - O(h), BinarySearchTree - O(n), AVL Tree - O(h)

(C) BinaryTree - O(n), BinarySearchTree - O(n), AVL Tree - O(log n)

(D) BinaryTree - O(h), BinarySearchTree - O(h), AVL Tree - O(log n)

Q38. What is the maximum number of times that the AVL tree will have to be restructured after an insertion?

(A) O(1) (B) O(n) (C) O(logn) (D) O(nlogn)

Q39. What is the maximum number of times that the AVL tree will have to be restructured after a removal?

(A) O(1) (B) O(n) (C) O(logn) (D) O(nlogn)

For the next three questions, check the ADT or data structure that is most appropriate for each of the following

problems:

Q40. You want to build an address book with entries in alphabetical order by last name.

(A) Unsorted List (B) Sorted List (C) Stack (D) Balanced Search Tree

Q41. You want to build a meeting reminder for a PDA that keeps track of events you schedule and periodically

checks the next event to sound an alarm to remind you of the next thing you need to do.

(A) Priority Queue (B) List (C) Hashtable (D) Stack

Q42. You want to build a table of contents for a textbook. The textbook consists of chapters, chapters consist of

sections, and sections consist of subsections.

(A) List (B) Hashtable (C) Tree (D) Binary Tree

Q43. What is the worst case running time of adding n distinct elements to a binary search tree?

(A) O(n) (B) O(nlog n) (C) O(log n) (D) O(n

2

)

Q44. Suppose that we have numbers between 1 and 1000 in a binary search tree and we want to search for the

number 363. Which of the following sequences cannot be the sequence of nodes visited in the search?

(A) 924, 220, 911, 244, 898, 258, 362, 363 (B) 2, 399, 387, 219, 266, 382, 381, 278, 363

(C) 925, 202, 911, 240, 912, 245, 363 (D) 935, 278, 347, 621, 399, 392, 358, 363

Q45. In an array-based implementation of a Heap, the right-child of the right-child of the node at index i, if it

exists, can be found at what array location consider root node is at index 1 in array ?

(A) 4i + 3 (B) 2i + 1 (C) 4i + 1 (D) 4i + 2

Q46. For which of the following data structures does the Find function require no worse than O(log n) running

time?

(A) Binary Search Tree (B) Heap

(C) AVL tree

(D) Find does not run in time O(log n) for any of these structures.

Q47. Which of the following is true about trees?

(A) A perfect binary tree is a binary tree in which every node has exactly zero or two children.

(B) A binary tree of height h can have at most 2h + 1 nodes.

(C) For a tree with more than 3 nodes, inorder and postorder traversals cant be the same.

(D) If you are given a trees preorder, inorder, and postorder traversals you can construct the tree.

Q48. Which of the following could not be a possible path in a binary search tree for seaech the element 223?

(A) 492, 125, 418, 197, 223 (B) 15, 982, 178, 645, 207, 517, 208, 223

(C) 995, 353, 254, 498, 223 (D) 128, 735, 209, 245, 223

Q49. In an n node AVL tree, what is the worst-case running time for a removal?

(A) O(log n) to find the node to be removed and O(1) to rebalance the tree after removing it.

(B) O(log n) to find the node to be removed and O(n) to rebalance the tree after removing it.

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 34

(C) O(n) to find the node to be removed and O(log n) to rebalance the tree after removing it.

(D) O(log n) to find the node to be removed and O(log n) to rebalance the tree after removing it.

Q50. In the following binary tree, which nodes are not height-balanced?

(A) B, C, and D (B) B and C (C) D and E (D) A, B, and E

Q51. Which of the following sequences of keys cannot be the inOrder traversal of an AVL tree?

(A) 1 3 6 8 12 15 17 19 (B) 50 120 160 172 183 205 200 230

(C) 12 14 20 22 24 27 40 45 (D) All of these are valid inOrder traversals.

Q52. Consider the Binary Search Tree built by inserting the following sequence of integers, one at a time:

5; 4; 7; 9; 8; 6; 2; 3; 1. If the node containing 5 were removed from the tree, what would be the right child

of the node containing 2?

(A) Null (B) The node containing 3

(C) The node containing 4 (D) The node containing 6

For the next ten questions consider the following data

Each item below is a description of a data structure, its implementation, and an operation on the structure. In

each case, choose the appropriate running time from the list below. The variable n represents the number of

items (keys, data, or key/data pairs) in the structure. In answering this question you should assume the best

possible implementation given the constraints, and also assume that every array is sufficiently large to handle

all items.

Q53. Worst case for finding a given key in a Binary Search Tree.

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q54. Worst case for inserting a single key into an AVL Tree.

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q55. Worst case for an algorithm to find the smallest key that is within fixed distance d from a given key in a

Binary Search Tree (if such a key exists).

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q56. Worst case for an algorithm to find the smallest key that is within fixed distance d from a given key in an

AVL Tree (if such a key exists).

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q57. Given a Binary Tree, check to see if it is a Binary Search Tree.

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q58. Remove all the nodes from an AVL Tree (as is done by the destructor).

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q59. Compute the height of every subtree in a Binary Search Tree.

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q60. Given a Binary Search Tree whose subtree heights have been computed determine if the Binary Search

Tree is an AVL Tree.

(A) O(1) (B) O(log n) (C) O(n) (D) O(n log n)

Q61. Suppose a binary tree holds 127 keys. Then our node-based implementation of that tree has how many

NULL pointers?

(A) 64 (B) 128 (C) 256

(D) The answer cannot be determined from the information given.

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 35

Recursion

Q1. void count (int a, int b){

if (a < = b){

printf (%d ,a );

count (a+1, b);

}}

What will be displayed by the method call count (10, 20)?

(A) 10 11 12 13 14 15 16 17 18 19 20 (B) 11 12 13 14 15 16 17 18 19 20

(C) 20 19 18 17 16 15 14 13 12 11 10 (D) 20 19 18 17 16 15 14 13 12 11

Q2. Consider the count method below.

void count (int a, int b) {

if (a < = b) {

count (a+1, b);

printf (%d, a);

}}

What will be displayed by the method call count (10, 20)?

(A) 10 11 12 13 14 15 16 17 18 19 20 (B) 11 12 13 14 15 16 17 18 19 20

(C) 20 19 18 17 16 15 14 13 12 11 10 (D) 20 19 18 17 16 15 14 13 12 11

Q3. What value will be returned by the call m09(5)?

int m09(int n) {

if (n = = 1)

return 25;

else

return m09(n-1);

}

(A) 1 (B) 5 (C) 25 (D) 125

Q4. What value will be returned by the call m10(5)?

int m10(int n) {

if (n = = 1)

return 25;

else

return n + m10(n-1);

}

(A) 5 (B) 39 (C) 125 (D) 195

Q5. What value will be returned by the call m11(1)?

int m11(int n) {

if (n = = 5)

return 0;

else

return n + m11(n+1);

}(A) 1 (B) 10 (C) 50 (D) 75

Q6. What value will be returned by the call m13(5)?

int m13(int n) {

if (n = = 1)

return 1;

else

return n * m13(n-1);

}

(A) 5 (B) 20 (C) 24 (D) 120

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 36

Q7. What value will be returned by the call m14(5, 6)?

int m14 (int a, int b) {

if (a == 0)

return 0;

else

return b + m14(a-1, b);

}

(A) 1 (B) 11 (C) 30 (D) 120

Q8. What is the value return by method(5)?

int method(int i) {

if (i < 1) return 0;

if (i = = 1) return 3;

return 2 * method( i-2 );

}

(A) 0 (B) 3 (C) 6 (D) 12

Q9. Which sorting algorithm, implemented recursively, appears below?

void recursiveSort(Comparable a[], int n) {

if (n > 1){

for (int i = 1; i < n; i++)

if (a[i-1] > a[i])

swapReferences(a, i-1, i);

recursiveSort(a, n-1);

}}

(A) BubbleSort (B) InsertionSort (C) RadixSort (D) SelectionSort

Q10. What is the following function computing? Assume a and b are positive integers.

int fn( int a, int b){

if (b = = 0)

return b;

else

return (a * fn(a, b - 1));

}

(A) Output will be 0 always (B) Output will always be b

(C) Computing ab (D) Computing a + b

Q11. What would be returned by the following recursive function after we call test(0, 3)

int test (int a, int b) {

if (a= =b) return 1;

else if (a > b) return 0;

else return (a + test(a+1, b));

}

(A) 1 (B) 2 (C) 3 (D) 4

For next two questions refer to method result:

public int result (int n){

if (n = = 1)

return 2;

else

return 2 * result (n-1);

}

Q12. What value does result(5) return?

(A) 64 (B) 32 (C) 16 (D) 8

Q13. If n > 0, What will be evaluate by result(n)?

(A) 2 (B) 2

n

(C) 2n (D) n

2

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 37

Q14. Refer to method mystery:

public int mystery (int n, int a, int d){

if (n = =1)

return a;

else

return d + mystery(n-1,a,d);

}

What value is returned by the call mystery (3, 2, 6)?

(A) 20 (B) 14 (C) 10 (D) 8

Q15. Consider the following code:

int foo(int a, int b) {

if (a%b = = 0)

return b;

else

return foo(b, a%b);

}

What is the output given by foo(17, 3)?

(A) 1 (B) 2 (C) 3 (D) 17

For the next two questions consider the following code fragment:

void p(int n){

if(n==1 || n== 2)

return 1;

else

return p(n-1) + p(n-2);

}

Q16. What does the given pseudo code compute?

(A) 2

n

(B) n! (C) Fibonacci (n) (D) n

2

Q17. What is the return value from the given algorithm for p(6)?

(A) 8 (B) 36 (C) 64 (D) 720

Q18. What is the output of the following program?

void Q (int n){

if (n < = 1)

return;

else{

Q (n 1);

printf (%d, n);

}}

void main (){

Q (4);

}

(A) 1 2 3 4 (B) 2 3 4 (C) 4 3 2 1 (D) 4 3 2

Q19. What is the output of the following program?

void Q (int n){

static int a = 1;

if (n < = 1)

return;

else {

Q (n 1);

a++;

printf (%d, a);

}}

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 38

void main (){

Q (4);

}

(A) 2 3 4 5 (B) 1 2 3 4 (C) 2 3 4 (D) 4 3 2

Q20 What is the value returned by Q(4)?

int Q (int n){

int y;

static int a = 1;

if (n < = 1)

return a;

else {

y = Q (n 1);

a = a + y;

return y;

}}

(A) 1 (B) 2 (C) 3 (D) 4

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 39

Program Designing Techniques

Q1. We employ dynamic programming approach when

(A) It gives optimal solution (B) The solution has optimal substructure

(C) It is faster than Greedy technique (D) The given problem can be reduced to the 3-SAT problem

Q2. Which of the following are true of the fractional knapsack problem, where x

i

is the fraction of item i

placed in the knapsack?

I. The items are processed in increasing order of value-weight ratios.

II. If the sum of all the weights is less than the capacity of the knapsack, then x

i

= 1 for i=1 n.

III. If the sum of all the weights is greater than or equal to the capacity of the knapsack, then the greedy

strategy will fit the knapsack exactly with an optimal solution.

(A) III (B) I and II (C) I and III (D) II and III

Q3. Which of the following is not true of the Huffman-Encoding algorithm? (Let n denote the maximum

number of distinct elements in the character set.)

(A) The time required to build the frequency-table is linear in respect to the length of the original file.

(B) The time required to build the Huffman-tree is O (n log n), using a heap for a priority queue.

(C) The maximum depth of any node in the Huffman-tree is O (log n).

(D) Given the Huffman-tree, the time to determine the encoding of the character set is O (n).

Q4. Which of the following classes of problems can be solved in polynomial time in a non-deterministic

model of computation?

(A) P (B) NP

(C) NP & NP-Complete (D) P, NP, and NP-Complete

Q5. Which of the following are true of an arbitrary problem in the class NP-Complete?

I. It is also in NP.

II. It is also in NP-Hard.

III. A certificate of the solution can be verified in polynomial time.

(A) III (B) I and II (C) I and III (D) I, II, and III

Q6. A problem is said to be NP-Complete

(A) If it is as hard as any problem in NP

(B) A non-polynomial time algorithm has been discovered

(C) A polynomial time algorithm can exist but needs a parallel computer

(D) There is Greedy solution to the problem

Q7. Which of the following problems is known to have a polynomial time solution?

(A) Longest simple path problem for a given graph

(B) The 3-colorability problem in graphs

(C) The Eulerian cycle in a graph

(D) The Hamiltonian Cycle in a graph

Q8. The distance matrix of a graph with vertices P, Q, R and S is given by

The shortest path from Q to S consists of edges

(A) QR and RS (B) QS (C) PQ and RS (D) there is no path

Q9. Which of the following basic algorithms can be used to most efficiently determine the presence of a cycle

in a given graph

(A) Minimum cost spanning tree algorithm (B) Breadth-first search algorithm

(C) Depth-first search algorithm (D) None of the above

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 40

For next two questions, consider the following algorithm:

function mystery(n)

1. r = 0

2. for i = 1 to n -1 do

3. for j = i + 1 to n do

4. for k = 1 to j do

5. r = r + 1

5. return r

The parameter n in the heading of this function definition is a positive integer.

Q10. The algorithm implementing the function mystery is an example of a:

(A) Greedy algorithm (B) Divide-and-conquer algorithm

(C) Dynamic programming algorithm (D) None of the above

Q11. Concerning the run time of the mystery algorithm:

(A) The worst-case run time is in u(2

n

) (B) The worst-case run time is in u (n

3

)

(C) The worst-case run time is in u (n) (D) The worst-case run time is in u (n log n)

Q12. There are two basic versions of the knapsack problem: the fractional and the integral. For the fractional

knapsack problem, select a method to solve it optimally and efficiently:

(A) A greedy algorithm

(B) An algorithm based on dynamic programming

(C) An algorithm based on backtracking search

(D) A divide-and-conquer algorithm

Q13. For the integral knapsack problem, which of the following is correct?

(A) Both a greedy algorithm and dynamic programming can be used to produce an optimal solution.

(B) Both dynamic programming and backtracking search can be used to produce an optimal solution.

(C) Both a greedy algorithm and backtracking search can be used to produce an optimal solution.

(D) Only dynamic programming can be used to produce an optimal solution.

Q14. Which of the following are true of the Bellman-Ford algorithm?

I. It solves the all-pairs shortest-path problem.

II. It permits negative-weight edges.

III. It has a time complexity of O(|V||E|), assuming an adjacency-list representation.

(A) I (B) I and II (C) I and III (D) II and III

For next two questions, consider the following algorithm:

Function minimum (A, p, r)

1. If rp 1

2. Then return min {A[p], A[r]}

3. Else x minimum (A, p, (p+r)/2 )

4. y minimum (A,1+ (p+r)/2, r)

5. return min {X,Y}

The first parameter in the heading of this function definition is an array A[1..n ] of integrs, where n1 and the

second and third parameters are p, re{1,,n}

Q15. When a call minimum (A, 1, n) is made, what is returned?

(A) The smallest integer in A[1..n], provided the entries in A are sorted in increasing order

(B) The smallest integer in A[1..n], provided the entries in A are sorted in decreasing order

(C) The smallest integer in A[1..n], regardless of the entries in A

(D) The smallest integer in A[1..n], provided the entries in A are all nonnegative

Q16. The algorithm implementing the function minimum is an example of a:

(A) Greedy algorithm (B) Divide-and-conquer algorithm

(C) Dynamic programming algorithm (D) Exhaustive-search algorithm

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 41

Q17. Starting at vertex f, which is a correct order for Dijkstras single-source shortest-path algorithm to add

edges to the shortest-path tree?

(A) ( f ;e) then (e;d) then (d;a) then (a;g) then (g;c) then (c;b)

(B) ( f ;e) then ( f ;c) then (c;b) then (b;a) then (a;g) then (a;d)

(C) ( f ;e) then ( f ;c) then (c;b) then ( f ;d) then ( f ;g) then (c;a)

(D) ( f ;e) then ( f ;c) then ( f ;d) then (c;a) then (c;b) then (c;g)

Q18. Consider the following weighted graph, Assume that we use Dijkstras algorithm to find shortest paths

form vertex s to the other vertices in the graph. In which order are the final distance lables u.d computed ?

(Or in other words, in which order are the shortest paths computed ?)

(A) s, 1, 4, 3, 2, 5 (B) s, 1, 4, 2, 3, 5 (C) s, 1, 4, 2, 5, 3 (D) s, 1, 4, 5, 2, 3

Q19. Consider the undirected graph shown in figure below, the shortest path will be reported by Dijkstras

shortest path algorithm?

(A) a, b, h, c, e, i, f, d, j, z, g (B) a, b, h, c, e, i, f, d, g, z, j

(C) a, b, h, e, c, i, f, d, g, z, j (D) None of the above

Q20. Which is true of a Hamiltonian tour?

(A) Determining whether there is a tour is NP-hard, but not NP-complete.

(B) Determining whether there is a tour is O(E) where E is the number of edges, but actually identifying

the tour is O(n

2

).

(C) Determining whether there is a tour is O(E) where E is the number of edges, and actually identifying

the tour is O(E).

(D) Determining whether there is a tour is NP-complete

Q21. What is true of the single source shortest path algorithm when there are negative edges present?

(A) There is no problem. The original algorithm handles this case without modification.

(B) There is no problem if you just add a constant to each edge before solving the problem in the

traditional manner. Just subtract the constant after you are finished.

(C) The algorithm will not be able to give any answer.

(D) In some cases, there is no such thing as a shortest path in the presence of negative edges.

Q22. In the knapsack problem you have items weights and values; you want best set of items for you knapsack

within a specified weight limit. The algorithm is optimal under a greedy heuristic if

(A) All weights are integer (B) All weights are positive

(C) A fraction of an item can be used (D) None of the above

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 42

For the next four questions consider given data:

Suppose we have seven decision problems A, B, C, D, E, F, and G such that A

P

B, A

p

E, B

P

C, C

P

A,

C

P

F, D

P

B, F

p

B, G

P

E, where X

P

Y means that there is a polynomial-time many-one reduction from X

to Y, i.e., X reduces to Y.

Q23. What is implied if we know that F is NPC and A is solvable in polynomial time?

(A) B is not solvable in polynomial time (B) If B is solvable in polynomial time P = NP.

(C) C is NPhard (D) D is NPhard

Q24. What is implied if we know that A is NPC and F is solvable in polynomial time?

(A) E is NP-hard (B) B is NPcomplete

(C) P = NP (D) E is P

Q25. What is implied if we know that C is NPC and E is solvable in polynomial time?

(A) D may be solvable in polynomial time (B) P = NP

(C) B is solvable in polynomial time (D) P = NP

Q26. What is implied if we know that G is NPC and D is solvable in polynomial time?

(A) E is solvable in polynomial time (B) P = NP

(C) A is NPhard (D) B may be solvable in polynomial time

For each of the problem below, determine whether they are in P, or not in P. In this test, we assume that NP= P

Q27. Given a graph with n vertices, determine whether it can be coloured using n colours.

(A) in P (B) in NP (C) not in P (D) not in NP

Q28. Given a graph with n vertices, determine whether it can be coloured using n + 1 colours.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q29. Given a graph and an integer k, determine whether it can be coloured using k + 1 colours.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q30. Given a graph, determine whether it has a clique of size 3.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q31. Given a graph, determine whether it has a clique of size 5.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q32. Given a graph, determine whether it has a clique of size n + 1.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q33. Given a set A of n integers and an integer k, determine whether there are 3 elements from A such that their

sum is k.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q34. Given a set A of n integers, an integer k, and another integer m, determine whether there are m elements

from A such that their sum is k.

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q35. Given a set A of n integers, determine whether they can be divided into 3 groups, such that the sums of the

3 groups are the same. (i.e. similar to the partition problem, instead of dividing into 2, this problem divide

the input into 3 groups).

(A) in P (B) in NP (C) Not in P (D) Not in NP

Q36. Consider the following instance of the fractional knapsack problem:

n = 3, m = 20, (P

1

, P

2

, P

3

) = (25, 24, 15) and (W

1

, W

2

, W

3

) = (18, 15, 10), What is The maximum profit?

(A) 24.25 (B) 28.2 (C) 31.5 (D) None of the above

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 43

Q37. Consider the following instances of the 0-1 knapsack problem: n = 3, m = 20, (P

1

, P

2

, P

3

) = (25, 24, 15)

and (W

1

, W

2

, W

3

) = (18, 15, 10), what is the maximum profit?

(A) 25 (B) 24 (C) 15 (D) None of the above

Q38. What is the maximum profit generated by the function job sequencing with deadlines when n = 7,

(P

1

, P

2

P

3

., P

7

) = (3, 5, 20, 18, 1, 6, 30) and (d

1

, d

2

,.., d

7

) = (1, 3, 4, 3, 2, 1, 2)

(A) 71 (B) 74 (C) 78 (D) None of the above

Q39. An optimal binary merge pattern for ten files whose lengths are 28, 32, 12, 5, 84, 53, 91, 35, 3 and 11.

Find an optimal cost of merging.

(A) 385 (B) 686 (C) 1295 (D) None of the above

Q40. We want to store files of lengths (in MB) {12, 34, 56, 73, 24, 11, 34, 56, 78, 91, 98, 45) on three tapes.

How should we store them on the three tapes so that the mean retrieval time is minimized?

(A) 108.5 (B) 97 (C) 96.5 (D) None of the above

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 44

Practice Test 1

Q1. Consider the following algorithm.

Algorithm Sort (A, n)

Input: Array A containing n different integer values.

Output: Array A sorted in increasing order of value.

for i = n 1 down to 0 do {

m = i

(*)

t = A[m]; A[m] = A[i]; A[i] = t; // Swap A[i] and A[m]

}

return A

}

Which of the following instructions must be inserted at the point marked (*) so that the algorithm

correctly sorts the values stored in A in increasing order of value?

(A) for j = 0 to n 1 do { if A[j] > A[m] then m = j

(B) for j = i + 1 to n 1 do { if A[j] > A[m] then m = j

(C) for j = 0 to i 1 do { if A[j] > A[m] then m = j

(D) for j = i + 1 to n 1 do { if A[j] < A[m] then m = j

Q2. Consider the following pairs of functions f(n), g(n). For which pair the functions are such that f(n) is

O(g(n)) and g(n) is not O(f(n))?

(A) f(n) = n

2

, g(n) = n log n (B) f(n) = log n, g(n) = log(n

2

)

(C) f(n) = n, g(n) = 1000n (D) f(n) = log(n

3

), g(n) = n

Q3. The lower bound for all comparison-based sorting algorithm cases:

(A) Five elements cannot be sorted with six comparisons.

(B) Five elements can be sorted with eight comparisons.

(C) N elements can be sorted with nlog

2

n comparisons.

(D) Radix sort must use at least nlog

2

n comparisons, even if they are disguised as bit operation

Q4.

(A) (n) is in (g(n)) and isnt in (g(n)) (B) (n) isnt in (g(n)) and isnt in (g(n))

(C) (n) isnt in (g(n)) and is in (g(n)) (D) (n) is in (g(n)) and is in (g(n))

Q5. The function grows ______ when input n is doubled in size:

(A) 2 times (B) 4 times (C) by 1 (D) by constant factor

Q6. To calculate tight asymptotic (u notation) for T (n) = T (10n/9) + n, we use,

(A) Master case 1 (B) Mastercase 2 (C) Master case 3 (D) Master cant be applied

Q7. Give a tight asymptotic upper bound (O notation) on the solution to the following recurrences

T(n) = T(n-1) + 1/n

(A) O(logn) (B) O (nlogn) (C) log(n))

2

(D) None of the above

Q8. Insert the numbers in the sequence "8, 5, 1, 4, 2, 10, 12, 7, 3" into a min-heap. If the root is said to be at

level 1, the root's children at level 2, and so on, what is the level in which 8 occurs in the heap?

(A) 1 (B) 2 (C) 3 (D) 4

Q9. What is the run-time of the following segment of code in terms of n?

int i = n, j, k, total = 0;

while (i > 0) {

for (j = 0; j < i; j++)

for (k = 0; k < i; k++)

total++;

i = i/4;

}

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 45

(A) n

2

(B) n

3

(C) n (D) nlogn

Q10. What is the solution to the following recurrence: T(n) = T (n 1) + lg n?

(A) (n

2

) (B) (n) (C) (lgn) (D) (n lgn)

Q11. Which of the following is true?

(A) n

= (n

) (B) n

= (n

) (C) n

= o (n

) (D) n

+ o (n

) = o (n

)

Q12. Consider the following function:

int f (int n) {

if (n= =0) return 0;

else if (n= =1) return 1;

else {

int val = 3*f (n-1);

val = val - 2*f (n-2);

val + = 1;

return val;

}}

Let f(n) be the value returned by the function f when given input n. The recurrence relation for f(n) will be

(A) f(n) = 3f(n - 1) - 2f(n - 2) + 1 (B) f(n) = 3 f(n - 1) + 2f(n - 2) +1

(C) f (n) = f(n - 1) + f(n - 2) + 1 (D) f(n) = f(n - 1) - f(n - 2) +1

Q13. Which of the following statements is(are) TRUE?

(i) n = O (n /log n) (ii) n! = O(n

n

)

(A) Only (i) (B) Only (ii) (C) Both are TRUE (D) Neither of them are TRUE

Q14. If f (n) is in u(n

E

), then:

(A) f (n) is in O(n

E

) but not in O(n

E

) (B) f (n) is in O (n

E

) but not in O(n

E

)

(C) f (n) is in O(n

E

) and also in O (n

E

) (D) All of the above are possible, depending on f and E

Q15. If f (n) is in u(g(n)) and exists, then some possible values for this limit are:

(A) 10 and 100 (B) 0 and 100 (C) 10 and (D) 0, 1 and

Q16. What is the running time of Quick-Sort on n identical elements?

(A) O(1) (B) O (n) (C) O (n log n) (D) u (n

2

)

Q17. Approximately how much iteration will binary search need to find a value in a list of 512 items?

(A) 512 (B) 256 (C) 9 (D) 3

Q18. The recursive Fibonacci function is inefficient because

(A) It does many repeated computations

(B) Recursion is inherently inefficient compared to iteration

(C) Calculating Fibonacci numbers is intractable

(D)None of the above

Consider the following code fragment

int mystery(int a, int b) {

if (b = = 1)

return a;

else

return a + mystery(a, b-1);

}

Q19. What is the value of mystery (2, 3)?

(A) 2 (B) 4 (C) 6 (D) infinite recursion

Q20. Still referring to the code fragment of the previous question, what is the value of mystery (2,0)?

(A) 2 (B) 4 (C) 0 (D) infinite recursion

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 46

For the next two questions consider given program

int t(int n) {

if (n = = 1 || n = = 2)

return 2*n;

else

return t(n-l) t(n-2);

}

Q21. What will be returned by t(5)?

(A) 4 (B) 2 (C) -2 (D) -4

Q22. For the method call t(6), how many calls to t will be made, including the original call?

(A) 6 (B) 7 (C) 11 (D) 15

Q23. Consider method foo;

foo(int x){

if (x = = 1 || x = = 3)

return x;

else

return x * foo(x - l); }

Assuming no possibility of integer overflow, what will be the value of z after execution of the following

statement?

int z = foo(foo(3) + foo(4));

(A) (15!)/(2!) or 15*14*13*...*4*3 (B) 3! + 4! or 3*2 + 4*3*2 or 6 + 24 or 30

(C) (7!)! or 5040! (D) 15

Q24. Suppose that you are considering running two algorithms under the same conditions and on the same

computer, on problems of size n. Furthermore, suppose that Algorithm A has a worst-case runtime

complexity proportional to log n while algorithm B has a worst-case runtime complexity proportional to n.

For large enough values of n,

(A) Algorithm A is always faster than algorithm B, in the worst-case scenario.

(B) Algorithm A is always slower than algorithm B, in the worst-case scenario.

(C) Algorithm A may be faster or slower than algorithm B, in the worst-case scenario, depending on those

pesky constants which we typically ignore when we use O-notation.

(D) We cannot conclude anything about this situation when n is large; we should be looking at the case

when n is small.

Q25. Arrange the following functions in increasing order:

4n+n

logn

, logn+n

0.02

, n

3

, 2

n

, n!, 20n

2

(A) logn+n

0.02

, 20n

2

, n

3

, 4n+n

log n

, 2

n

, n!

(B) logn+n

0.02

, 20n

2

, n

3

, 2

n

, 4n+n

log n

, n!

(C) logn+n

0.02

, 20n

2

, n

3

, 2

n

, n!, 4n+n

log n

(D) logn+n

0.02

, 4n+n

log n

, 20n

2

, n

3

, 2

n

, n!

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 47

Practice Test 2

Q1. Suppose T1(N) = O(f(N)) and T2(N) = O(f(N)). Which of the following is true?

(A) T1(N) + T2(N) = O(f(N)) (B) T1(N) - T2(N) = o(f(N))

(C) T1(N) / T2(N)= O(1) (D) T1(N) = O(T2(N))

Q2. For the following program fragments running time complexity will be

sum = 0;

for (i = 1; i < n; i++)

for (j = 1; j < i*i; j++)

if (j % i == 0)

for (k = 0; k < j; k++)

sum++;

(A) n

3

(B) n

2

(C) n

4

(D) None of the above

Q3. Consider methods f1 and f2

int fl(int a, int b) {

if (a = = b)

return b;

else

return a + f2(a - l, b);

}

int f2(int p, int q) {

if (p < q)

return p + q;

else

return p + fl(p - 2, q);

}

What value will be returned by a call to f1(5, 3)?

(A) 6 (B) 7 (C) 12 (D) 15

The following two questions are based on the binary search tree shown below, using the standard binary search

tree deletion algorithm.

Q4. After deleting 23 with the standard binary-search-tree algorithm, what node is at the root?

(A) 11 (B) 25 (C) 27 (D) 14

Q5. After deleting 23 with the standard binary-search-tree algorithm, what parent # child node pair does not

occur in the tree?

(A) 25 # 27 (B) 27 # 11 (C) 11 # 7 (D) 7 # 9

Q6. Given the code fragment

struct NodeType {

int data;

NodeType* next;

};

NodeType* p ,q;

p = new NodeType;

pdata = 12;

pnext = NULL;

q = new NodeType;

qdata = 5;

qnext = p

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 48

Which of the following expressions has the value NULL?

(A) p (B) q (C) qnext (D) qnextnext

Q7. Given the declarations

struct ListNode {

float volume;

ListNode* next;

};

ListNode* headPtr ,* ptr;

float searchVal;

Assume that headPtr is the external pointer to a linked list of many nodes. Which code segment below

searches the list for the first occurrence of searchVal, leaving ptr pointing to the node where it was found?

(Assume searchVal is definitely in the list.)

(A) ptr = headPtr; (B) ptr = headPtr;

while (ptr.volume != searchVal) while (ptrvolume != searchVal)

ptr = ptr.next; ptr++;

(C) ptr = headPtr; (D) ptr = headPtrvolume;

while (ptrvolume != searchVal) while (ptr != searchVal)

ptr = ptrnext; ptr = ptrnext;

Q8. What is the minimum number of elements in a heap of height 4 if root is at height 0?

(A) 31 (B) 16 (C) 4! (D) 6

Q9. Consider generating binary search trees using a given set of numbers in the given order. The tree is to be

constructed by inserting the numbers into the current partial tree such that at any node, the following

condition is satisfied: value of left child < value of node < value of right child. Which of the following

sequence of numbers will result in a tree that is strictly binary, i.e., every node has either two children or

no children?

(A) 4 5 2 1 3 (B) 4 6 5 7 3 2 (C) 4 6 2 1 3 5 (D) 4 5 3 2 6

Q10. Which of the following represents the minimum order of time required to interchange the mth and nth

elements of a singly linked list? Assume m and n are very large so that the time for pointer manipulations

may be ignored compared to the traversal time.

(A) max(m,n) (B) min(m,n) (C) m+n (D) m+min(m,n)

Q11. Construct a min-heap from the following sequence of integer elements.

120 140 40 50 80 70 60 90 20 100

After deleting the root element from the heap, what will be the post order traversal of the heap?

(A) 140 100 90 80 50 120 70 60 40 (B) 140 100 90 80 120 70 50 60 40

(C) 140 100 80 90 120 70 50 60 40 (D) 140 90 100 50 80 40 120 60 70

Q12. We are told that the integers between 1 and 1000 are arranged in a binary search tree below are four lists

of vertices encountered as we search for the number 363. Which list cannot be produced by this search

through a binary search tree?

(A) 924, 220, 911, 244, 898, 248, 363 (B) 2, 252, 401, 398, 330, 344, 397, 363

(C) 925, 202, 911, 240, 912, 245, 363 (D) 2, 399, 387, 219, 266, 382, 381, 278, 363

Q13. A stake machine pushes operands on a stack and evaluates binary operators by a pcs (i.e pop / compute /

store) where the top two operands are popped computation is performed and the result is pushed onto

stack. Evaluation of an expression (x* y) + (u* ) by Reverse Polish notation in a stack machine needs

(A) 4 push and 3 pcs instructions (B) 6 push and 1 pcs instructions

(C) 4 push and 1 pcs instructions (D) 5 push and 2 pcs instructions

Q14. The five items: A, B, C, D and E are pushed in a stack,one after the other starting from A. The stack is

popped four times and each element is inserted in a queue. Then two elements are deleted from the queue

and pushed back on the stack. Now one item is popped from the stack.

The popped item is.

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 49

(A) A (B) B (C) C (D) D

Q15. What is the output of the following program?

struct mylist{

int data;

mylist* next;

}

int main() {

mylist* head;

myListk cur;

myListk previous = NULL;

for(int i = 0; i < 4; i++) {

head = new mylist;

headdata=0;

headnext=previous;

for(cur = previous; cur! = NULL; cur = curnext)

headdata+=1+2*curdata

previous = head;

}

while (previous != NULL) {

cout<<previousdata ;

previous = previousnext;

delete cur;

return 0;

}}

(A) 13 4 1 0 (B) 0 0 0 0 (C) 13 4 0 0 (D) None of the above

Q16. What result is achieved through the following code snippet?

int initialize(struct NODE **circle) {

if ((*circle = (struct NODE *)malloc(sizeof(NODE))) == NULL)

return 0;

(*circle) next = NULL;

(*circle) previous = NULL;

return 1;

}

(A) The node circle is assigned to NULL

(B) The size of the node circle is assigned to NULL

(C) Both the pointers next and previous are assigned to NULL

(D) None of these options

Q17. The five items: A, B, C, D and E are pushed in a stack,one after the other starting from A. The stack is

popped four times and each element is inserted in a queue. Then two elements are deleted from the queue

and pushed back on the stack. Now one item is popped from the stack.

The popped item is.

(A) A (B) B (C) C (D) D

Q18. If the inorder traversal of the binary tree T is

A D B G C F E

and each node of T has either 0 or 2 children, which of the following nodes is NOT a leaf of that tree?

(A) A (B) B (C) C (D) D

Q19. Suppose that Table is an array of records used to implement a linked list, as shown below.

DATA STRUCTURE & ALGORITHMS

www.gaterevolution. Page 50

Each component of Table contains an information field and a pointer to the next element in a linked list. (The pointer

values are indices of Table, and 0 signifies the end of a list.) What is the list of characters starting with Table[5]?

(A) da (B) djaba (C) daadjaba (D) dajabadj

Q20. The alpha-mem, a new data structure, supports two operations.

The insert operation allows "words" to be stored in the alpha-mem.

The remove operation causes the "word" in the alpha-mem which is first alphabetically to be printed and

removed from the alpha-mem.

Which of the following is true of an alpha-mem?

(A) If words are inserted in alphabetical order and all words are inserted before any are removed, then it

works like a stack

(B) If words are inserted in alphabetical order, then it works like a stack whether or not all inserts precede

any removes.

(C) If words are inserted in reverse alphabetical order, then it works like a queue whether or not all inserts

precede any removes.

(D) If words are inserted in alphabetical order, then it works like a queue whether or not all inserts

precede any removes.

Q21. In a min-heap with N nodes what is the cost of removing the minimum element in the heap?

(A) O(1) (B) O(log N) (C) O(N) (D) O(N log N)

- Modern GPUHochgeladen vonx2y2z2rm
- An empirical comparison of the runtime of five sorting algorithmsHochgeladen vonlimitedmage
- Shell SortHochgeladen vonDjJethro Bagcat Fernandez
- 10 Sorting 1Hochgeladen von@786
- Review on Sorting Algorithms A Comparative StudyHochgeladen vonAI Coordinator - CSC Journals
- CSC112-COURSE-HANDBOOK+Spring+2015--+Sana+RizwanHochgeladen vonAliImran
- Ch13Hochgeladen vonCarter Lyon
- Chapter 10 - SortingHochgeladen vonNguyễn Thanh Linh
- BucketSort PaperHochgeladen vonbhumka
- COS211 SortingHochgeladen vonFrancois Impey
- Chapter 08Hochgeladen vonKevinFitzgibbons
- External SortingHochgeladen vonmanuelq9
- 05Hochgeladen vonIbrahim Hawari
- lecture09_4on1Hochgeladen vonkoiuy12
- 02 Sort HashHochgeladen vonBrian
- Sample CopyHochgeladen vonbachayadav
- Unit I ppt.pptxHochgeladen vonJayakumar Loganathan
- Sorting AlgorithmHochgeladen vonwilliam walters
- Sorting-Algorithm.pdfHochgeladen vonИРадојичић
- 1-s2.0-S1877050916315381-mainHochgeladen vonjosepepe092
- lecture1.pdfHochgeladen vonAnonymous pow3LFS
- Theory of Computation Aqa OcrHochgeladen vonHenri
- External SortingHochgeladen vonShouvikBhattacharya
- chap08Hochgeladen vonapi-400294283
- All SortingHochgeladen vonkrutarth Ganatra
- Algorithms TopicsHochgeladen vonAnonymous 1Xtj2DjM
- Goodrich_6e_Ch12_RadixSort-handouts.pdfHochgeladen vonDAWIT SEGED
- quick sort.pdfHochgeladen vonpratik Yadav
- mcq for algorithHochgeladen vonmoomin
- Bubble SortingHochgeladen vonBishwajit Banerjee

- java programmingHochgeladen vonMelody Mwhengwere
- Hindi SmsHochgeladen vonFanindra Prajapati
- local variable’s name is same as member’s nameHochgeladen vonFanindra Prajapati
- OFFICE SHORTCUTS KEY.txtHochgeladen vonFanindra Prajapati
- Project SynopsisHochgeladen vondhootshab
- Linear Algebra - Jim HefferonHochgeladen vonSteven Gerrard
- network-notes.pdfHochgeladen vonNasarMahmood

- Complement of a SetHochgeladen vonHyung Bae
- Andre Derain EssayHochgeladen vonogangurel
- Chapter 2Hochgeladen vonSPAM
- [Erasmus] QM - 3 - 6 - Infrastructure Management Assurance Standardization Metrology ILAC AccreditationHochgeladen vonHussein Hanoura
- unit4Hochgeladen vondon6080
- BL652Hochgeladen vonPatrick Manzanza
- assignment1-2014Hochgeladen vonMoin Ahmed
- Pwc Supply Chain FlexibilityHochgeladen vonAngela Li
- Unit - 42 (1)Hochgeladen vonUmair Gaba
- Norway PptHochgeladen vonShubashPoojari
- Foucault - The Order of Discourse - 1984Hochgeladen vonontic65
- Copy of Blank Form Per Grading PeriodHochgeladen vonRobert Frio
- Olympiad Books from HBCSE — Olympiads HBCSEHochgeladen vonBodhayan Prasad
- C# Interview Questions and AnswersHochgeladen vonAkhilendu Shukla
- Katz Overheads HRMG200 Spring, 2009Hochgeladen vonmailforjon
- An Analysis of Wind Resource Uncertainty in Energy Production EstimatesHochgeladen vond_u_i_l_i_o
- Project3_JoseZapataB00092132Hochgeladen vonJosé Zapata Neto
- Simhasan ChakraHochgeladen vonNemikumar Gandhi
- An Improved De-Duplication Technique for Small Files in HadoopHochgeladen vonAnonymous kw8Yrp0R5r
- Langmuir Isotherm Explains Maximum Foamability.2016Hochgeladen vonMiguel Angel Sandoval
- unit plan- happinessHochgeladen vonapi-379569263
- Postflaviana - Nuclear FusionHochgeladen vonmika2k01
- Love in the Time of CapitalHochgeladen vonzizek
- Critical Analysis and Action PlanHochgeladen vonkstephens73
- Intro to Project ManagementHochgeladen vonswabi826462
- Module 4 Financial Functions, Data Tables, And Amortization SchedulesHochgeladen vonNisha Prudhomme
- Final - Full Paper Template for Author - Iconic 2018Hochgeladen vonArdhi Shihab
- Tamsin Meaney, Ola Helenius, Maria L. Johansson, Troels Lange, Anna Wernberg (Eds.) - Mathematics Education in the Early Years_ Results From the POEM2 Conference, 2014 (2016, Springer International Publishing) (1)Hochgeladen vonClaudiaElizabethCornejoMorales
- Alias Studiotools Sketching v13Hochgeladen vonRodrigo Wanzeler
- [Presentation] The Megaphone EffectHochgeladen vonDyasanti Vidya S.

## Viel mehr als nur Dokumente.

Entdecken, was Scribd alles zu bieten hat, inklusive Bücher und Hörbücher von großen Verlagen.

Jederzeit kündbar.