0% found this document useful (0 votes)
52 views

Implementation of Algorithms To Solve Problems

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Implementation of Algorithms To Solve Problems

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Implementation of

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

Multiple choice questions 4. (a) Binary Search


Explanation: Binary Search: Search a sorted array
1. (d) Ο(n) by repeatedly dividing the search interval in half.
Explanation: Binary Search is a process finding Begin with an interval covering the whole array.
an element from the ordered set of elements. The 5. (c) 9, 8, 6, 3, 3, 1
worst case time Complexity of binary search is Explanation: Non increasing order simply refers
O(log 2 n). to the idea of sorting things where subsequent
2. (d) All of the above items are less than, or equal to the previous item.
Explanation: It is a type of data structure where 6. (b) To make sure that it is still a complete binary
the arrangement of the data follows a linear trend. tree.
The data elements are arranged linearly such that Explanation: A binary heap is a heap data
the element is directly linked to its previous and structure that takes the form of a binary tree.
the next elements. As the elements are stored Binary heaps are a common way of implementing
linearly, the structure supports single-level priority queues.
storage of data.
7. (a) Base case
3. (b) Greedy Algorithm
Explanation: Theta notation of an expression
Explanation: An algorithm that always takes the is to drop low order terms and ignore leading
best immediate, or local, solution while finding an constants.
answer.
8. (d) None of the above operations to the returned value for the smaller
9. (d) both A & B input.
10. (b) It undertakes all other factors to be dynamic. 19. (a) sorted
11. (d) Ο(n2) 20. (c) stacks
Explanation: The worst case occurs in the Linear 21. (b) FIFO
Search Algorithm when the item to be searched is 22. (b) 2k+1 − 1
in end of the Array. Explanation: The maximum number of nodes at
12. (a) Queue level ‘l’ of a binary tree is 2l-1
Explanation: First in First out Queue: For this, the 23. (a) queue
element that goes first will be the first to come out. Explanation: Depth First Traversal (or Search)
To work with FIFO, you have to call Queue() class for a graph is similar to Depth First Traversal of
from queue module. a tree. The only catch here is, unlike trees, graphs
13. (b) nn - 2 spanning trees may contain cycles, a node may be visited twice.
14. (b) Merge Sort To avoid processing a node more than once, use a
Boolean visited array.
Explanation: Merge Sort is a Divide and Conquer
algorithm. It divides the input array into two 24. (a) queue
halves, calls itself for the two halves, and then Explanation: Breadth-first search is an algorithm
merges the two sorted halves. The merge() for searching a tree data structure for a node that
function is used for merging two halves. satisfies a given property. It starts at the tree root
and explores all nodes at the present depth prior
15. (d) Polish Notation
to moving on to the nodes at the next depth level.
Explanation: Polish notation is a notation form
25. (d) stack
for expressing arithmetic, logic and algebraic
equations. Its most basic distinguishing feature 26. (b) False
is that operators are placed on the left of their 27. (c) Recursion
operands. If the operator has a defined fixed 28. (c) Both Stack & Queue
number of operands, the syntax does not require 29. (d) All of the above
brackets or parenthesis to lessen ambiguity. 30. (c) Binary Search Tree
16. (c) sorted list 31. (a) node.next inaccessible
Explanation: A sorted list is a combination of an
array and a hash table. It contains a list of items Fill in the blanks
that can be accessed using a key or an index. If
you access items using an index, it is an Array List, 32. (c) Post-order Traveller
and if you access items using a key, it is a Hash 33. (b) linear search
table. The collection of items is always sorted by 34. (a) linear search
the key value. 35. (a) complete binary tree
17. (a) parent nodes have values more than or equal 36. (b) pivot element
to their child 37. (b) selection sort
Explanation: A min-heap is a binary tree such 38. (a) Selection Sort
that. - the data contained in each node is less than
39. (c) resource allocation
(or equal to) the data in that node›s children. - the
40. (d) None of the above
binary tree is complete.
41. (c) Execute infinitely.
18. (c) recursive
Explanation: A recursive algorithm is an Match the following
algorithm which calls itself with «smaller (or
simpler)» input values, and which obtains the 42. (c) 1-(iii), 2-(i), 3-(ii)
result for the current input by applying simple 43. (b) 1-(ii), 2-(iii), 3-(i)
51. (c) O(n2)
Numerical based
Explanation: Bubble Sort is an easy-to-implement,
44. (d) 10 stable sorting algorithm with a time complexity of
Explanation: (1ST-8,7,9,22,5,13,31 (4 SWAPS) O(n²) in the average and worst cases – and O(n) in
2ND-7,8,9,5,13,22,31 (3SWAPS) the best case.
3RD-7,8,5,9,13,22,31(1 SWAPS) 52. (a) 4
4TH-7,5,8,9,13,22,31(1 SWAPS) Explanation: The given array is arr = {1, 2, 4, 3}.
5TH-5,7,5,9,13,22,31(1 SWAPS) Bubble sort is used to sort the array elements.
4+3+1+1+1=10 How much iteration will be done to sort the array?
45. (a) 4 Explanation: Even though the first two elements
are already sorted, bubble sort needs 4 iterations
Explanation: In 1 st pass, 25134, 2 and 5 are
compared. No change in that case to sort the given array.

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.

You might also like