Algorithm Viva Q&A – A Complete Guide for Computer Science Students

Algorithm Viva Questions and Answers

Algorithm Viva Questions and Detailed Answers

2. Divide and Conquer Approach

Q1. What is the Divide and Conquer strategy?

It is an algorithmic paradigm that breaks a problem into subproblems, solves them recursively, and then combines their results. Examples include Merge Sort, Quick Sort, and Binary Search.

Q2. Explain Finding Minimum and Maximum using Divide and Conquer.

The array is split into halves. Recursively, min and max of each half are found, and finally the overall min and max are determined using a comparison of results from the halves.

Q3. Describe Merge Sort.

Merge Sort divides the list into halves, recursively sorts them, and merges the sorted halves. It has a time complexity of O(n log n) and is stable but not in-place.

Q4. Explain Quick Sort.

Quick Sort selects a pivot, partitions the array such that elements lesser are on the left and greater on the right, then recursively sorts the partitions. Its average time complexity is O(n log n).

Q5. How does Binary Search work?

Binary Search works on sorted arrays by repeatedly dividing the search interval in half and comparing the target value to the middle element. Time complexity is O(log n).

3. Greedy Method Approach

Q6. What is the Greedy Method?

The greedy method builds a solution step by step, choosing the best local option at each step, hoping to find the global optimum.

Q7. Explain Dijkstra's Algorithm.

Dijkstra's algorithm finds the shortest path from a single source to all vertices in a graph with non-negative weights. It uses a priority queue to select the next minimum distance vertex.

Q8. What is the Fractional Knapsack problem?

It is a greedy problem where items can be broken to maximize profit. Sort items by value/weight ratio and take portions of items to fill the knapsack to capacity.

Q9. Describe Job Sequencing with Deadlines.

This problem involves scheduling jobs to maximize profit such that each job is completed within its deadline. Jobs are sorted by profit and scheduled if a free slot is available before the deadline.

Q10. What are Kruskal and Prim’s Algorithms?

Kruskal's algorithm selects the smallest weight edge that doesn't form a cycle. Prim's algorithm starts from a node and grows the spanning tree by adding the smallest edge connecting the tree to a new vertex.

4. Dynamic Programming Approach

Q11. Explain Bellman-Ford Algorithm.

Bellman-Ford finds shortest paths from a single source to all vertices, even with negative weights. It relaxes all edges repeatedly for V-1 times.

Q12. Describe Floyd-Warshall Algorithm.

Floyd-Warshall finds shortest paths between all pairs of vertices. It uses a dynamic programming table that is updated considering each vertex as an intermediate point.

Q13. What is the 0/1 Knapsack Problem?

Items cannot be broken and must be taken fully or not at all. A DP table is built where each cell represents the maximum value with a given weight and items subset.

Q14. Explain the Travelling Salesperson Problem (TSP).

TSP seeks the shortest route visiting all cities exactly once and returning to the origin. It is NP-Hard and solved using dynamic programming or approximation methods.

Q15. What is the Longest Common Subsequence (LCS)?

LCS finds the longest sequence present in the same order in both strings. It uses a DP table to compare characters and store intermediate results.

5. Backtracking and Branch and Bound

Q16. Describe the N-Queen Problem.

The goal is to place N queens on an N×N chessboard such that no two queens threaten each other. Backtracking is used to explore possible placements and backtrack on conflict.

Q17. What is the Sum of Subsets problem?

It involves finding subsets of numbers that sum up to a given target. Backtracking explores all subsets and prunes branches where the sum exceeds the target.

Q18. Explain Graph Coloring using Backtracking.

Graph coloring assigns colors to vertices so that no two adjacent vertices have the same color. Backtracking tries all color assignments and backtracks on conflicts.

6. String Matching Algorithms

Q19. Describe the NaΓ―ve String Matching Algorithm.

This brute-force method compares the pattern with all substrings of the text. Time complexity is O(nm), where n is text length and m is pattern length.

Q20. Explain Rabin-Karp Algorithm.

It uses hashing to find patterns in text. It calculates the hash of the pattern and substrings, reducing comparisons with rolling hash functions. Time complexity is O(n+m) on average.

Q21. Describe the Knuth-Morris-Pratt (KMP) Algorithm.

KMP avoids redundant comparisons by using a prefix table (LPS array) to skip characters when a mismatch occurs. It has a time complexity of O(n).


Prepared by Dr. Radhika Nanda | HOD – Computer Engineering

Comments

Popular posts from this blog

Analysis of algorithms viva questions / Interview questions - set1 /sorting algorithms

Operating System Viva questions/interview questions 2025

Recommendation System viva questions/ Interview questions