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

Algorithm_2k22_23_24

Uploaded by

blackcreator420
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Algorithm_2k22_23_24

Uploaded by

blackcreator420
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2022

i) Which of the following is asymptotically smallest?


Answer: b) log n
Explanation: Logarithmic growth is slower than linear, linear-logarithmic, or
exponential growth.

ii) Which following statement is true?


Answer: b) All trees are graphs
Explanation: A tree is a specific type of graph that is connected and acyclic.
iii) Which of the following sorting algorithms is the fastest?
Answer: b) Quick sort
Explanation: Quick sort is often faster in practice due to its efficient average-case performance
(O(n log n)) and good cache behavior.
iv) 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?
Answer: b) 2
Explanation: The array becomes sorted after two passes: one to swap 4 and 3, and another to
confirm sorting.
v) Choose the incorrect statement about merge sort from the following:
Answer: b) it is an adaptive algorithm
Explanation: Merge sort is not adaptive because it always divides the array regardless of its initial
order.
vi) What is a randomized QuickSort?
Answer: d) A random number is generated which is used as the pivot
Explanation: Randomized QuickSort selects a random pivot to improve performance by reducing
the likelihood of worst-case scenarios.
vii) Which of the following algorithm implementations is similar to that of an insertion sort?
Answer: a) Binary heap
Explanation: Binary heaps involve inserting elements in a similar fashion to insertion sort
(heapifying during insertion).
viii) Which of the following is true?
Answer: a) Prim's algorithm initializes with a vertex
Explanation: Prim's algorithm begins with a single vertex and grows the minimum spanning tree
by adding edges.
ix) Given an array arr (45,77,89,90,94,99,100) and key 99; what are the mid values in the first
and second levels of recursion?
Answer: a) 90 and 99
Explanation: In the first level, the middle is 90. In the second level, the middle of the subarray is
99.
x) What is a Rabin and Karp Algorithm?
Answer: a) String Matching Algorithm
Explanation: Rabin-Karp uses hashing to search for patterns in strings.
xi) What is the worst-case time complexity of Prim's algorithm if an adjacency matrix is used?
Answer: b) O(V²)
Explanation: With an adjacency matrix, iterating over all vertices for each vertex results in O(V²).
xii) Floyd Warshall Algorithm used to solve the shortest path problem has a time complexity
of:
Answer: b) O(V³)
Explanation: Floyd-Warshall computes all-pairs shortest paths in O(V³) using dynamic
programming.

i) Running time complexity of heap sort is ___________


Answer: O(n log n)
Explanation: Heap sort builds a heap in O(n) and then performs O(log n) heapify
operations for each element, leading to O(n log n).

ii) LIFO scheme is used in _____________ data structure.


Answer: Stack
Explanation: Last-In-First-Out (LIFO) is the fundamental behavior of stacks.
iii) An algorithm is a ____________ to solve a problem.
Answer: step-by-step procedure
Explanation: An algorithm provides a systematic approach to solving computational problems.
iv) Element of queue will be deleted from _____________ end.
Answer: front
Explanation: Queues follow the First-In-First-Out (FIFO) principle where deletions occur from the
front.
v) Worst case for linear search is ________________.
Answer: O(n)
Explanation: In the worst case, the element to be found is at the last position or not present,
requiring n comparisons.
vi) Each node in a binary tree has at most _______________ child nodes.
Answer: 2
Explanation: A binary tree allows at most two children per node: left and right.
vii) O-notation provides an asymptotic ________________.
Answer: upper bound
Explanation: Big O notation describes the worst-case upper limit of an algorithm’s running time or
space.
viii) Deletion in heap requires _____________ number of arrays.
Answer: O(log n)
Explanation: Deletion involves replacing the root with the last element and heapifying, which
takes O(log n).
ix) Time complexity of insertion sort is ________________________.
Answer: O(n²)
Explanation: In the worst case, every element has to be compared with all previous elements,
leading to O(n²).
x) The process where two rotations are required to balance a tree is called
___________________.
Answer: double rotation
Explanation: Double rotation occurs in AVL trees when balancing requires two steps: a left
rotation followed by a right rotation (or vice versa).
xi) Big O notation derives the ___________ case.
Answer: worst
Explanation: Big O describes the maximum amount of time or space an algorithm can take.
xii) ________________ notation gives the lower bound of the function f(n).
Answer: Omega (Ω)
Explanation: Ω-notation describes the best-case scenario or minimum time complexity.
xiii) Balance factor of AVL tree is _____________________.
Answer: -1, 0, or 1
Explanation: The balance factor is the height difference between the left and right subtrees, limited
to these values in an AVL tree.
xiv) _____________ is the formula used in Euclid's algorithm for finding the greatest common
divisor of two numbers.
Answer: gcd(a, b) = gcd(b, a % b)
Explanation: Euclid’s algorithm recursively uses the modulo operation to find the GCD.
xv) If an algorithm takes O(n²), it is faster for sufficiently larger n than if it had taken
_______________________.
Answer: O(n³)
Explanation: Quadratic time complexity (O(n²)) is asymptotically smaller than cubic time
complexity (O(n³)).

i) Write one difference between path and cycle.


Answer: A path is a sequence of vertices with no repeated vertices, while a cycle is a
path that starts and ends at the same vertex, forming a loop.

ii) Define insertion sort.


Answer: Insertion sort is a sorting algorithm where elements are picked one by one and placed in
their correct position in a sorted portion of the array.
iii) What is another name of height-balanced binary search tree?
Answer: AVL Tree.
iv) What is the Greedy method?
Answer: The Greedy method is an algorithmic approach that makes the locally optimal choice at
each step to achieve the global optimum.
v) Mention the various types of searching techniques in C.
Answer: Linear search, binary search, hashing, and interpolation search.
vi) What is Linear Time Sorting?
Answer: Linear time sorting algorithms, such as counting sort and radix sort, sort data in O(n) time
by not comparing elements directly.
vii) What is Collision?
Answer: A collision occurs in hashing when two keys hash to the same index in the hash table.
viii) What is the advantage of linked list representation of binary trees over arrays?
Answer: Linked list representation dynamically allocates memory, making it space-efficient and
suitable for trees with uneven or sparse structures.
ix) What is cut vertex?
Answer: A cut vertex is a vertex whose removal increases the number of connected components in
a graph.
x) What is direct addressing?
Answer: Direct addressing uses an array, where the index corresponds directly to the key, for
storing data without hashing.
xi) What is data abstraction?
Answer: Data abstraction is the process of hiding implementation details and exposing only the
functionality of data structures or objects.
xii) What is sink in a graph?
Answer: A sink is a vertex in a directed graph that has no outgoing edges but may have incoming
edges.
xiii) Define in-degree of a graph.
Answer: The in-degree of a vertex in a directed graph is the number of edges directed toward it.
xiv) What is pivot in Quick Sort?
Answer: A pivot is an element chosen in Quick Sort around which the array is partitioned into two
subarrays.

i) Which is the best searching algorithm and why?


Answer: Binary search is the best searching algorithm for sorted arrays because it
reduces the search space by half in each step, achieving a time complexity of O(log n).
However, for unsorted data, hashing is better due to its average O(1) time
complexity.
ii) Describe best case time complexity.
Answer: Best case time complexity represents the minimum time required for an algorithm to
execute. For example, in linear search, the best case occurs when the element is found at the first
position, with a time complexity of O(1).

iii) Define 'Big Theta' Notation.


Answer: Big Theta (Θ) notation provides a tight bound on an algorithm's time complexity,
indicating that the algorithm takes the same time asymptotically in both the best and worst cases.
Example: If T(n) = Θ(n²), the time complexity is bounded both above and below by n².

iv) What do you mean by sorting?


Answer: Sorting is the process of arranging elements in a specific order, such as ascending or
descending. Examples include sorting numbers in ascending order or arranging names
alphabetically.

v) Define Dynamic Programming algorithm.


Answer: Dynamic Programming (DP) is an optimization technique that solves problems by
breaking them into overlapping subproblems, solving each subproblem only once, and storing their
results.
Example: The Fibonacci sequence can be solved efficiently using DP.

vi) Why is a sorting technique called stable?


Answer: A sorting technique is called stable if it preserves the relative order of elements with equal
keys. For example, in stable sort algorithms like merge sort, if two elements have the same value,
their order remains the same as in the input.

vii) Which one is more efficient - Insertion Sort or Merge Sort?


Answer: Merge sort is more efficient than insertion sort for large datasets due to its O(n log n)
time complexity compared to insertion sort's O(n²) in the worst case. However, for small datasets,
insertion sort can be faster due to its simpler implementation.

viii) Write the advantages of height balancing.


Answer: Height balancing, as in AVL trees, ensures:
1. O(log n) time complexity for search, insertion, and deletion operations.
2. Avoidance of skewed trees, which can degrade performance.

ix) What is Omega Notation & give example?


Answer: Omega (Ω) notation represents the lower bound of an algorithm's time complexity,
indicating the best case.
Example: In linear search, Ω(1) occurs when the desired element is at the first position.
x) What is inorder traversal in BST?
Answer: Inorder traversal in a Binary Search Tree (BST) visits nodes in ascending order:
1. Traverse the left subtree.
2. Visit the root.
3. Traverse the right subtree.
Example: For a BST with nodes 10, 20, and 30, the inorder traversal will be: 10 → 20 → 30.

xi) Describe spanning tree with an example.


Answer: A spanning tree is a subgraph of a graph that includes all vertices with the minimum
number of edges required to form a tree (no cycles).
Example: For a graph with vertices {A, B, C, D} and edges {AB, AC, BD, CD}, a spanning tree
could be edges {AB, BD, CD}.

xii) What do you mean by space complexity?


Answer: Space complexity refers to the amount of memory an algorithm uses, including the input
data size, auxiliary variables, and recursion stack.
Example: Merge sort has a space complexity of O(n) due to the additional space required for
temporary arrays.
2023
i) ADT full form
Answer: c) Abstract Data Type
Explanation: ADT defines a data structure in terms of operations, hiding
implementation details.

ii) In a multiset, elements are arranged in an unordered manner, with elements having a
multiplicity of
Answer: b) More than one
Explanation: A multiset allows duplicate elements, with multiplicity indicating their count.

iii) Binary Search can be categorized into which of the following?


Answer: b) Divide and conquer
Explanation: Binary search splits the problem into smaller subproblems recursively.

iv) What is the time complexity of binary search with iteration?


Answer: b) O(log n)
Explanation: Binary search divides the search range in half at each step.

v) Which of the following is/are property/properties of a dynamic programming problem?


Answer: d) Both optimal substructure and overlapping sub-problems
Explanation: Dynamic programming uses these properties for efficient solutions.

vi) A circuit that does not repeat vertices is called


Answer: b) a path
Explanation: A path traverses vertices without revisiting any of them.

vii) What is the time complexity of Kruskal's algorithm (E is the number of edges and V is the
number of vertices of input graph)?
Answer: b) O(E log V)
Explanation: Kruskal's algorithm sorts edges and uses union-find, leading to this complexity.

viii) Time complexity of insertion sort is


Answer: b) Quadratic
Explanation: Insertion sort has O(n²) complexity in the worst case.
ix) If 4xyz/, number of elements of the power set of A is:
Answer: b) 8
Explanation: The power set has 2n elements for a set of size n; here 23=8.

x) Time complexity depends on


Answer: b) Run time
Explanation: Time complexity measures the algorithm's performance during execution.

xi) Which of the following is a linear data structure?


Answer: a) Array
Explanation: Arrays store elements in a sequential, linear order.

xii) What is the maximum number of swaps that can be performed in the Selection Sort
algorithm?
Answer: a) n-1
Explanation: Selection sort swaps once per pass, totaling n−1 swaps.

xiii) Which of the following has search efficiency of O(1)?


Answer: c) Hash Table
Explanation: Hash tables provide O(1) average-case search due to direct addressing.

xiv) MST full form is:


Answer: c) Minimum Spanning Tree
Explanation: MST connects all vertices with the minimum total edge weight.

xv) If the array is already sorted, which of these algorithms will exhibit the best performance?
Answer: b) Insertion Sort
Explanation: Insertion sort runs in O(n) for already sorted arrays.

xvi) What is the maximum number of swaps that can be performed in the Bubble Sort
algorithm?
Answer: d) n(n-1)/2
Explanation: Bubble sort swaps every pair in the worst case, leading to this many swaps.

xvii) Two main measures of the efficiency of an algorithm are?


Answer: a) Time and space complexity
Explanation: Efficiency depends on execution time and memory usage.
xviii) If several elements are competing for the same bucket in the hash table, what is it
called?
Answer: c) Collision
Explanation: A collision occurs when multiple keys hash to the same index.

xix) Quick Sort algorithm is a


Answer: c) Divide & Conquer Algorithm
Explanation: Quick Sort partitions the array and solves subarrays recursively.

xx) Name of the data structure used by recursion is:


Answer: a) Stack
Explanation: Recursion uses the call stack to store intermediate states.

xxi) DAG full form is:


Answer: b) Direct Acyclic Graph
Explanation: A DAG is a graph with directed edges and no cycles.

xxii) Dijkstra's algorithm is known as


Answer: d) Single Source shortest path
Explanation: It computes the shortest path from a single source to all nodes.

xxiii) Which of the following is true?


Answer: a) Prim's algorithm initializes with a vertex
Explanation: Prim's algorithm starts from an arbitrary vertex.

xxiv) Which of the following algorithms are used to find the shortest path from a source node
to all other nodes in a weighted graph?
Answer: b) Dijkstra's Algorithm
Explanation: Dijkstra's algorithm efficiently finds shortest paths in weighted graphs.
2024
i) LIFO scheme is used in data structure.
Answer: a) stack
Explanation: LIFO (Last In, First Out) is the principle used in stacks, where the last
element inserted is the first to be removed.

ii) Which of the following sorting algorithms has a worst-case time complexity of O(n log n)?
Answer: b) Merge sort
Explanation: Merge sort has O(n log n) worst-case time complexity due to its divide-and-conquer
approach.

iii) O-notation provides an asymptotic


Answer: a) upper bound
Explanation: Big-O notation describes the upper bound or worst-case performance of an algorithm.

iv) Time complexity of worst case for Linear Search is


Answer: c) O(n)
Explanation: In the worst case, linear search checks every element in the array, resulting in O(n)
time complexity.

v) Which of the following algorithm design techniques is used in the Merge sort algorithm?
Answer: c) Divide and conquer
Explanation: Merge sort divides the problem into smaller subproblems and conquers them
recursively.

vi) Which of the following is false in the case of a spanning tree of a graph G?
Answer: d) It can be either cyclic or acyclic
Explanation: A spanning tree is always acyclic by definition.

vii) Which of the following is true?


Answer: a) Prim's algorithm initializes with a vertex
Explanation: Prim's algorithm starts from an arbitrary vertex and grows the spanning tree.

viii) Topological sort can be applied to which of the following graphs?


Answer: d) Directed Acyclic Graphs
Explanation: Topological sorting is only applicable to directed acyclic graphs (DAGs).
ix) A Circuit that does not repeat vertices is called
Answer: b) path
Explanation: A path in a graph is a sequence of vertices where no vertex is repeated.

x) What is a hash table?


Answer: b) A structure that maps keys to values
Explanation: A hash table stores key-value pairs and uses a hash function to map keys to specific
indices.

xi) Dijkstra's algorithm is used for finding


Answer: b) shortest path
Explanation: Dijkstra’s algorithm finds the shortest path from a source node to all other nodes in a
weighted graph.

xii) A graph is called a ____ if it is a connected acyclic graph.


Answer: c) tree
Explanation: A tree is a connected acyclic graph, meaning it has no cycles and is fully connected.

xiii) A binary tree is balanced if the difference of height between left and right sub-tree is not
more than
Answer: b) 1
Explanation: A balanced binary tree maintains a height difference of at most 1 between the left and
right subtrees.

xiv) The following numbers are inserted into an empty binary search tree in the given order:
10, 1, 3, 5, 15, 12, 16. What is the number of leaf nodes of the binary search tree?
Answer: b) 4
Explanation: After inserting the numbers, the leaf nodes are the nodes with no children, and there
are 4 leaf nodes.

xv) What is a Rabin and Karp Algorithm?


Answer: a) String Matching Algorithm
Explanation: The Rabin-Karp algorithm is used for searching a pattern in a string using hashing.

You might also like