ADA Lab Viva Questions
ADA Lab Viva Questions
Viva Questions
1. What is an algorithm?
Answer: Algorithms can be classified into several types based on their design approach:
Divide and Conquer: Divides the problem into smaller subproblems, solves them, and
combines the results. Example: Merge Sort, Quick Sort.
Greedy Algorithm: Makes the locally optimal choice at each stage with the hope of
finding a global optimum. Example: Dijkstra's algorithm.
Dynamic Programming: Solves problems by breaking them down into overlapping
subproblems and storing the results to avoid redundant computations. Example:
Fibonacci sequence, Knapsack problem.
Backtracking: Tries to build a solution incrementally and abandons the solution if it
leads to an invalid state. Example: N-Queens problem, Sudoku solver.
Brute Force: Tries all possible solutions until the correct one is found. Example: Linear
search, Bubble sort.
Answer:
Answer: Big O notation is used to describe the upper bound of the time complexity or space
complexity of an algorithm. It helps in analyzing the worst-case scenario and allows comparison
between different algorithms to choose the most efficient one for large inputs. For example, O(n)
implies linear time complexity, while O(n^2) implies quadratic time complexity.
Answer:
Answer: Divide and Conquer is a strategy where a problem is divided into smaller
subproblems, solved independently, and then combined to form a solution. The subproblems are
usually easier to solve than the original problem.
Answer: Dynamic Programming (DP) is a method used for solving problems by breaking them
down into simpler subproblems. It stores the results of these subproblems to avoid redundant
computations, which significantly improves efficiency. It is useful for optimization problems.
Recursive Fibonacci has overlapping subproblems, which DP can optimize by storing the
results in a table to avoid recalculating them.
Answer: Greedy Algorithms make the best choice at each step with the hope that these choices
will lead to an optimal solution. They do not guarantee an optimal solution for all problems, but
they work well for certain problems.
Given a set of activities with start and finish times, the greedy approach selects the
activity with the earliest finish time that is compatible with the already selected activities.
Answer:
Advantages:
o Simplifies code and reduces the complexity of solving problems.
o Especially useful for problems that have a recursive structure, like tree traversals
or backtracking problems.
Disadvantages:
o May lead to stack overflow if recursion depth is too deep.
o Inefficient in terms of memory usage, as each recursive call requires additional
memory for function calls.
The problem is to place N queens on an NxN chessboard such that no two queens attack
each other. The algorithm places a queen in a position, then recursively tries to place the
next queen. If placing a queen leads to an invalid configuration, it backtracks by
removing the queen and trying a different position.
11. What is the difference between Depth-First Search (DFS) and Breadth-First Search (BFS)?
Answer:
12. What is a greedy algorithm, and how does it differ from dynamic programming?
Answer: A greedy algorithm makes a series of choices that appear to be the best at the
moment, hoping that these local optimal choices will lead to a global optimum. Dynamic
programming, on the other hand, solves a problem by breaking it down into overlapping
subproblems and stores the solutions to these subproblems, avoiding redundant calculations.
13. What are the types of searching algorithms, and how do they differ?
Answer:
Linear Search: Sequentially checks each element until the desired element is found or
the end is reached. Time complexity: O(n).
Binary Search: Searches for an element by repeatedly dividing the sorted array in half.
Time complexity: O(log n).
Hashing: Uses a hash function to map keys to positions in an array, allowing for faster
lookups. Average time complexity: O(1).
14. What is the difference between a graph's adjacency matrix and adjacency list representation?
Answer:
Adjacency Matrix: A 2D array where each cell (i, j) contains a value (1 or 0) indicating
whether there is an edge between vertex i and vertex j.
Dept. of CSE,RIT, Hassan 2024-25 Page 4
Analysis and Design of Algorithms lab BCSL404
Adjacency List: An array of lists. The index represents the vertex, and each list contains
the vertices connected to it.
Adjacency Matrix: Takes O(V^2) space, even if the graph is sparse.
Adjacency List: Takes O(V + E) space, making it more efficient for sparse graphs
24. Recursive and Iterative Binary Search: Which one is more efficient and why?
Answer: Iterative Binary Search is typically more efficient than Recursive Binary Search.
This is because iterative binary search avoids the overhead of recursive function calls and
stack space consumption, resulting in lower memory usage and potentially faster execution,
especially for large datasets.
30. What is the activity selection problem, and how can it be solved using a greedy approach?
Answer: The activity selection problem involves selecting the maximum number of non-
overlapping activities from a set of activities that share a common resource. A greedy
approach sorts activities by their end times and selects the first compatible activity at each
step.
31. Can you discuss the greedy algorithm for finding the minimum spanning tree in a graph?
Answer: Prim’s algorithm and Kruskal’s algorithm are two greedy approaches for finding the
minimum spanning tree in a weighted graph. Prim’s algorithm starts with an arbitrary vertex
and adds the minimum weight edge at each step until all vertices are included, while Kruskal’s
algorithm sorts edges by weight and adds them one by one while avoiding cycles.
32. What is Huffman coding, and how does it utilize a greedy strategy to compress data?
Answer: Huffman coding is a technique for lossless data compression where characters are
represented by variable-length codes. It uses a greedy strategy to assign shorter codes to more
frequent characters.
33. What is dynamic programming, and how does it differ from other methods?
Answer: Dynamic programming breaks down complex problems into smaller, simpler
subproblems and stores solutions to avoid repeating calculations, unlike other methods that
may solve problems directly without reusing solutions.
34. Explain the Fibonacci sequence and how dynamic programming helps calculate Fibonacci
numbers efficiently.
Answer: The Fibonacci sequence is a series where each number is the sum of the two
preceding ones. Dynamic programming stores previously calculated Fibonacci numbers to
avoid recalculating them, making the process faster and more efficient.
37. How does dynamic programming help solve the knapsack problem efficiently?
Answer: Dynamic programming efficiently solves the knapsack problem by considering all
possible combinations of items and weights, storing solutions to subproblems to avoid
recalculating them.
38. What are the pros and cons of Memoization or top-down Approach?
Answer: Pros of Memoization (Top-Down Approach):
Reduces redundant computations by storing previously computed results, improving
efficiency.
Simplifies implementation by leveraging recursion, making code easier to understand
and maintain.
Often leads to a more intuitive and straightforward solution to problems.
Cons of Memoization (Top-Down Approach):
May incur overhead due to function calls and maintaining a cache of results, potentially
increasing memory usage.
Recursion depth limitations in some programming languages may restrict the size of
problems that can be solved.
Requires careful handling of edge cases and ensuring proper initialization of the cache
to avoid errors.]
39. What’s the difference between top-down and bottom-up dynamic programming?
Answer: Top-down (memoization) starts from the top and breaks down the problem
recursively, while bottom-up (tabulation) builds solutions iteratively from the smallest
subproblems.
40. Can you give an example where dynamic programming may not give the best solution?
Answer: Dynamic programming may not provide the best solution for problems lacking
Dept. of CSE,RIT, Hassan 2024-25 Page 8
Analysis and Design of Algorithms lab BCSL404
optimal substructure or overlapping sub problems, like those with changing constraints or non-
linear dependencies.
1) What is Algorithm?
The name 'Algorithm' refers to the sequence of instruction that must be followed to clarify a
problem. The logical description of the instructions which may be executed to perform an
essential function.
Algorithms are usually generated independent of primary languages, i.e., an algorithm can be
achieved in more than one programming language.
The notations are described in terms of methods whose domains are the set of natural numbers N=
{0, 1, 2} Such notations are convenient for defining the worst-case running time function T(n).
Assume that we have a file on n elements. By applying a selection sort, the first item is compared
Dept. of CSE,RIT, Hassan 2024-25 Page 9
Analysis and Design of Algorithms lab BCSL404
with all remaining (n-1) elements. The smallest item is placed at the first location. Again, the
second item is compared with the remaining (n-1) elements. At the time of the comparison, the
smaller item is swapped with a bigger item. Similarly, the entire array is checked for smallest
component, and then swapping is done accordingly. Here we need n-1 passes or repetition to
rearrange the data completely.
7) What is NP-Complete?
An NP-Complete problem is a problem in NP that is as difficult as any other trouble in this
class because any other dispute in NP can be decreased to it in Polynomial-time.
12) Explain the algorithms for Merge sort and give a suitable
example.
The basic concept of merge sort has split the list into two smaller sub-lists of relatively equal size.
Recursively repeat this method until only one item is left in the sub-list. After this, various sorted
sub-lists are combined to form a sorted parent list. This method goes on recursively till the
original sorted list arrived.
Advantages
1. It is an easy and conventional technique of searching for information. The linear or sequential name
implies which the elements are stored in a systematic manner.
2. The item in the list can be in any order. i.e., the linear search can be tested on the sorted or unsorted
linear data structure.
Disadvantages
1. This procedure is insufficient when a large number of item is present in the list.
2. It consumes more time and decreases the retrieval rate of the system.