Implementation of Algorithms To Solve Problems
Implementation of Algorithms To Solve Problems
algorithms to solve
problems
Multiple choice questions 8. Identify the algorithm which cannot be designed
without recursion −
1. The worst-case run-time complexity in binary (a) Tower of Hanoi
search algorithms (b) Fibonacci Series
(a) Ο(n2) (b) Ο(nlog n) (c) Tree Traversal
(c) Ο(n3) (d) Ο(n)
(d) None of the above
2. What do you understand by linear data
9. The following sorting algorithms maintain two
structure −
sub-lists, one sorted and one to be sorted −
(a) Queue (b) Stack
(a) Selection Sort (b) Insertion Sort
(c) Arrays (d) All of the above
(c) Merge Sort (d) both A & B
3. The travelling salesman problem is an example of
10. A posterior analysis is more accurate than Piori
(a) Dynamic Algorithm
analysis because −
(b) Greedy Algorithm
(a) it contains the actual data.
(c) Recursive Approach
(b) It undertakes all other factors to be dynamic.
(d) Divide & Conquer
(c) It undertakes all other factors to be constant.
4. State the searching technique which do not require
the data to be in sorted form (d) It is a result of reverseengineering.
(a) Binary Search 11. What is the worst-case time complexity of linear
search algorithms?
(b) Interpolation Search
(a) Ο(1) (b) Ο(n)
(c) Linear Search
(c) Ο(log n) (d) Ο(n2)
(b) All of the above
5. Choose the series which is in Non-Increasing 12. Which of the following uses FIFO method ?
Order. (a) Queue (b) Stack
(a) 1, 3, 4, 6, 8, 9 (b) 9, 8, 6, 3, 4, 1 (c) Hash Table (d) Binary Search Tree
(c) 9, 8, 6, 3, 3, 1 (d) 1, 3, 3, 6, 8, 9 13. A complete graph can have
6. Using binary heap, the root›s rightmost (a) n2 spanning trees
component of the last level is substituted by the (b) nn - 2 spanning trees
root where the root is removed. What will be the (c) nn + 1 spanning trees
reason?
(d) nn spanning trees
(a) It is the easiest possible way.
14. From the options given below, identify which one
(b) To make sure that it is still a complete binary
is not the divide and conquer approach?
tree.
(a) Insertion Sort (b) Merge Sort
(c) B
ecause the left and right subtrees might be
missing. (c) Shell Sort (d) Heap Sort
(d) None of the above! 15. Prefix notation is also known as
7. The Θ notation in asymptotic evaluation (a) Reverse Polish Notation
represents − (b) Reverse Notation
(a) Base case (b) Average case (c) Polish Reverse Notation
(c) Worst case (d) NULL case (d) Polish Notation
16. In order traversal of binary search,the tree will 26. Postfix expression is just a contrary of prefix
produce − expression.
(a) unsorted list (a) True
(b) reverse of input (b) False
(c) sorted list 27. Stack is used for
(d) none of the above (a) CPU Resource Allocation
17. In a min-heap: (b) Breadth-First Traversal
(a) parent nodes have values more than or equal (c) Recursion
to their child (d) None of the above
(b) parent nodes have values less than or equal to 28. A circular linked list can use for
their child (a) Stack
(c) both statements are true (b) Queue
(d) both statements are wrong (c) Both Stack & Queue
18. A procedure that calls itself is called (d) Neither Stack nor Queue
(a) illegal call 29. Which is an instance of a dynamic programming
(b) reverse polish approach?
(c) recursive (a) Fibonacci Series
(d) none of the above (b) Tower of Hanoi
19. For a binary search algorithm to work, the array (c) Dijkstra Shortest Path
(list) must be (d) All of the above
(a) sorted 30. The following formula is of
(b) unsorted left_subtree (keys) ≤ node (key) ≤ right_subtree
(c) in a heap (keys)
(d) popped out of stack (a) Binary Tree
20. push() and pop() functions are found in (b) Complete Binary Tree
(a) queues (b) lists (c) Binary Search Tree
(c) stacks (d) trees (d) All of the above
21. Queue data structure works on 31. node.next -> node.next.next; will make
(a) LIFO (b) FIFO (a) node.next inaccessible
(c) FILO (d) none of the above (b) node.next.next inaccessible
22. The highest number of nodes in a binary tree with (c) this node inaccessible
height k, where the root is height 0, is (d) none of the above
(a) 2k − 1 (b) 2k+1 − 1
(c) 2k-1 + 1 (d) 2k – 1 Fill in the blanks
23. What data structure is used for the depth-first
32. Visiting root node after visiting left and right sub-
traversal of a graph?
trees is called __________ .
(a) queue (b) stack
(a) In-order Traversal
(c) list (d) none of the above
(b) Pre-order Traversal
24. Name data structure that is used for the breadth-
(c) Post-order Traveller
first traversal of a graph.
33. The worst-case complexity of binary search
(a) queue (b) stack
matches with _________
(c) list (d) none of the above
(a) interpolation search
25. Which data structure can be used to check if the
(b) linear search
syntax has balanced parenthesis?
(c) merge sort
(a) queue (b) tree
(d) none of the above
(c) list (d) stack
34. __________ Algorithm does not divide the list.
Match the following
(a) Linear search
(b) Binary search 42. Match the columns:
(c) Merge sort Group A Group B
(d) Quick sort 1. Completeness (i) time it take to
35. Heap is an example of ________. find a solution
(a) complete binary tree 2. Time Complexity (ii) memory need
to perform the
(b) spanning-tree
search
(c) sparse tree
3. Space Complexity (iii) Is the strategy by
(d) binary search tree which we find
36. Quicksort running time depends on the selection the solution.
of (a) 1-(iii), 2-(ii), 3-(i)
(a) size of array (b) 1-(i), 2-(ii), 3-(iii)
(b) pivot element (c) 1-(iii), 2-(i), 3-(ii)
(c) sequence of values (d) 1-(i), 2-(iii), 3-(ii)
(d) none of the above! 43. Match the columns:
37. ______________ sorting techniques has highest Group A Group B
best-case runtime complexity − 1. Bubble Sort (i) O(n)
(a) Quick sort 2. Shell Sort (ii) O(n2)
(b) Selection sort 3. Selection Sort (iii) O(n log n)
(c) Insertion sort (a) 1-(i), 2-(ii), 3-(iii)
(d) Bubble sort (b) 1-(ii), 2-(iii), 3-(i)
38. _____________ sorting algorithms are not stable? (c) 1-(i), 2-(iii), 3-(ii)
(a) Selection Sort (d) 1-(ii), 2-(i), 3-(iii)
(b) Bubble Sort
Numerical based questions
(c) Merge Sort
(d) Insertion Sort 44. The number of swaps which are needed to sort the
39. A queue data-structure can be used for_________ numbers 8, 22, 7, 9, 31, 5, 13 in ascending order,
(a) expression parsing using bubble sort is
(b) recursion (a) 11 (b) 12
(c) resource allocation (c) 13 (d) 10
(d) all of the above 45. How many swaps are essential to sort the given
40. __________algorithm cannot be designed without array using bubble sort - { 2, 5, 1, 3, 4}
recursion. (a) 4 (b) 5
(a) Tower of Hanoi (c) 6 (d) 7
(b) Fibonacci Series 46. The sum of binary trees with 3 nodes which when
(c) Tree Traversal traversed in post-order gives the sequence A,B,C
(d) None of the above is?
41. If there›s no base criteria in a recursive program, (a) 3 (b) 4
the program will______________ (c) 5 (d) 6
(a) not be executed. 47. What will be the worth of top, if there is a
(b) execute until all conditions match. proportion of stack STACK_SIZE is 5
(c) execute infinitely. (a) 5 (b) 6
(d) Obtain a progressive approach. (c) 4 (d) None
48. The minimum number of comparisons essential ?4?= arr[j];
to find the minimum and the maximum of 100 j =?5?;
numbers is }
(a) 148 (b) 146 arr[j + 1] = key;
(c) 147 (d) 100 }
49. State the Huffman code for the letters a,b,c,d,e? }
(a) 0,10,110,1110,1111 Answer the following question:
(b) 10,011,11,001,010 (a) What is the statement or expression at ?1?
(c) 10,01,0001,100,1010 (i) arr.length (ii) array.length
(b) 100,110,001,000,010 (iii) length (iv) -1
50. Out of 4 distinct keys, how many distinct primary (b) What is the statement or expression at ?2?
keys can be created?
(i) arr[i]; (ii) array[j+1]
(a) 5 (b) 20
(iii) array[j] (iv) 0
(c) 45 (d) 14
(c) What is the statement or expression at ?3?
51. The complexity of the bubble sort algorithm is …..
(i) i - 1 (ii) array[j]
(a) O(n) (b) O(logn)
(iii) i+1 (iv) n
(c) O(n2) (d) O(n logn)
(d) What is the statement or expression at ?4?
52. The given array is arr = {1,2,4,3}. Bubble sort is
(i) arr[j + 1] (ii) key
being used to sort the array elements. How many
iterations will be done to sort the array with an (iii) array[j] (iv) array[i]
improvised version? (e) What is the statement or expression at ?5?
(a) 4 (b) 2 (i) array[i+1] (ii) j - 1
(c) 1 (d) 0 (iii) j (iv) array[i]
53. How many passes does a Bubble sort algorithm 55. The following program code sorts a single
require for sorting a given list of n items? dimensional array in ascending order using
(a) n*2 (b) n*1 Selection Sort technique. There are some places
in the code marked as ?1?, ?2?, ?3?, ?4? and ?5? Are
(c) n+1 (d) n-1
to be replaced by a statement/expression so that
Programming based questions the code works correctly.
voidsort(int ?1?)
54. The following program code sorts a single {
dimensional array in ascending order using intn =?2?;
Insertion Sort technique. There are some places in
the code marked as ?1?, ?2?, ?3?, ?4? and ?5? which
// One by one move boundary of unsorted
are to be replaced by a statement/expression so
subarray
that the code works correctly.
for(int i = 0; i< n-1; i++)
voidsort(intarr[])
{
{
// Find the minimum element in unsorted
int n =?1?;
array
for(int i = 1; i< n; ++i) {
int ?3?=i;
int key = ?2?
for(int j = i+1; j < n; j++)
int j =?3? ;
if(?4? <arr[min_idx])
min_idx = j;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
// Swap the found minimum element with the
of their current position */
first
while(j >= 0&&arr[j] > key) {
// element
inttemp =?5?; {
arr[min_idx] = arr[i]; if (?1?)
arr[i] = temp; {
} /* pi is partitioning index, arr[pi] is now
(a) What is the statement or expression at ?1? at right place */
(i) arr[] (ii) array.length ?2?= partition(arr, low, high);
(iii) length (iv) -1 quickSort(arr, low,?3?); // Before pi
(b) What is the statement or expression at ?2? ?5?(arr, ?4?, high); // After pi
(i) arr.length (ii) array[j+1] }
(iii) array[j] (iv) 0 }
(c) What is the statement or expression at ?3? (a) What is the statement or expression at ?1?
(i) min_idx (ii) array[j] (i) first + last (ii) low < high
(iii) i+1 (iv) n (iii) length (iv) -1
(d) What is the statement or expression at ?4? (b) What is the statement or expression at ?2?
(i) arr[j] (ii) key (i) arr[mid] (ii) pi
(iii) array[j] (iv) array[i] (iii) array[j] (iv) 0
(e) What is the statement or expression at ?5? (c) What is the statement or expression at ?3?
(i) array[i+1] (ii) arr[min_idx] (i) mid + 1 (ii) pi - 1
(iii) j (iv) array[i] (iii) i+1 (iv) n
56. The following program code is using the quick (d) What is the statement or expression at ?4?
sort technique. Some places in the code marked (i) mid - 1 (ii) pi + 1
as ?1?, ?2?, ?3?, ?4? and ?5? Are to be replaced by (iii) array[j] (iv) array[i]
a statement/expression so that the code works
(e) What is the statement or expression at ?5?
properly.
(i) array[i+1] (ii) quickSort
quickSort(arr[], low, high)
(iii) j (iv) array[i]
Answers
Same pass 5 and 1 are compared thus swapped 53. (d) n-1
(total swap = TS = 1), => 21534 => 21354 (TS = 2), =>
21345 (TS = 3),
Programming based questions
Next pass i.e 2 => 1 and 2 swapped => 12345 (TS = 54. (a) (i) arr.length
4)
(b) (i) arr[i];
46. (c) 5
(c) (i) i - 1
Explanation: 2^n -n where n = number of nodes
(d) (i) arr[j + 1]
47. (c) 4
(e) (i) array[i+1]
48. (a) 148
Explanation: (N = 100 MIN NO OF 55. (a) (i) arr[]
COMPARISONS = (3N/2)-2 (b) (i) arr.length
(3 X 100)/2 - 2 = 148) (c) (i) min_idx
49. (a) 0,10,110,1110,1111 (d) (i) arr[j]
Explanation: The probability are ½,1/4, (e) (ii) arr[min_idx]
1/8,1/16,,1/32 56. (a) (ii) low < high
So, the Huffman code according to the tree is
(b) (ii) pi
unique.
(c) (ii) pi - 1
50. (d) 14
(d) (ii) pi + 1
Explanation: The number of distinct BST for n
nodes are given as ((2n)Cn)/(n+1) So, for 4 distinct (e) (ii) quickSort
nodes, we can have (8C4)/5 = 14 distinct BSTs.