Algorithm Quiz
Algorithm Quiz
Algorithm Quiz
Sorting
1. How many passes does an insertion sort algorithm consist of?
a) N
b) N-1
c) N+1
d) N2
Answer:b) N-1
6. What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
Answer:b) 5
7. For the following question, how will the array elements look like after second pass?
34, 8, 64, 51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21
Answer:d) 8, 34, 64, 51, 32, 21
10. Which of the following options contain the correct feature of an insertion sort algorithm?
a) anti-adaptive
b) dependable
c) stable, not in-place
d) stable, adaptive
Answer:d) stable, adaptive
11. Which of the following sorting algorithms is the fastest for sorting small arrays?
a) Quick sort
b) Insertion sort
c) Shell sort
d) Heap sort
Answer:b) Insertion sort
12. For the best case input, the running time of an insertion sort algorithm is?
a) Linear
b) Binary
c) Quadratic
d) Depends on the input
Answer:a) Linear
13. Which of the following examples represent the worst case input for an insertion sort?
a) array in sorted order
b) array sorted in reverse order
c) normal unsorted array
d) large array
Answer:b) array sorted in reverse order
15. Which of the following sorting algorithm is best suited if the elements are already
sorted?
a) Heap Sort
b) Quick Sort
c) Insertion Sort
d) Merge Sort
Answer:c) Insertion Sort
16. Which of the following is good for sorting arrays having less than 100 elements?
a) Quick Sort
b) Selection Sort
c) Merge Sort
d) Insertion Sort
Answer:d) Insertion Sort
19. In the following scenarios, when will you use selection sort?
a) The input is already sorted
b) A large file has to be sorted
c) Large values need to be sorted with small keys
d) Small values need to be sorted with large keys
Answer:c) Large values need to be sorted with small keys
21. What is the advantage of selection sort over other sorting techniques?
a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique
Answer:a) It requires no additional storage space
24. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection
sort respectively are __________
a) 5 and 4
b) 4 and 5
c) 2 and 4
d) 2 and 5
Answer:a) 5 and 4
30. Which of the following is not an advantage of optimised bubble sort over other sorting
techniques in case of sorted elements?
a) It is faster
b) Consumes less memory
c) Detects whether the input is already sorted
d) Consumes less time
Answer:c) Detects whether the input is already sorted
31. Merge sort uses which of the following technique to implement sorting?
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming
Answer:c) divide and conquer
32. Which of the following method is used for sorting in merge sort?
a) merging
b) partitioning
c) selection
d) exchanging
Answer:a) merging
37. Which of the following stable sorting algorithm takes the least time when applied to an
almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort
Answer:d) Merge sort
38. Merge sort uses which of the following algorithm to implement sorting?
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming
Answer:c) divide and conquer
39. Merge sort uses which of the following method to implement sorting?
a) merging
b) partitioning
c) selection
d) exchanging
Answer:a) merging
43. How many sub arrays does the quick sort algorithm divide the entire array into?
a) one
b) two
c) three
d) four
Answer:b) two
46. Which one of the following sorting algorithm is best suited to sort an array of 1 million
elements?
a) Bubble sort
b) Insertion sort
c) Merge sort
d) Quick sort
Answer: d) Quick sort
51. How many arrays are required to perform deletion operation in a heap?
a) 1
b) 2
c) 3
d) 4
Answer:b) 2
57. Which of the following non-comparison sort can also be considered as a comparison
based sort?
a) counting sort
b) MSD radix sot
c) bucket sort
d) pigeonhole sort
Answer:c) bucket sort
59. Which of the following don’t affect the time complexity of bucket sort?
a) algorithm implemented for sorting individual buckets
b) number of buckets used
c) distribution of input
d) input values
Answer:d) input values
.
60. Bucket sort is most efficient in the case when __________
a) the input is non uniformly distributed
b) the input is uniformly distributed
c) the input is randomly distributed
d) the input range is large
Answer:b) the input is uniformly distributed
64. Which of the following is true for the LSD radix sort?
a) works best for variable length strings
b) accesses memory randomly
c) inner loop has less instructions
d) sorts the keys in left-to-right order
Answer:b) accesses memory randomly
66. Which of the following combines qualities of MSD radix sort and LSD radix sort?
a) in-place MSD radix sort
b) stable MSD radix sot
c) 3 way radix quick sort
d) forward radix sort
Answer:d) forward radix sort
68. Which of the following is not true about MSD radix sort?
a) its processing starts from the most significant digit
b) it is not a stable sort
c) it is an in place sorting algorithm
d) it is non comparison based sort
Answer:c) it is an in place sorting algorithm
69. Which of the following statement is not a stable sorting algorithm?
a) LSD radix sort
b) MSD radix sort
c) Counting sort
d) Pigeonhole sort
Answer:b) MSD radix sort
Search
1. Where is linear searching used?
a) When the list has only a few elements
b) When performing a single search in an unordered list
c) Used all the time
d) When the list has only a few elements and When performing a single search in an
unordered list
Answer:d) When the list has only a few elements and When performing a single search in
an unordered list
4. What is the best case and worst case complexity of ordered linear search?
a) O(nlogn), O(logn)
b) O(logn), O(nlogn)
c) O(n), O(1)
d) O(1), O(n)
Answer:d) O(1), O(n)
7. Is the space consumed by the linear search(recursive) and linear search(iterative) same?
a) No, recursive algorithm consumes more space
b) No, recursive algorithm consumes less space
c) Yes
d) Nothing can be said
Answer:a) No, recursive algorithm consumes more space
10. Can linear search recursive algorithm and binary search recursive algorithm be
performed on an unordered list?
a) Binary search can’t be used
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used
Answer:a) Binary search can’t be used
11. What is the recurrence relation for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
Answer:c) T(n-1)+c
15. What is the average case time complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer:b) O(logn)
19. In which of the cases uniform binary search fails compared to binary search?
a) A table lookup is generally faster than an addition and a shift
b) Many searches will be performed on the same array
c) Many searches will be performed on several arrays of the same length
d) Complexity of code
Answer:d) Complexity of code
20. Given delta[4] is a global array and number of elements in the sorted array is 10, what
are the values in the delta array?
a) 4, 3, 1, 0
b) 5, 3, 1, 0
c) 4, 2, 1, 1
d) 5, 2, 1, 1
Answer: b) 5, 3, 1, 0
21. What is the time complexity of uniform binary search?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer:b) O(logn)
24. Depth First Search is equivalent to which of the traversal in the Binary Trees?
a) Pre-order Traversal
b) Post-order Traversal
c) Level-order Traversal
d) In-order Traversal
Answer: a) Pre-order Traversal
.
25. Time Complexity of DFS is? (V – number of vertices, E – number of edges)
a) O(V + E)
b) O(V)
c) O(E)
d) O(V*E)
Answer: a) O(V + E)
26. The Data structure used in standard implementation of Breadth First Search is?
a) Stack
b) Queue
c) Linked List
d) Tree
Answer: a) Stack
27. The Depth First Search traversal of a graph will result into?
a) Linked List
b) Tree
c) Graph with back edges
d) Array
Answer: b) Tree
28. A person wants to visit some places. He starts from a vertex and then wants to visit
every vertex till it finishes from one vertex, backtracks and then explore other vertex from
same vertex. What algorithm he should use?
a) Depth First Search
b) Breadth First Search
c) Trim’s algorithm
d) Kruskal’s Algorithm
Answer: a) Depth First Search
31. Regarding implementation of Depth First Search using stacks, what is the maximum
distance between two nodes present in the stack? (considering each edge length 1)
a) Can be anything
b) 0
c) At most 1
d) Insufficient Information
Answer: a) Can be anything
33. Breadth First Search is equivalent to which of the traversal in the Binary Trees?
a) Pre-order Traversal
b) Post-order Traversal
c) Level-order Traversal
d) In-order Traversal
Answer: c) Level-order Traversal
34. Time Complexity of Breadth First Search is? (V – number of vertices, E – number of
edges)
a) O(V + E)
b) O(V)
c) O(E)
d) O(V*E)
Answer: a) O(V + E)
35. The Data structure used in standard implementation of Breadth First Search is?
a) Stack
b) Queue
c) Linked List
d) Tree
Answer: b) Queue
36. The Breadth First Search traversal of a graph will result into?
a) Linked List
b) Tree
c) Graph with back edges
d) Arrays
Answer: b) Tree
37. A person wants to visit some places. He starts from a vertex and then wants to visit
every place connected to this vertex and so on. What algorithm he should use?
a) Depth First Search
b) Breadth First Search
c) Trim’s algorithm
d) Kruskal’s algorithm
Answer: b) Breadth First Search
42. Which of the following is not a branch and bound strategy to generate branches?
a) LIFO branch and bound
b) FIFO branch and bound
c) Lowest cost branch and bound
d) Highest cost branch and bound
Answer: d) Highest cost branch and bound
43. Which data structure is used for implementing a LIFO branch and bound strategy?
a) stack
b) queue
c) array
d) linked list
Answer: a) stack
44. Which data structure is used for implementing a FIFO branch and bound strategy?
a) stack
b) queue
c) array
d) linked list
Answer: b) queue
45. Which data structure is most suitable for implementing best first branch and bound
strategy?
a) stack
b) queue
c) priority queue
d) linked list
Answer: c) priority queue
46. Which of the following branch and bound strategy leads to breadth first search?
a) LIFO branch and bound
b) FIFO branch and bound
c) Lowest cost branch and bound
d) Highest cost branch and bound
Answer: b) FIFO branch and bound
47. Which of the following branch and bound strategy leads to depth first search?
a) LIFO branch and bound
b) FIFO branch and bound
c) Lowest cost branch and bound
d) Highest cost branch and bound
Answer: a) LIFO branch and bound
49. Which of the following can traverse the state space tree only in DFS manner?
a) branch and bound
b) dynamic programming
c) greedy algorithm
d) backtracking
Answer: d) backtracking
50. Which of the following is false in the case of a spanning tree of a graph G?
a) It is tree that spans G
b) It is a subgraph of the G
c) It includes every vertex of the G
d) It can be either cyclic or acyclic
Answer:d) It can be either cyclic or acyclic
51. Consider a complete graph G with 4 vertices. The graph G has ____ spanning trees.
a) 15
b) 8
c) 16
d) 13
Answer:c) 16
53. Consider the graph M with 3 vertices. Its adjacency matrix is shown below. Which of the
following is true?
a)Graph M has no minimum spanning tree
b) Graph M has a unique minimum spanning trees of cost 2
c) Graph M has 3 distinct minimum spanning trees, each of cost 2
d) Graph M has 3 spanning trees of different costs
Answer: c) Graph M has 3 distinct minimum spanning trees, each of cost 2
54. Consider a undirected graph G with vertices { A, B, C, D, E}. In graph G, every edge has
distinct weight. Edge CD is edge with minimum weight and edge AB is edge with maximum
weight. Then, which of the following is false?
a) Every minimum spanning tree of G must contain CD
b) If AB is in a minimum spanning tree, then its removal must disconnect G
c) No minimum spanning tree contains AB
d) G has a unique minimum spanning tree
Answer:c) No minimum spanning tree contains AB
55. Consider the graph shown below. Which of the following are the edges in the MST of
the given graph?
a) (a-c)(c-d)(d-b)(d-b)
b) (c-a)(a-d)(d-b)(d-e)
c) (a-d)(d-c)(d-b)(d-e)
d) (c-a)(a-d)(d-c)(d-b)(d-e)
Answer:c) (a-d)(d-c)(d-b)(d-e)
56. Which of the following is not the algorithm to find the minimum spanning tree of the
given graph?
a) Boruvka’s algorithm
b) Prim’s algorithm
c) Kruskal’s algorithm
d) Bellman–Ford algorithm
Answer:d) Bellman–Ford algorithm
Algorithm
1. Kruskal’s algorithm is used to ______
a) find minimum spanning tree
b) find single source shortest path
c) find all pair shortest path algorithm
d) traverse the graph
Answer:a) find minimum spanning tree
What is the weight of the minimum spanning tree using the Kruskal’s algorithm?
a) 24
b) 23
c) 15
d) 19
Answer:d) 19
5. Consider the following graph. Using Kruskal’s algorithm, which edge will be selected
first?
a) GF
b) DE
c) BE
d) BG
Answer:c) BE
6. Which of the following edges form minimum spanning tree on the graph using kruskals
algorithm?
a) (B-E)(G-E)(E-F)(D-F)
b) (B-E)(G-E)(E-F)(B-G)(D-F)
c) (B-E)(G-E)(E-F)(D-E)
d) (B-E)(G-E)(E-F)(D-F)(D-G)
Answer:a) (B-E)(G-E)(E-F)(D-F)
What is the weight of the minimum spanning tree using the Prim’s algorithm,starting from
vertex a?
a) 23
b) 28
c) 27
d) 11
Answer:c) 27
12. Worst case is the worst case time complexity of Prim’s algorithm if adjacency matrix is
used?
a) O(log V)
b) O(V2)
c) O(E2)
d) O(V log E)
Answer:b) O(V2)
Which of the following edges form the MST of the given graph using Prim’a algorithm,
starting from vertex 4.
a) (4-3)(5-3)(2-3)(1-2)
b) (4-3)(3-5)(5-1)(1-2)
c) (4-3)(3-5)(5-2)(1-5)
d) (4-3)(3-2)(2-1)(1-5)
Answer:d) (4-3)(3-2)(2-1)(1-5)
16. Prim’s algorithm can be efficiently implemented using _____ for graphs with greater
density.
a) d-ary heap
b) linear search
c) fibonacci heap
d) binary search
Answer:a) d-ary heap
19. Which of the following is the most commonly used data structure for implementing
Dijkstra’s Algorithm?
a) Max priority queue
b) Stack
c) Circular queue
d) Min priority queue
Answer: d) Min priority queue
23. How many times the insert and extract min operations are invoked per vertex?
a) 1
b) 2
c) 3
d) 0
Answer: a) 1
24. The maximum number of times the decrease key operation performed in Dijkstra’s
algorithm will be equal to ___________
a) Total number of vertices
b) Total number of edges
c) Number of vertices – 1
d) Number of edges – 1
Answer: b) Total number of edges
25. What is running time of Dijkstra’s algorithm using Binary min- heap method?
a) O(V)
b) O(VlogV)
c) O(E)
d) O(ElogV)
Answer: d) O(ElogV)
a) a-b-e
b) a-c-e
c) a-c-d-e
d) a-c-d-b-e
Answer: b) a-c-e
31. How many solution/solutions are available for a graph having negative weight cycle?
a) One solution
b) Two solutions
c) No solution
d) Infinite solutions
Answer: c) No solution
33. How many times the for loop in the Bellmann Ford Algorithm gets executed?
a) V times
b) V-1
c) E
d) E-1
Answer: b) V-1
37. Consider the following graph. What is the minimum cost to travel from node A to node
C?
a) 5
b) 2
c) 1
d) 3
Answer: b) 2
38. In the given graph, identify the path that has minimum cost to travel from node a to node
f.
a) a-b-c-f
b) a-d-e-f
c) a-d-b-c-f
d) a-d-b-c-e-f
Answer: d) a-d-b-c-e-f
48. Who proposed the modern formulation of Floyd-Warshall Algorithm as three nested
loops?
a) Robert Floyd
b) Stephen Warshall
c) Bernard Roy
d) Peter Ingerman
Answer: d) Peter Ingerman
49. What happens when the value of k is 0 in the Floyd Warshall Algorithm?
a) 1 intermediate vertex
b) 0 intermediate vertex
c) N intermediate vertices
d) N-1 intermediate vertices
Answer: b) 0 intermediate vertex
50. In the given graph, what is the minimum cost to travel from vertex 1 to vertex 3?
a) 3
b) 2
c) 10
d) -3
Answer: d) -3
51. In the given graph, how many intermediate vertices are required to travel from node a to
node e at a minimum cost?
a) 2
b) 0
c) 1
d) 3
Answer: c) 1
54. Fractional knapsack problem is solved most efficiently by which of the following
algorithm?
a) Divide and conquer
b) Dynamic programming
c) Greedy algorithm
d) Backtracking
Answer: c) Greedy algorithm
58. The main time taking step in fractional knapsack problem is ___________
a) Breaking items into fraction
b) Adding items into knapsack
c) Sorting
d) Looping through sorted items
Answer: c) Sorting
62. What happens when the backtracking algorithm reaches a complete solution?
a) It backtracks to the root
b) It continues searching for other possible solutions
c) It traverses from a different route
d) Recursively traverses through the same route
Answer: b) It continues searching for other possible solutions
63. A node is said to be ____________ if it has a possibility of reaching a complete solution.
a) Non-promising
b) Promising
c) Succeeding
d) Preceding
Answer: b) Promising
67. Which of the following logical programming languages is not based on backtracking?
a) Icon
b) Prolog
c) Planner
d) Fortran
Answer: d) Fortran
68. The problem of finding a list of integers in a given specific range that meets certain
conditions is called?
a) Subset sum problem
b) Constraint satisfaction problem
c) Hamiltonian circuit problem
d) Travelling salesman problem
Answer: b) Constraint satisfaction problem
70. ___________ enumerates a list of promising nodes that could be computed to give the
possible solutions of a given problem.
a) Exhaustive search
b) Brute force
c) Backtracking
d) Divide and conquer
Answer: c) Backtracking
71. The problem of finding a subset of positive integers whose sum is equal to a given
positive integer is called as?
a) n- queen problem
b) subset sum problem
c) knapsack problem
d) hamiltonian circuit problem
Answer: b) subset sum problem
72. The problem of placing n queens in a chessboard such that no two queens attack each
other is called as?
a) n-queen problem
b) eight queens puzzle
c) four queens puzzle
d) 1-queen problem
Answer: a) n-queen problem
74. Placing n-queens so that no two queens attack each other is called?
a) n-queen’s problem
b) 8-queen’s problem
c) Hamiltonian circuit problem
d) subset sum problem
Answer: a) n-queen’s problem
76. In n-queen problem, how many values of n does not provide an optimal solution?
a) 1
b) 2
c) 3
d) 4
Answer: b) 2
77. Which of the following methods can be used to solve n-queen’s problem?
a) greedy algorithm
b) divide and conquer
c) iterative improvement
d) backtracking
Answer: d) backtracking
78. Of the following given options, which one of the following is a correct option that
provides an optimal solution for 4-queens problem?
a) (3,1,4,2)
b) (2,3,1,4)
c) (4,3,2,1)
d) (4,2,3,1)
Answer: a) (3,1,4,2)
82. Of the following given options, which one of the following does not provides an optimal
solution for 8-queens problem?
a) (5,3,8,4,7,1,6,2)
b) (1,6,3,8,3,2,4,7)
c) (4,1,5,8,6,3,7,2)
d) (6,2,7,1,4,8,5,3)
Answer: b) (1,6,3,8,3,2,4,7)
83. Which of the following is/are property/properties of a dynamic programming problem?
a) Optimal substructure
b) Overlapping subproblems
c) Greedy approach
d) Both optimal substructure and overlapping subproblems
Answer: d) Both optimal substructure and overlapping subproblems
84. If an optimal solution can be created for a problem by constructing optimal solutions for
its subproblems, the problem possesses ____________ property.
a) Overlapping subproblems
b) Optimal substructure
c) Memoization
d) Greedy
Answer: b) Optimal substructure
85. If a problem can be broken into subproblems which are reused several times, the
problem possesses ____________ property.
a) Overlapping subproblems
b) Optimal substructure
c) Memoization
d) Greedy
87. In dynamic programming, the technique of storing the previously calculated values is
called ___________
a) Saving value property
b) Storing value property
c) Memoization
d) Mapping
Answer: c) Memoization
90. Which of the following problems should be solved using dynamic programming?
a) Mergesort
b) Binary search
c) Longest common subsequence
d) Quicksort
Answer: c) Longest common subsequence
92. Consider the recursive implementation to find the nth fibonacci number:
93. What is the time complexity of the recursive implementation used to find the nth
fibonacci term?
a) O(1)
b) O(n2)
c) O(n!)
d) Exponential
Answer: d) Exponential
94. What is the space complexity of the recursive implementation used to find the nth
fibonacci term?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: a) O(1)
95. Find the maximum sub-array sum for the given elements.
{2, -1, 3, -4, 1, -2, -1, 5, -4}
a) 3
b) 5
c) 8
d) 6
Answer: b) 5
96. Find the maximum sub-array sum for the given elements.
{-2, -1, -3, -4, -1, -2, -1, -5, -4}
a) -3
b) 5
c) 3
d) -1
Answer: d) -1
97. What is the time complexity of the divide and conquer algorithm used to find the
maximum sub-array sum?
a) O(n)
b) O(logn)
c) O(nlogn)
d) O(n2)
Answer: c) O(nlogn)
98. What is the space complexity of the divide and conquer algorithm used to find the
maximum sub-array sum?
a) O(n)
b) O(1)
c) O(n!)
d) O(n2)
Answer: b) O(1)
99. Find the longest increasing subsequence for the given sequence:
{10, -10, 12, 9, 10, 15, 13, 14}
a) {10, 12, 15}
b) {10, 12, 13, 14}
c) {-10, 12, 13, 14}
d) {-10, 9, 10, 13, 14}
Answer: d) {-10, 9, 10, 13, 14}
100. Find the length of the longest increasing subsequence for the given sequence:
{-10, 24, -9, 35, -21, 55, -41, 76, 84}
a) 5
b) 4
c) 3
d) 6
Answer: d) 6
101. The number of increasing subsequences with the longest length for the given
sequence are:
{10, 9, 8, 7, 6, 5}
a) 3
b) 4
c) 5
d) 6
Answer: d) 6
103. Which of the following methods can be used to solve the Knapsack problem?
a) Brute force algorithm
b) Recursion
c) Dynamic programming
d) Brute force, Recursion and Dynamic Programming
Answer: d) Brute force, Recursion and Dynamic Programming
104. You are given a knapsack that can carry a maximum weight of 60. There are 4 items
with weights {20, 30, 40, 70} and values {70, 80, 90, 200}. What is the maximum value of
the items you can carry using the knapsack?
a) 160
b) 200
c) 170
d) 90
Answer: a) 160
105. What is the time complexity of the brute force algorithm used to solve the Knapsack
problem?
a) O(n)
b) O(n!)
c) O(2n)
d) O(n3)
Answer: c) O(2n)
106. Which of the following methods can be used to solve the longest common
subsequence problem?
a) Recursion
b) Dynamic programming
c) Both recursion and dynamic programming
d) Greedy algorithm
Answer: c) Both recursion and dynamic programming
107. Consider the strings “PQRSTPQRS” and “PRATPBRQRPS”. What is the length of the
longest common subsequence?
a) 9
b) 8
c) 7
d) 6
Answer: c) 7
108. Which of the following problems can be solved using the longest subsequence
problem?
a) Longest increasing subsequence
b) Longest palindromic subsequence
c) Longest bitonic subsequence
d) Longest decreasing subsequence
Answer: b) Longest palindromic subsequence
110. What is the time complexity of the brute force algorithm used to find the longest
common subsequence?
a) O(n)
b) O(n2)
c) O(n3)
d) O(2n)
Answer: d) O(2n)
111. Which of the following is the longest common subsequence between the strings
“hbcfgmnapq” and “cbhgrsfnmq” ?
a) hgmq
b) cfnq
c) bfmq
d) fgmna
Answer: d) fgmna