0% found this document useful (0 votes)
18 views21 pages

Algo

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

Algo

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

Recursion and Master Theorem:

Q. Is there are 3 recursive call are there what is the time complexity (my qs to chat gpt)
ChatGPT said:

When a recursive algorithm makes three recursive calls at each step, the time complexity depends
on two key factors:
1. The number of recursive calls per step (in this case, 3).
2. The size reduction of the problem in each recursive step (usually denoted by how much
smaller the input gets).
This type of problem can be described using a recurrence relation, and you can solve it using the
Master Theorem or through recursion tree analysis.

General Form:
For an algorithm that makes 3 recursive calls and reduces the input size by a constant factor in each
step, the recurrence relation looks like:
T(n)=3T(n / k)+O(nd)

Where:
• T(n) is the time complexity for input size n,
• 3 indicates three recursive calls,
• n / k represents the input size reduction in each step (by a factor k),
• O(nd) represents the time taken outside the recursive calls, often linear or quadratic.

Using the Master Theorem:


The Master Theorem applies to recurrences of the form:
T(n)=aT(n / b)+O(nd)

Where:

> a is the number of recursive calls (3 in this case),


> b is the factor by which the problem size is divided in each step,
> d is the exponent of the work done outside the recursive calls.

We can compare the values of a, bd, and use the Master Theorem rules to determine the time
complexity:
1. If a>bd, then T(n)=O(nlogba).
2. If a=bd, then T(n)=O(nd logn).
3. If a<bd, then T(n)=O(nd).

Example:
If the recurrence is:
T(n)=3T(n / 2)+O(n)

Here, a=3, b=2, and d=1.

• Logb a=log2 3 ≈ 1.585,


• Since a>bd (because 3 > 21), the time complexity is dominated by the recursive calls.
Thus, the time complexity is: T(n) = O(nlog23) ≈ O(n1.585)

Conclusion:
When there are 3 recursive calls and the problem size is reduced by a constant factor k (e.g., divided
by 2), the time complexity depends on how the problem is divided. In typical cases where the input
size is halved at each step, the time complexity will be approximately O(n log23) , or about O(n1.585).
LESSON – 10
Q.1. If the array A contains the items 10, 4, 7, 23, 67, 12 and 5 in that order, what will be the
resultant array A after third pass of insertion sort? [Asked in L&T Infotech (LTI) 2021]

a) 67, 12, 10, 5, 4, 7, 23

b) 4, 7, 10, 23, 67, 12,5

c) 4, 5, 7, 67, 10, 12, 23

d) 10, 7, 4, 67, 23, 12,5

QWhat is the running time of an insertion sort algorithm if the input is pre-sorted? [Asked in IBM
2020]
a) O(N2) b) O(N log N) c) O(N) d) O(M log N)

-->The ans is O(N), because when the array is


already sorted the intrnal loop doesn’t run.
-->To traves the entire array we requre only the
extrenal array. So, time complexity = O(N)

Q.2. Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of insertions done while
running insertion sort on the array? [Asked in Amcat 2020]

a)79421 47921 24791 12479

b)97412 97124 91247 12479

c)74219 42197 21974 19742

d)79421 24791 47921 12479

Q The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array elements. How many
iterations will be done to sort the array with improvised version? [Asked in TCS NQT 2021] [Asked
in Capgemini2020]

a)4 b) 2 c)1 d)0

Improvised version means, busing


the bubble sort algo with the flag.

Ans:
d)0
LESSON – 12

i)
To determine the total cost of the algo we have to cal
the cost of build_max_heap() function.

As for loop is there, we know the cost = O(n)

ii)
Cost of this is = O(1)

Cost of the for loop = O(n/2) but we can write it


as O(n)
Now in our 3rd set we have to cal. the cost of this
function.

iii)
>This function is used to compare the value with
left and right node and among them which contains
maximum value, it moves to the upper part of the
Heap.
>We can notice that max-heapify() function if called
recursively.
> So, in the worst case when the maximum node is
at the bottom of tree,so to do the max heapify the
the algo have to travel the bottom of the tree.
> As the height of the tree is nlogn, so time
complexity in this case is also nlogn.

For all these operation time complexity is O(1)


From all these we know that,
To Build Max Heap Time Complexity:
• Max-Heapify, cost / time complexity = O(logn)
• As Max-Heapify() function is inside the for loop
of build_max_heap() function, Time complexity
for build_max_heap() = O(nlogn).
• So, to build max heap time complexity is
O(nlogn).

To Sort The Heap Time Complexity:


• As for loop is there time complexity = O(n)
• Inside the for loop max-heapify is present. So,
time complexity for that = O(logn)
• So, to sort the heap time complexity = O(nlogn).

Total Time Complexity To Build and Sort The Max Heap:

Time complexity = O(nlogn)

Time complexity = O(nlogn)

So, total time complexity = nlogn + nlogn = 2nlogn


we can write that O(nlogn).

Search for it
To do traves the max heap total time complexity ?
To insert an element in max heap time complexity?

To delete an element from heap total time complexity = logn


LESSON – 13
Q.1. Assume that a Merge_Sort algorithm in the worst case takes 30 seconds for an input of size 64.
Which of the following most closely approximates the maximum input size of a problem that can be
solved in 6 minutes? (Gate-2015) (1 Marks) [Asked in Capgemini 2021]

a) 256
b) 512
c) 1024
d) 2048

Q.2. A list of n string, each of length n, is sorted into lexicographic order using the merge-sort
algorithm. The worst case running time of this computation is (Gate- 2012) (1 Marks)

(A) O(nlogn)
(B) O(n2 logn)
(C) O(n2 + logn)
(D) O(n2)

Q.3. Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30,35, 50 respectively. They
are to be merged into a single sequence by merging together two sequences at a time. The number
of comparisons that will be needed in the worst case by the optimal algorithm for doing this is
______. [Asked in Wipro NLTH 2020]

a) 500 b) 358 c) 450 d) 259

Q Merge sort uses which of the following technique to implement sorting? [Asked in Syntel 2019]
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming

Ans:
c) divide and conquer

Q.4. Which of the following sorting algorithms users recursion? [Asked in Capgemini 2020]
a) Heap sort
b) Bubble sort
c) Merge sort
d) Insertion sort
Ans:
a) Heap sort and c) Merge sort
In heap sort the max_heapify is called recursively .

Q.5. What is the auxiliary space complexity of merge sort? [Asked in Hexaware 2020]
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Q Sorting compares two adjoining values and exchange them if they are not in proper order. [Asked
in TCS NQT 2020]
a) Heap
b) Selection
c) Insertion
d) Bubble

Ans:
d)Bubble sort

Q.6. An operator delete(i) for a binary heap data structure is to be designed to delete the item in
the i-th node. Assume that the heap is implemented in an array and i refers to the i-th index of
the array. If the heap tree has depth d (number of edges on the path from the root to the
farthest leaf), then what is the time complexity to re-fix the heap efficiently after the removal of
the element? (Gate-2016) (1 Marks)

a) O(1)

b) O(d) but not O(1)

c) O(2d) but not O(d)

d) O(d2d) but not O(2d)


LESSON – 14
Q.1. Consider the process of inserting an element into a Max Heap, where the Max Heap is
represented by an array. Suppose we perform a binary search on the path from the new leaf to the
root to find the position for the newly inserted element, the number of comparisons performed is:
(Gate-2007) (1 Marks)

a) O(log2n) b) O(log2 log2n) c) ϴ(n) d) ϴ(nlog2n)

Q.2. Consider a complete binary tree where the left and the right sub-trees of the root are max-
heaps. The lower bound for the number of operations to convert the tree to a heap is
(GATE-2015) (1 Marks)

a) Ω(logn) b) Ω(n) c) Ω(nlogn) d) Ω(n2)

Ans:
As left and right sub-tree are max-heaps, the upper elements of the node are already maximum.

• We know that max-heapify algo works with 3 nodes i.e.


left-node, right-node and their parent node.
• Here in this question we have to consider these 3 nodes
only.
• So, the time complexity is same with max-heapify, i.e. =
O(logn).
• As here it ask for lower bound ans = a) Ω(logn)

Q.3. An array of 25 distinct elements is to be sorted using quick sort. Assume that the pivot element
is chosen uniformly at random. The probability that the pivot element gets placed in the worst
possible location in the first round of partitioning (rounded off to 2 decimal places) is_____.
(Gate-2019) (1 Marks) [Asked in CoCubes 2020]

Q.4. Which one of the following is the recurrence equation for the worst case time complexity of
the Quick sort algorithm for sorting n(> 2) numbers? In the recurrence equations given in the
options below, c is a constant. (Gate-2015) (1Marks)

(a) T(n) = 2T(n/2) + cn

(b) T(n) = T(n - 1) + T(1) + cn

(c) T(n) = 2T(n - 1) +n

(d) T(n) = T(n/2) + cn

Q.5. You have an array of n elements. Suppose you implement quicksort by always choosing
the central element of the array as the pivot. Then the tightest upper bound for the
worst-case performance is (Gate-2014) (1 Marks)

a) O(n2) b) O(nLogn) c) Theta(nLogn) d) O(n3)


Q.6. 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 the 7, but it is not the 9
(C) The pivot is not the 7, but it could be the 9
(D) Neither the 7 nor the 9 is the pivot.

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

Q.7. Consider the Quick sort algorithm. Suppose there is a procedure for finding a pivot element
which splits the list into two sub-lists each of which contains at least one-fifth of the elements.
Let T(n) be the number of comparisons required to sort n elements. Then (Gate-2008) (2 Marks)
(A) T(n) <= 2T(n/5) +n
(B) T(n) <= T(n/5) + T(4n/5) +n
(C) T(n) <= 2T(4n/5) +n
(D) T(n) <= 2T(n/2) +n

Q. Consider the following array. Which algorithm out of the following options uses the least
number of comparisons (among the array elements) to sort above array in ascending order?
[Asked in Tech Mahindra 2019]

a) Insertion sort
b) Selection sort 23 32 45 69 72 73 89 97
c) Quick sort using the last element as pivot
d) Merge sort

Ans:
a) Insertion sort

Q Assume that the algorithms considered here sort the input sequences in ascending order. If the
input is already in ascending order, which of the following are TRUE? (Gate-2016) (1 Marks)

I. Quick sort runs in ϴ(n2) time


II. Bubble sort runs in ϴ(n2) time
III. Merge_Sort runs in ϴ(n) time
IV. Insertion sort runs in ϴ(n) time

a) I and II only
b) I and III only
c) II and IV only
d) I and IV only
Ans:
d) I and IV only
For bubble sort is nothing is mentioned, consider the default case as algorithm includes the flag.
Here in this question the same thing is assumed.
Q.8 There are n unsorted arrays: A1 , A2 , …. An . Assume that n is odd. Each of A1 , A2 , …. An ,
contains n distinct elements. There are no common elements between any two arrays. The worst-
case time complexity of computing the median of the medians of A1 , A2 , …. An , is ______
(Gate-2019) (1 Marks)
a) O(n)
b) O(nlogn)
c) O(n2)
d) Q(n2logn)

char gpt:
Binary Search:
• What It Is: An algorithm that searches for a target value in a sorted array by repeatedly
dividing the search space in half.
• Data Structure: Works on a sorted array or list.
• How It Works:
• Binary search begins by comparing the target element with the middle element of the
array.
• If the target is smaller than the middle element, the search continues in the left half;
if larger, the search continues in the right half.
• This halving process continues until the target is found or the array is reduced to zero
elements.
• Time Complexity:
• Best case: O(1) (if the middle element is the target).
• Average case and worst case: O(logn) (since it halves the search space at each step).
• Requirement: The array must be sorted before performing a binary search.

Aspect Binary Search Binary Search Tree (BST)


Data Structure Sorted array or list Binary tree
Sorting Requirement Array must be sorted before searching No pre-sorting required
Insertion/Deletion Not applicable (arrays are static) Can insert or delete nodes
dynamically
Search Time O(logn) for a sorted array O(logn) for balanced trees, O(n) for
unbalanced
Space Complexity O(1) (no extra space required) O(n) (space for tree nodes)
Use Cases Efficient search in a sorted list/array Dynamic data structure for search,
insertion, and deletion

Q.9. Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a sequence of 1’s.
The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of
locations in A. The worst-case number of probes performed by an optimal algorithm is_______ .
(Gate-2017) (1 Marks)
Ans:
0’s followed by 1’s means 1st 0’s come then 1’s come.
Ex: 000001111
So, this order is sorted, and for binary search in sorted array time complexity = logn .
So, here time complexity = log231 = 4.95
As number of probes asked, means number of search ask. So total number of search = 5 (we can’t
tell number of searches is 4.59.)
Q Which of the following sorting algorithms in its typical implementation gives best performance
when applied on an array which is sorted or almost sorted (maximum 1 or two elements are
misplaced).
(A) Quick Sort (B) Heap Sort (C) Merge Sort (D) Insertion Sort
Ans:
(D) Insertion sort

Q Consider a situation where swap operation is very costly. Which of the following sorting
algorithms should be preferred so that the number of swap operations are minimized in general?
(A) Heap Sort (B) Selection Sort (C) Insertion Sort (D) Merge Sort

Ans:
(B) Selection Sort
Selection sort is worst in comparisons.

Q.10. You have to sort 1 GB of data with only 100 MB of available main memory. Which sorting
technique will be most appropriate?
(A) Heap sort (B) Merge sort (C) Quick sort (D) Insertion sort

Ans:
(B) Merge sort

Q Which sorting algorithm will take least time when all elements of input array are identical?
Consider typical implementations of sorting algorithms.
(A) Insertion Sort (B) Heap Sort (C) Merge Sort (D) Selection Sort

Ans:
(A) Insertion Sort
Lesson – 15
Q We are given 9 tasks T1, T2.... TS. The execution of each task requires one unit of time. We can
execute one task at a time. Each task Ti has a profit Pi and a deadline di Profit Pi is earned if the
task is completed before the end of the dith unit of time.

Task T1 T2 T3 T4 T5 T6 T7 T8 T9
Profit 15 20 30 18 18 10 23 16 25
Deadline 7 2 5 3 4 5 2 7 3

Are all tasks completed in the schedule that gives maximum profit? (Gate-2005) (2 Marks)
(A) All tasks are completed (B) T1 and T6 are left out
(C) T1 and T8 are left out (D) T4 and T6 are left out
LESSON – 16
Q.1.

Q.2.

Q.3. Suppose the letters a, b, c, d, e, f has probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively.
Which of the following is the Huffman code for the letter a, b, c, d, e, f? (Gate - 2007) (2 Marks)
(A) 0, 10, 110, 1110, 11110, 11111 (B) 11, 10, 011, 010, 001, 000
(C) 11, 10, 01, 001, 0001, 0000 (D) 110, 100, 010, 000, 001, 111

Q.4.
LESSON – 18
Q.1. A networking company uses a compression technique to encode the message before
transmitting over the network. Suppose the message contains the following characters with their
frequency: If the compression technique used is Huffman Coding, how many bits will be saved in
the message?(compare it with ascii code)

(A) 224 (B) 800 (c) 576 (D) 324

Q In question 1, which of the following represents the word “dead”?


(A)1011111100101 (B) 0100000011010
(C) Both A and B (D) None of these
Ans:
(C) Both A and B
Because as we write 0’s and 1’s in either right edge or left edge of tree, the ans we calculate and it’s
1’s complement both are valid ans.

Q.2.

Q.3.
Q.4. Given,a set of 8 files from F1 to F8 with following number of pages find the minimum number
of record movements to merge them into single file?

Q.5. Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30, 35, 50 respectively. They
are to be merged into a single sequence by merging together two sequences at a time. The number
of comparisons that will be needed in the worst case by the optimal algorithm for doing this is____.
(Gate-2014) (2 Marks)

Q.6. Consider the string abbccddeee. Each letter in the string must be assigned a binary code
satisfying the following properties :

1. For any two letters, the code assigned to one letter must not be a prefix of the code
assigned to the other letter.
2. For any two letters of the same frequency, the letter which occurs earlier in the
dictionary order is assigned a code whose length is at most the length of the code
assigned to the other letter.

Among the set of all binary code assignments which satisfy the above two properties, what is the
minimum length of the encoded string? (GATE- 2021)
(a) 23 (b) 25 (c) 21 (d) 30
LESSON – 21
Q.1. Consider two strings A = “qpqrr” and B = “pqprarp”. Let x be the length of the longest
common subsequence (not necessarily contiguous) between A and B and let y be the number of
such longest common subsequences between A and B. Then x + 10y =____ (Gate-2014) (2 Marks)”

Q.2.

Q.3.

Traditional method, no short are available

Q.4.

Traditional method, no short are available


Q.5.
LESSON – 22
Q.1.

Q.2.

Q.3.

Q.4.
Q.5.

(B) option is true and it’s a important understanding. It’s like a bridge in minimum spanning tree.
Bridge means the only option to connect the graph.
Ans : (C)

Q.6.
LESSON – 23
Q.1.

Q.2.

Q.3.

Q.4.

Q.5.
PYQ(YouTube with web sites)
Q.1.

Q.2. Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting
depth-first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited
after visiting u in the traversal. Which of the following statement is always true?

A. {u,v} must be an edge in G, and u is a descendant of v in T


B. {u, v} must be an edge in G, and v is a descendant of u in T
C. If {u, v} Is not an edge in G then u is a leaf in T
D. If {u, v} is not an edge in G then u and v must have the same parent in T

ALGO – 2
Q.1. Consider the depth-first-search of an undirected graph with 3 vertices P, Q, and R. Let
discovery time d(u) represent the time instant when the vertex u is first visited, and finish time f(u)
represent the time instant when the vertex u is last visited. Given that

d(P) =5units f(P) = 12 units


d(Q) = 6 units f(Q) = 10 units
d(R) = 14 unit f(R) = 18 units

Which one of the following statements is TRUE about the graph?

A. There is only one connected component


B. There are two connected components, and P and R are connected
C. There are two connected components, and Q and R are connected
D. There are two connected components, and P and Q are connected

Q.2.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy