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

ADA Lab Viva Questions

The document provides a comprehensive overview of algorithms, including definitions, types, and key concepts such as time complexity, space complexity, and various algorithmic strategies like Divide and Conquer, Greedy Algorithms, and Dynamic Programming. It also discusses specific algorithms and their applications, such as Dijkstra's algorithm and Huffman coding, as well as the differences between searching algorithms like Linear Search and Binary Search. Additionally, it covers the importance of Big O notation and the distinctions between recursive and iterative approaches.

Uploaded by

n.shruthi0201
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)
187 views

ADA Lab Viva Questions

The document provides a comprehensive overview of algorithms, including definitions, types, and key concepts such as time complexity, space complexity, and various algorithmic strategies like Divide and Conquer, Greedy Algorithms, and Dynamic Programming. It also discusses specific algorithms and their applications, such as Dijkstra's algorithm and Huffman coding, as well as the differences between searching algorithms like Linear Search and Binary Search. Additionally, it covers the importance of Big O notation and the distinctions between recursive and iterative approaches.

Uploaded by

n.shruthi0201
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/ 11

Analysis and Design of Algorithms lab BCSL404

Viva Questions
1. What is an algorithm?

Answer: An algorithm is a well-defined, step-by-step procedure or set of rules to solve a


specific problem or perform a computation. It takes an input, processes it, and produces an
output. The algorithm must be finite, unambiguous, and must terminate after a certain number of
steps.

2. What are the different types of algorithms?

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.

3. What is the difference between time complexity and space complexity?

Answer:

 Time Complexity: Refers to the amount of time an algorithm takes to complete as a


function of the size of the input. It is often expressed using Big O notation (e.g., O(n),
O(n^2)).
 Space Complexity: Refers to the amount of memory an algorithm requires to complete
as a function of the size of the input. It also is often expressed using Big O notation (e.g.,
O(1), O(n)).

Dept. of CSE,RIT, Hassan 2024-25 Page 1


Analysis and Design of Algorithms lab BCSL404

4. What is the significance of Big O notation?

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.

5. What is the difference between a recursive and an iterative algorithm?

Answer:

 Recursive Algorithm: A recursive algorithm solves a problem by calling itself with


smaller inputs until it reaches a base case. Example: Factorial calculation.
 Iterative Algorithm: An iterative algorithm solves a problem by repeating a set of
instructions until a certain condition is met, usually with loops like for or while.
Example: Linear search.

6. What is Divide and Conquer? Explain with an example.

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.

Example: Merge Sort:

1. Divide the array into two halves.


2. Recursively sort each half.
3. Merge the sorted halves to get the final sorted array.

7. What is Dynamic Programming?

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.

Example: Fibonacci Sequence:

Dept. of CSE,RIT, Hassan 2024-25 Page 2


Analysis and Design of Algorithms lab BCSL404

 Recursive Fibonacci has overlapping subproblems, which DP can optimize by storing the
results in a table to avoid recalculating them.

8. Explain the concept of Greedy Algorithms with an example.

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.

Example: Activity Selection Problem:

 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.

9. What are the advantages and disadvantages of using Recursion?

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.

10. Explain the concept of Backtracking with an example.

Answer: Backtracking is a technique used to find solutions to problems by trying possible


candidates and abandoning them if they fail to satisfy the constraints.

Example: N-Queens Problem:

 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.

Dept. of CSE,RIT, Hassan 2024-25 Page 3


Analysis and Design of Algorithms lab BCSL404

11. What is the difference between Depth-First Search (DFS) and Breadth-First Search (BFS)?

Answer:

 Depth-First Search (DFS): Explores as far as possible along a branch before


backtracking. It uses a stack data structure (either implicitly through recursion or
explicitly using a stack).
 Breadth-First Search (BFS): Explores all neighbors at the present depth before moving
on to nodes at the next depth level. It uses a queue data structure.

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.

 Greedy Algorithms: Make the best choice at the moment.


 Dynamic Programming: Solves problems by solving subproblems and using the results
to solve the overall problem.

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

15. What is a sorting algorithm?


Answer: A sorting algorithm is a method used to arrange elements in a specific order, often
from smallest to largest or vice versa, making data easier to manage and search.

16. What are the different types of sorting algorithms?


Answer: There are two types of Sorting algorithms: Comparison based sorting algorithms and
non-comparison-based sorting algorithms. Comparison based sorting algorithms
include Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort, etc.
and non-comparison-based sorting algorithms include Radix Sort, Counting Sort and Bucket
Sort.

17. What are the different types of searching algorithms?


Answer: Searching algorithms include Linear Search, Binary Search, Depth-First Search
(DFS), Breadth-First Search (BFS), and Hashing, each with its own approach to find elements.

18. Explain Linear Search and its time complexity.


Answer: Linear Search checks each element in a list one by one until finding the target or
reaching the end. Its time complexity is O(n) in the worst case.

19. How does Binary Search work?


Answer: Binary Search divides a sorted array in half repeatedly, narrowing down the search
space by comparing the target with the mid until finding the target or exhausting the elements.
Its time complexity is O(log n).

20. What are the requirements for using Binary Search?


Answer: Binary Search requires a sorted array and the ability to access elements by index for
efficient traversal.

21. Explain why complexity of Binary search is O (log2n) ?


Answer: Binary search halves the search space with each step, reducing the number of
elements to be searched by half each time. This logarithmic reduction results in a time
Dept. of CSE,RIT, Hassan 2024-25 Page 5
Analysis and Design of Algorithms lab BCSL404
complexity of O(log2n), where n is the number of elements in the sorted array.

22. How does Hashing work in searching?


Answer: Hashing uses a hash function to compute an index for each element, allowing for
constant-time search operations in the average case by storing elements in a hash table.

23. Compare Linear Search and Binary Search.


Answer: Linear Search checks elements sequentially, while Binary Search halves the search
space with each step, making it more efficient for sorted data with a time complexity of O(log
n).

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.

25. Why use binary search if there is a ternary search?


Answer: Binary search is preferred for finding specific values in sorted arrays, as it divides
the search space in half with each step, resulting in efficient searches with a time complexity
of O(log2n). Binary Search is useful for finding maximum or minimum value in a Monotonic
function whereas Ternary search is useful for finding the maximum or minimum value in a
unimodal function. Also, the time complexity of Ternary Search is O(2 * log3N) which is
greater than O(log2N).

26. When is each searching algorithm most appropriate to use?


Answer: Choose the appropriate searching algorithm based on factors like data structure, data
size, and desired search efficiency, such as Binary Search for sorted arrays and Hashing for
constant-time searches.

27. What is a greedy algorithm?


Answer: A greedy algorithm makes locally optimal choices at each step with the hope of
finding a global optimum solution.

28. What is greedy algorithm used for?


Answer: Greedy algorithms are primarily used for optimization problems where making
Dept. of CSE,RIT, Hassan 2024-25 Page 6
Analysis and Design of Algorithms lab BCSL404
locally optimal choices at each step leads to finding a globally optimal solution. They find
applications in various domains such as scheduling, routing, resource allocation, and
combinatorial optimization.

29. Explain Dijkstra’s algorithm and its application.


Answer: Dijkstra’s algorithm finds the shortest path from a starting node to all other nodes in
a weighted graph. It’s commonly used in routing and network optimization problems.

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.

Dept. of CSE,RIT, Hassan 2024-25 Page 7


Analysis and Design of Algorithms lab BCSL404
35. What kinds of problems are suitable for dynamic programming solutions?
Answer: Dynamic programming works well for problems with overlapping subproblems and
optimal substructure, meaning solutions can be built from smaller optimal solutions.

36. What is memoization in dynamic programming, and why is it useful?


Answer: Memoization involves storing previously calculated results to avoid redundant
computations in recursive algorithms, saving time and improving efficiency. Memoization is
used in Top-down approach.

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.

2) What is Asymptotic Notation?


A way to represents the behavior of functions in the limit or without bounds.

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).

It can also be extended to the domain of the real number.

3) What is the time complexity of Algorithm?


The time complexity of an algorithm denoted the total time needed by the program to run to
completion. It is generally expressed by using the big O notation.

4) Explain the algorithms for Bubble sort and give a suitable


example.
In bubble sort technique the list is split into two sub-lists sorted and unsorted. The smallest
component is bubbled from unsorted sub-list. After moving the smallest component, the
imaginary wall moves one element ahead. The bubble sort was initially written to bubble up the
highest item in the list. But there is no difference whether the highest / lowest item is bubbled.
This technique is simple to understand but time- consuming. In this type, two successive
components are compared, and swapping is done. Thus, step-by- step entire array items are
checked, given a list of 'n' elements the bubble sort needed up to n-1 passes to sort the record.

5) Explain the algorithm for selection sort and give a suitable


example.
In selection sort, the list is split into two sub-lists sorted and unsorted. These two lists are split by
an imaginary wall. We find the smallest item from unsorted sub-list and swap it to the starting.
And the wall moves one item ahead, as the sorted file is increases and an unsorted file is
decreased.

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.

6)Explain the algorithms for QUICK sort (partition


exchange sort) and give a suitable example.
Quicksort is based on division. It is also called a partition exchange sorting. The basic concept of
quick sort method is to pick one item from an array and rearranges the remaining item around it.
This element split the main list into two sublists. This chosen item is known as a pivot. Once the
pivot is selected, then it shifts all the components less than pivot to the left of value pivot, and all
the items higher than the pivot are shifted to the right side. This process of choosing pivot and
division the list is tested recursively until sub-lists consisting of only one element.

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.

8) Differentiate Time Efficiency and Space Efficiency?


Time Efficiency measured by estimate the number of times the essential algorithms functions are
executed. Space Efficiency is measured by calculating the number of additional memory units
consumed by the algorithm.

9) What is the Order of Algorithm?


The order of algorithm is standard documentation of an algorithm that has been developed to
represent a task that bound the estimated time for algorithms. The order of an algorithm is a
method of defining its efficiency. It is commonly referred to as O-notation.

10) What is Brute Force?


Brute Force is a straightforward method for solving problem, usually directly based on the
problem's statement and descriptions of the concepts involved.

11) What are the various criteria used to improve the


effectiveness of the algorithm?
Input- Zero or more quantities are externally provided.

Output- At least one quantity is composed

Dept. of CSE,RIT, Hassan 2024-25 Page 10


Analysis and Design of Algorithms lab BCSL404
Definiteness- Each instruction is simple and unambiguous
Finiteness- If we trace out the instructions of an algorithm, then for all step the algorithm
complete after a finite number of steps

Effectiveness- Every instruction must be elementary.

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.

13) What is a Linear Search?


Linear search method is also called a sequential search technique. The linear search is a technique
of searching an item in a list in sequence. In this technique, the array is searched for the required
item from the starting of the list/array or the last component to the first component of the array
and continues until the element is found or the entire list/array has been searched.

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.

Time complexity: O(n)

14) What is Binary Search?


Binary search is higher than the linear search. However, it cannot be tested on the unsorted data
structure. The binary search is based on the method divide-and-conquer. The binary search begins
by testing the data in the middle component of the array. This determines an object is whether in
the first half or second half. If the object is in the first half, we do not need to check the second
half and if it is in the second half no need to check in first half. Similarly, we repeat this
procedure until we find an object in the list or not found from the list. Here we need three
variables to identify first, last, and middle elements.

To implement a binary search technique, the item must be in sorted order.

Dept. of CSE,RIT, Hassan 2024-25 Page 11

You might also like