100 questions and answers related to Algorithms (Computer Science)
By Prasanta Nandi
1-10: Introduction to Algorithms
What is an algorithm?
- An algorithm is a step-by-step procedure or set of rules to solve a specific problem or perform a task.
What are the characteristics of a good algorithm?
- Correctness, efficiency, finiteness, definiteness, input, and output.
What is the difference between an algorithm and a program?
- An algorithm is a conceptual procedure, while a program is an implementation of an algorithm using a programming language.
What are the types of algorithms?
- Sorting, searching, graph, dynamic programming, greedy, and divide & conquer algorithms.
What is algorithm complexity?
- It is a measure of the performance of an algorithm in terms of time (time complexity) and space (space complexity).
What is time complexity?
- It describes how the running time of an algorithm increases with the input size.
What is space complexity?
- It describes the amount of memory an algorithm uses as a function of input size.
What is an asymptotic notation?
- A notation used to describe the complexity of an algorithm (Big O, Big Theta, Big Omega).
What is Big-O notation?
- It represents the worst-case complexity of an algorithm.
What is the best-case and worst-case complexity?
- Best-case: the minimum time an algorithm takes.
- Worst-case: the maximum time an algorithm takes.
11-20: Searching Algorithms
What is a searching algorithm?
- An algorithm used to find an element in a data structure.
What is linear search?
- A simple searching algorithm that checks each element one by one.
What is the time complexity of linear search?
- O(n) in the worst and average cases.
What is binary search?
- A searching algorithm that works on sorted arrays by dividing the search space into halves.
What is the time complexity of binary search?
- O(log n) in the worst case.
What is the main difference between linear search and binary search?
- Linear search works on both sorted and unsorted arrays, while binary search only works on sorted arrays.
What is interpolation search?
- A search algorithm that estimates the position of a target value based on the distribution of data.
What is the time complexity of interpolation search?
- O(log log n) in the best case, but O(n) in the worst case.
Which searching algorithm is best for a small dataset?
- Linear search is efficient for small datasets.
Which searching algorithm is best for large sorted datasets?
- Binary search is efficient for large sorted datasets.
21-30: Sorting Algorithms
What is a sorting algorithm?
- An algorithm that arranges elements in a specific order (ascending or descending).
What is Bubble Sort?
- A simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order.
What is the time complexity of Bubble Sort?
- O(n²) in the worst and average cases.
What is Selection Sort?
- A sorting algorithm that selects the smallest element and places it in its correct position.
What is the time complexity of Selection Sort?
- O(n²) in all cases.
What is Insertion Sort?
- A sorting algorithm that builds the sorted list one element at a time by inserting elements into their correct positions.
What is the time complexity of Insertion Sort?
- O(n²) in the worst case and O(n) in the best case.
What is Merge Sort?
- A divide & conquer sorting algorithm that splits the array into halves and merges them in sorted order.
What is the time complexity of Merge Sort?
- O(n log n) in all cases.
What is Quick Sort?
- A sorting algorithm that selects a pivot and partitions the array around it.
31-40: Graph Algorithms
What is a graph?
- A data structure consisting of vertices (nodes) and edges (connections).
What is BFS (Breadth-First Search)?
- A traversal algorithm that explores all nodes level by level.
What is DFS (Depth-First Search)?
- A traversal algorithm that explores as far as possible along a branch before backtracking.
What is Dijkstra’s Algorithm used for?
- Finding the shortest path between nodes in a weighted graph.
What is the time complexity of Dijkstra’s Algorithm?
- O(V²) using an adjacency matrix and O((V + E) log V) using a priority queue.
What is the Bellman-Ford Algorithm?
- A shortest path algorithm that works with negative weights.
What is Prim’s Algorithm?
- An algorithm used to find the Minimum Spanning Tree (MST).
What is Kruskal’s Algorithm?
- An algorithm that finds the MST by sorting edges and adding them one by one.
What is Floyd-Warshall Algorithm?
- An algorithm used to find the shortest paths between all pairs of vertices.
What is Topological Sorting?
- A linear ordering of vertices in a Directed Acyclic Graph (DAG).
41-50: Divide & Conquer Algorithms
What is the Divide & Conquer approach?
- A technique that breaks a problem into smaller subproblems, solves them, and combines solutions.
Which sorting algorithms use Divide & Conquer?
- Merge Sort and Quick Sort.
What is the time complexity of Quick Sort?
- O(n log n) on average and O(n²) in the worst case.
What is the best case for Quick Sort?
- When the pivot divides the array into nearly equal parts.
What is the recurrence relation for Merge Sort?
- T(n) = 2T(n/2) + O(n)
What is Karatsuba Algorithm used for?
- Fast multiplication of large numbers.
What is Strassen’s Algorithm?
- An algorithm for fast matrix multiplication.
What is the time complexity of Strassen’s Algorithm?
- O(n².81)
Why is Merge Sort stable but Quick Sort is not?
- Merge Sort maintains the relative order of equal elements, while Quick Sort does not always.
What is the best-case time complexity of Merge Sort?
- O(n log n)