Algo
Algo
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.
Where:
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)
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]
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)
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]
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]
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.
ii)
Cost of this is = O(1)
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.
Search for it
To do traves the max heap total time complexity ?
To insert an element in max heap time complexity?
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]
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)
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)
Ans:
As left and right sub-tree are max-heaps, the upper elements of the node are already maximum.
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)
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)
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)
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.
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)
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.
Q.4.
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?
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
Q.2.