Algorithm & Data Structures - Q&A
1. Define Algorithm. List out the criteria's that all algorithms must satisfy.
An algorithm is a finite sequence of well-defined instructions used to solve a problem or perform a computation.
Criteria:
- Input: Zero or more inputs provided externally.
- Output: At least one output/result.
- Finiteness: Must terminate after a finite number of steps.
- Definiteness: Each step must be clearly and unambiguously defined.
- Effectiveness: All operations must be basic enough to be performed exactly and in a finite time.
2. On what factors efficiency of an algorithm depends?
Efficiency depends on:
- Time Complexity
- Space Complexity
- Input Size
- Type of Input
- Constants and Lower Order Terms
3. Arrange the following functions in ascending order: n, 2^n, n!, n^3, nlogn, logn, n^2
Ascending order: logn < n < nlogn < n^2 < n^3 < 2^n < n!
4. Define Brute force method. List out the problems that can be solved using this method.
Brute Force is a straightforward method of solving a problem by trying all possible solutions.
Algorithm & Data Structures - Q&A
Problems:
- String Matching
- TSP
- Sorting
- Searching
- Matrix Multiplication
5. Define sorting. List any two sorting techniques.
Sorting is arranging data in a specified order (ascending or descending).
Two techniques:
1. Bubble Sort
2. Quick Sort
6. Mention the steps involved in merge sort.
Steps:
1. Divide the array into halves
2. Recursively sort both halves
3. Merge the sorted halves
7. Define i) Transitive Closure ii) Adjacency Matrix
i) Transitive Closure: A graph showing reachability of vertices.
ii) Adjacency Matrix: A 2D array representing graph edges.
Algorithm & Data Structures - Q&A
8. State Horspool's algorithm for pattern matching.
Uses a shift table to skip unnecessary comparisons:
- Preprocess pattern
- Compare from right
- Shift using table on mismatch
9. Define i) Feasible Solution ii) Optimal Solution
i) Feasible Solution: Satisfies all constraints.
ii) Optimal Solution: Best among all feasible ones.
10. Define Minimum Spanning Tree.
A subset of edges that connects all vertices with minimum total weight and no cycles.
11. What is Backtracking?
A technique to solve problems by exploring options and backtracking upon failure, e.g., N-Queens.
12. What is sum of sub-set problem?
To find if a subset exists whose sum equals a given value. Solved using backtracking or DP.