Javatpoint Logo
Javatpoint Logo

Computer Science Quiz - III: Part 2

Topic - 6 Algorithms

1) Assume that T(n) is a function that the recurrence defines.

T(n) = 2T(n/2) + n when n >= 2 and T(1) = 1,

Which of the following claims is accurate?

  1. T(n) = O(n)
  2. T(n) = O(n logn)
  3. T(n) = O(n1/2)
  4. T(n) = O(logn)

Answer: (a) T(n) = O(n)


Given Recurrence Relation:

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

Using Masters' Theorem:

T(n) = aT(n/b) + f(n) ... (ii)

Comparing (i) and (ii), we get

a = 2,

b = 2, and

f(n) = n

Case I:

f(n) = O(n^logba)

n = O(n^log22)

n = O(n^1)

n = O(n), Equation holds

Hence, T(n) = O(n). Therefore, (a) is the correct answer.

2) There is a total of25 horses, and you need to identify the top 3 speed horses. To determine their relative speeds, races can be held between a maximum of five horses. You can never determine the horse's true speed in a race. Identify the number of races necessary to findthe top three horses.

  1. 5
  2. 6
  3. 7
  4. 8

Answer: (c) 7

Explanation: Run 5 races with the horses divided into 5 groups. Take the winners of the first five races and run thesixth race; the winner of this race will be the fastest of 25horsesoverall.

Run the seventh race now with the following horses.

1) The second and third-place horses in the sixth race

2) Select the second and third-fastest horses in the group that are tied for first in the sixth race.

3) Select the animal in the group that finished second fastest in the sixth race.

The fastestand secondfastest of the7th raceare the secondfastest and third fastestof all 25 horses, respectively.

Therefore, (c) is the correct answer.

3) There are n unique elements in an unordered list. To discover an element in this list that is neither the maximum nor the minimum, comparisons must be made a total of

  1. O(n)
  2. O(logn)
  3. O(n logn)
  4. O(1)

Answer: (d) O(1)


We simply need to compare any 3 of the elements. The middle element of the three will be one of the not maximum and minimum elements. Since there are constant numbers of comparisons, the time complexity is (1).

Therefore, (d) is the correct answer.

4) Two lists are supposed to be concatenated in O(1) time. Which of the subsequent list implementations should be used?

  1. Singly Linked List
  2. Doubly Linked List
  3. Circular Singly Linked List
  4. Circular Doubly Linked List

Answer: (d) Circular Doubly Linked List


Singly, doubly, and circular singly linked lists cannot be used to implement the given idea as we do not have a pointer to the last node. To concatenate two lists of either type, we need to traverse one of the lists to get access to the last node, which requires O(n) time.

A circular doubly linked list can be concatenated in constant time O(1). We only need to execute the correct sequence of code.

Let the front and last of the pointer of list1 and list2 be front1 and last1 and front2 and last2, respectively.

last1 -> next = front2

front2 -> prev = last1

front1 -> prev = last2

last2 -> next = front

We can rename the pointer of the new node as front = front1 and last = last2.

5) The worst-case time complexities of the Tower of Hanoi with n disks problem, the binary search on the sorted list of n elements, the quick sort on an array of n elements and the heap sort on an array of n elements are as follows

  1. O(2n), O(n logn), O(n2), O(nlogn), respectively
  2. O(2n), O(logn), O(ln logn), O(n logn), respectively
  3. O(2n), O(logn), O(n logn), O(n2), respectively
  4. O(2n), O(logn), O(n2), O(n logn), respectively

Answer: (d) O(2n), O(logn), O(n2), O(n logn)


The Tower of Hanoi is a mathematical puzzle in which we are given n disks of different sizes and three rods starting, ending and auxiliary. The objective of the puzzle is to move all the disks from the starting rod to the ending rod, and the rules are:

  1. We can move only one disk at a time, and
  2. A larger disk cannot be placed above a smaller disk.

The time complexity of this problem is O(2n).

In the worst-case, the time complexity of binary search is O(logn)

In the worst-case, the time complexity of heap sort remains O(n logn) but the time complexity of quick sort changes to O(n2).

6) 6 Q Take into account the following claims:

I. A leaf node is always where a max-smallest heapelement can be found.

II. The root node's child is always the second-largest element in a max-heap.

III. A binary search tree can be used to quickly create a max-heap in Q(n) time.

IV. A max-heap can be converted into a binary search tree in Q(n) time.

Which of the aforementioned claims is true?

  1. I, II, and IV
  2. I, II, and III
  3. II, III, and IV
  4. I, III and IV

Answer: (b) I, II, and III


The first statement is true. Leaf nodesin a max heap always containthe smallest element. Therefore, we must search for the minimum value for each leaf node. The worst-case complexity is O(n).

The second statement is also true. The largest element is always found at the top, and the second largest element is always the root child.

The third statement is true because building a max heap requires O(n) time.

The fourth statement is false since it takes O(n logn) time to create a binary search tree from a max-heap.

Therefore, (b) is the correct answer.

7) When used on an array that is sorted or nearly sorted, which of the following sorting algorithms, in its typical implementation, performs the best (maximum 1 or two elements are misplaced)?

  1. Heap Sort
  2. Merger Sort
  3. Quick Sort
  4. Insertion Sort

Answer: (d) Insertion Sort


In the best case, the time complexity of insertion sort becomes O(n) since it compares the elements and inserts them at their right position. So, when it comes to sorting a nearly sorted array, it will not take more time. On the other hand, heap sort, and merge sort will take O(n logn) time, and quick sort will take O(n2) will be the worst.

8) Think about a scenario where a swap procedure is exceedingly expensive. Which of the following sorting algorithms ought to be chosen in order to reduce the overall number of swap operations?

  1. Insertion Sort
  2. Selection Sort
  3. Merge Sort
  4. Heap Sort

Answer: (b) Selection Sort

Explanation: The selection sort selects the minimum or the maximum and swaps the element with its correct position. It does not take more than O(n) swaps. Selection sort performs best when it comes to number of swaps required.

9) If an element in an array X is larger than every element to its right, it is referred to as the leader of the array. a good approach for finding every leader in an array

  1. Use a left-to-right pass of the array to solve it in linear time.
  2. Use a right-to-left pass of the array to solve it in linear time.
  3. Use divide and conquer to solve the problem in Q(nlogn) time
  4. Find a solution in time theta (n2).

Answer: (b) Use a right-to-left pass of the array to solve it in linear time


The plan is to count up the most elements that have been reached so far while scanning the array's elements from right to left. Print the value whenever the maximum fluctuates.

To put the concept into practice, take the following actions:

  1. The last index position is where we begin. There are never any elements to its right. Hencethe last place is always the leader.
  2. After that, we continue iterating through the array until theindex position = 0. We constantly monitor the maximum value.
  3. Every time a maximum value is encountered higher than the highest maximum value previously encountered, we either print or save the value because it is the leader.

This can be done in linear time. Hence, (b) is the correct answer.

10) Assume that G is an undirected weighted graph and e is an edge in G with the maximum weight. Consider the minimum weight spanning tree in G that has the edge e in it. That which is always TRUE among the following statements?

  1. All the edges in G have unique weight
  2. All the edges in some cut sets in G are of maximum weight
  3. G has a unique minimum spanning tree
  4. Edge e cannot be a part of a cycle in G

Answer: (b) All the edges in a cut set in G are of maximum weight


Consider the following graph taken as an example:

Computer Science Quiz - III: Part 2

There are three edges with maximum weight. The edge ef will be the part of the mst.

A cut set is defined as the set of edges by removing them disconnects the graphs.

Option A: This statement is false because we cannot conclude that each edge is of unique weight. Although edges can be the same weight.

Option B: The cut set {ab, ac} contains both the edges of maximum weight. Hence, option (b) is true.

Option C: This statement is false. Here, we have the option to choose between edges ab and ac. So, we can guarantee uniqueness.

Option D: This statement is also false. There are possibilities that e can be a part of a cycle.

11) The appropriate data structure usedto utilize in order to implement Dijkstra's shortest path algorithm on unweighted graphs is:

  1. Stack
  2. Heap
  3. BST
  4. Queue

Answer: (b) Heap


Both the heap and the priority queue are particularly attractive data structuresallowing:

  • Add a component to the heap that has a priority.
  • The highest-priority piece should be taken out of the heap or priority queue and returned.
  • Access to the Peak element that has maximum prioritywithout deleting it.

Maintaining a list of components and searching through it for the one with the highest priority is a straightforward approach to create a heap or priority queue data type. This gives Dijkstra's shortest path algorithm on an unweighted graph an implementation time of O(n).

12) Which of the following claims about the Bellman-Ford shortest path algorithm is true?

I. If there is a negative weighted cycle, it is always found.

II. Determines whether any negative weighted cycle can be reached from the source.

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

Answer: (b) Only II


The Bellman-Ford shortest path algorithm is used to determine a single-sourceshortest path. Therefore, it can only detect cycles that can be reached from a certain source and cannot find cycles with anegative weight that are not reachable from the given source.

Think about a disconnected situation where a negative weight cycle cannot be reached at all from the source.If there is a negative weight cycle reachable from the source, after (number of vertices- 1)number of iterations, the algorithm will detect it.

So, saying it will always find the negative weight cycle is wrong. Hence, the statement I is false, and the statement II is true.

13) Think about the tree arcs of a BFS traversal starting at source node W in an unweighted, connected, undirected graph. Using the tree T created by the tree arcs, one may compute

  1. The shortest route between each pair of vertices
  2. The shortest route from W to each graph vertex.
  3. Only those nodes that are leaves of T have the shortest pathways from W to them.
  4. The graph's longest path.

Answer: (b) The shortest route from W to each graph vertex


The tree formed by doing BFS traversal from node W gives shortest path of all the nodes from W. In an unweighted graph, BFS always finds the shortest route from the source to every other vertex.

Therefore, (b) is the correct answer.

14) Assume that G is an undirected connected weighted graph with a set of distinct positive edge weights. Which of the following statements (if any) is/are TRUE if each edge weight is increased by the same amount?

I. There will be no change in the minimum spanning tree.

II. The shortest distance between all the vertices will remain same.

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

Answer: (a) Only I


The shortest route might alter. The reason is thatany pair of edges, such as a and b, may have a different number of edges in various pathways. For illustration, suppose the shortest path between a and bhas 4edges and a weight of 12. Allow for a second path with two edges and a weight of 17. The weight of the shortestpath after incrementing each edge weight with 5is now 12+ 4*5 = 32. The other path's weight is boosted by 2*5, making it 17+ 10 = 27. Thus, the shortest way switches to another path with aweight of 27.

There is no modification to the Minimum Spanning Tree. Keep in mind that in Kruskal'salgorithm, the edges are sorted first. If all weights are increased, the edges' order won't change.

15) What is the weight of the minimum spanning tree of the given graph?

Computer Science Quiz - III: Part 2
  1. 28
  2. 36
  3. 34
  4. 40

Answer: (c) 34


Using Kruskal's algorithm, we first sort all the edges in descending edge weight. And try to select the edges with minimum edge weight and we have to make sure we avoid any type of cycle formation in the spanning tree:

Edge Weight Selected Reason
ac 2 Yes They have the minimum edge weights, and all are necessary.
eh 2 Yes
if 2 Yes
dh 3 Yes
ef 4 Yes
ab 4 Yes
bd 5 Yes
ad 6 No On selecting ad, we get a cycle of adba, which will eventually affect the property of the spanning tree.
bi 6 No On selecting bi, we get a cycle of bifehdb.
cd 9 No On selecting cd we get a cycle of cdbac which will also violate the property of the spanning tree.
fg 12 Yes To connect g with the MST, we must select fg with the minimum edge weight among the edges connecting g.
dg 13 No On selecting them we get some cycle. We have already selected n - 1 edges.
be 18 No
hg 19 No

So, the weight of the minimum spanning tree = 2 + 2 + 2 + 3 + 4 + 4 + 5 + 12 = 34.

Therefore, (c) is the correct answer.

16) The Floyd Warshall algorithm for computing all-pair shortest routes is based on the following:

  1. Greedy Method
  2. Dynamic Programming
  3. Divide and Conquer
  4. Subtract and Conquer

Answer: (b) Dynamic Programming


All pairs of shortest-path issues can be resolved using the Floyd Warshall algorithm. Finding the shortest distances between each pair of vertices in an edge-weighted directed graph is the goal of the problem. Dynamic programming serves as its foundation. Therefore, (b) is the correct answer.

17) Consider a chain of matrices that have been multiplied: A1 x A2 x A3 x A4 x A5, where A1, A2, A3, A4 and A5 matrices with dimensions of 5 x 10, 10 x 35, 35 x 15, 15 x 25, and 25 x 100 respectively. What will the minimum total number of scalar multiplications in the parenthesisation of A1 x A2 x A3 x A4 x A5 be _____________?

  1. 18775
  2. 34000
  3. 9000
  4. 19375

Answer: (a) 18775


Formula to calculate the cost of multiplication: m[i, j] represents the cost of multiplying matrix number i to j.

m[i, j] = { 0 if (i == j)}

m[i, j] = { min[m[i, k] + m[k+1, j] + Pi-1 Pk Pj] if (i < j)}

The range of k will be i <= k < j. We will calculate the cost on each value of k and select the minimum cost.

We have created this table using the matrix dimension:

P0 P1 P2 P3 P4 P5
5 10 35 15 25 100

The cost of multiplying one matrix is zero that's why we have filled zero in the respective cells.

Now, calculating m[1, 2]. The possible value of k is 1.

For k = 1:

m[1, 2] = m[1, 1] + m[2, 2] + P0* P1* P2

= 0 + 0 + 5 * 10 * 35

= 1750

Similarly, we can find the cost of m[2, 3], m[3, 4] and m[4, 5].

Now, calculating m[1, 3]. The possible values of k = 1, 2

For k = 1:

m[1, 3] = m[1, 1] + m[2, 3] + P0 * P1 * P3

= 0 + 5250 + 5 * 10 * 15

= 0 + 5250 + 750

= 6000

For k = 2:

m[1, 3] = m[1, 2] + m[3, 3] + P0 * P2 * P3

= 1750 + 0 + 5 * 35 * 15

= 4375

We have got the minimum cost for k = 2.

Similarly, we can calculate the cost of m[2, 4] and m[3, 4].

The complete table will look like the following:

Computer Science Quiz - III: Part 2

The cost of multiplying the matrices is 18750.

18) In order to find the longest common subsequence between two strings,we use the concept of dynamic programming. Using an array L[M, N], the values of L(i, j) could be determined using dynamic programming based on the correct recursive definition of L(i, j). Which of the following claims about the dynamic programming answer to the recursive definition of L(i, j) is TRUE?

  1. To correctly compute the values of L(i, j), all elements ofL(M, N)must be initialized to 0.
  2. Both a row-majororder and a column-majororder of L(M, N)can be used to compute the values of l(i, j).
  3. Both the row-majororder and the column-major order of L (M, N)cannot be used to compute the values of l(i, j).
  4. If either p < ror q <s, L[p, q] must be calculated before L[r, s].

Answer: (b) Both a row-majororder and a column-majororder of L(M, N)can be used to compute the values of l(i, j).


Option A is False because it is not necessary to initialized all the elements of L[M, N] to 0. We only fill zeros in the first row and the first column.

Option B is True. By doing the computation either in row major or column major, we can perform the desired computation.

Option C is False. It negates the option B which is not true.

Option D is False because to compute the value of l[i, j], we only need the values of l[i-1, j], l[i, j-1] and l[i-1, j-1]. The condition p<r or q<s do not completely satisfy the condition.

19) Match the following:

A. Huffman Coding i. Subtract and Conquer
B. Floyd Warshall Algorithm ii. Divide and Conquer
C. Selection Sort iii. Greedy Technique
D. Merge Sort iv. Dynamic Programming
  1. A - (iii), B - (iv), C - (ii), D - (i)
  2. A - (iv), B - (iii), C - (ii), D - (i)
  3. A - (iii), B - (iv), C - (i), D - (ii)
  4. A - (i), B - (ii), C - (iii), D - (iv)

Answer: (c) A - (iii), B - (iv), C - (i), D - (ii)


  • An algorithm for lossless data compression is Huffman coding. Input characters are given variable-length codes, the lengths of which are determined by the frequency of the matching characters. It is based on the greedy method.
  • All pairs of shortest-path issues can be resolved using the Floyd Warshall algorithm. Finding the shortest distances between each pair of vertices in an edge-weighted directed graph is the goal of the problem. Dynamic programming serves as its foundation.
  • The selection sort method sorts an array by repeatedly selecting the first element from the unsorted portion that is the least in value while still taking into account ascending order. The least entry (regarding ascending order) from the unsorted subarray is chosen and relocated to the start of the unsorted subarray in each iteration of the selection sort. Subtract and conquer is the theory behind the concept.
  • The Divide and Conquer concept serves as the foundation for the sorting algorithm known as Merge Sort. This algorithm divides the array into two equal parts, which are then combined in a sorted way.

Therefore, option (c) is the correct answer.

20) Think about the sequence abbbcddddee. Each letter in the string needs to have a binary code that meets the requirements listed below:

  • The code assigned to one letter for any pair of letters cannot be the prefix of the code assigned to the other letter.
  • The letter that appears earlier in the dictionary order is given a code whose length is at most equal to the code given to the other letter for any two letters with the same frequency.

What is the shortest encoded string length among all the binary code assignments that meet the two properties mentioned above?

  1. 24
  2. 25
  3. 26
  4. 27

Answer: (a) 24


The properties mentioned in the question are the same as for Huffman coding.

We will follow two basic rules here:

i. For creating a Tree, we will select the alphabet with the minimum frequency and create the Huffman tree in bottom-up fashion.

ii. We will assign binary 1 to the right edge of a node and binary 0 to the left edge of a node.

To assign the code, we will traverse the tree from the root to all the leaf nodes containing alphabet.

Alphabet Frequency
a 1
b 3
c 1
d 4
e 2

Computer Science Quiz - III: Part 2

Binary Code Assigned to each alphabet:

a - 0100, Length = 4

b - 00 , Length = 2

c - 0101, Length = 4

d - 1, Length = 1

e - 011, Length = 3

Shortest length of the string in binary = 4 * 1 + 2 * 3 + 4 * 1 + 1 * 4 + 3 * 2

= 4 + 6 + 4 + 4 + 6 = 24

Therefore, (a) is the correct answer.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA