Algorithms MCQs with answers and time complexity
Review searching, sorting, algorithm design and time complexity, then answer questions with explanations. Practise choosing an approach and explaining its cost for placement and GATE CS concept preparation.
Algorithms are step-by-step procedures for solving computational problems. Understanding algorithm complexity (Big-O notation), sorting methods, searching techniques, and paradigm approaches like dynamic programming and greedy algorithms is essential for technical interviews and competitive programming. This module covers fundamental algorithmic concepts with their time and space complexities.
Key formulas and rules
8 rulesTime Complexity Growth:
Master Theorem: For : compare and to yield , , or
Binary Search: time and space on sorted arrays
Merge Sort: time in all cases, requires auxiliary space
Quick Sort: Recurrence average time, worst case, recursion stack space
Elementary Sorts: Bubble, Selection, and Insertion sorts run in worst-case time with auxiliary space
Graph Traversals: BFS and DFS visit all reachable nodes in time and space using adjacency lists
Dynamic Programming: Solves problems with optimal substructure and overlapping subproblems using memoization (top-down) or tabulation (bottom-up)
Key concepts
Big-O Notation
Big-O describes the upper bound of algorithm growth rate as input size n approaches infinity. O(1) is constant time (best), O() is logarithmic (binary search), O(n) is linear (single loop), O(n ) is linearithmic (efficient sorts), is quadratic (nested loops), and O(2 ) is exponential (brute force). When analyzing, drop constants and lower-order terms: O(2n + 3) becomes O(n). Space complexity tracks auxiliary memory used.
Sorting Algorithms
Comparison-based sorts: Bubble Sort (adjacent swaps), Selection Sort (find minimum), Insertion Sort (build sorted portion) — all . Efficient sorts: Merge Sort (divide and conquer, stable, O(n )), Quick Sort (pivot partitioning, average O(n ), unstable). Non-comparison: Counting Sort O(n+k), Radix Sort O(dx(n+b)). Stability matters when equal elements must maintain relative order. Merge Sort is always O(n ) but uses O(n) space; Quick Sort is in-place but worst case.
Searching Algorithms
Linear Search checks each element sequentially — O(n) time, works on unsorted data. Binary Search requires sorted array, repeatedly halves the search space by comparing with middle element — O() time, O(1) space. Binary Search can find first/last occurrence by adjusting bounds. Interpolation Search estimates position for uniformly distributed data — O(log ) average, O(n) worst. Ternary Search divides into three parts for unimodal functions.
Graph Traversal: BFS and DFS
Breadth-First Search (BFS) explores level by level using a queue, finding shortest path in unweighted graphs. Depth-First Search (DFS) explores as deep as possible using recursion/stack, good for cycle detection and topological sort. Both visit each vertex and edge once: O(V + E) time, O(V) space for visited set. BFS uses queue; DFS uses call stack (implicit) or explicit stack. DFS variants: preorder, inorder, postorder for trees.
Dynamic Programming
DP solves problems by breaking into overlapping subproblems and storing results to avoid recomputation. Requirements: Optimal substructure (optimal solution contains optimal subsolutions) and overlapping subproblems (same subproblems solved multiple times). Two approaches: Top-down (memoization — recursive with cache) and Bottom-up (tabulation — iterative filling table). Classic examples: Fibonacci, Longest Common Subsequence, 0/1 Knapsack, Matrix Chain Multiplication. Trade space for time.
Greedy Algorithms
Greedy makes locally optimal choice at each step hoping for global optimum. Works when problem has greedy choice property (local optimal leads to global optimal) and optimal substructure. Examples: Activity Selection, Huffman Coding, Dijkstra's shortest path (non-negative weights), Kruskal's/Prim's MST. Unlike DP, greedy doesn't reconsider choices. Not always optimal — verify with proof or counterexample. Often simpler and faster than DP when applicable.
Worked examples
Example 1
What is the time complexity of the following nested loop?
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum += arr[i][j];
}
}Solution
Step 1: Outer loop iteration count.
The outer loop executes times ().
Step 2: Inner loop execution per outer iteration.
For each outer iteration, the inner loop executes times.
Step 3: Total elementary operations.
Each inner operation runs in constant time.
Answer: (quadratic time complexity).
Example 2
Apply binary search to locate target key 23 in the sorted array: [2, 5, 8, 12, 16, 23, 38, 45, 56].
Solution
Step 1: Iteration 1.
Step 2: Iteration 2.
Step 3: Iteration 3.
Answer: Target found at index 5 after 3 comparisons.
Example 3
Solve the 0/1 Knapsack problem with capacity and items: [(wt: 2, val: 3), (wt: 3, val: 4), (wt: 4, val: 5)].
Solution
Step 1: Define DP state.
Let denote maximum value obtainable using a subset of the first items with capacity .
Step 2: DP table computation.
- Capacity 0 to 5 for Item 1 (): dp[1] = [0, 0, 3, 3, 3, 3]
- Capacity 0 to 5 for Item 2 (): dp[2] = [0, 0, 3, 4, 4, 7] (at : )
- Capacity 0 to 5 for Item 3 (): dp[3][5] = \max(7, dp[2][1] + 5) = \max(7, 0 + 5) = 7
Answer: Maximum knapsack value is 7 (taking items 1 and 2 with weights ).
Example 4
State the recurrence relation and overall time complexity for Merge Sort.
Solution
Step 1: Recurrence formulation.
Merge sort divides the array into 2 equal halves and merges them in linear time:
Step 2: Apply Master Theorem.
Here .
Answer: in best, average, and worst cases.
Representative solved questions
See the kind of question in this topic before opening the full practice set.
Question 1
Which sorting algorithm has the best average-case time complexity?
Answer: D. All have O(n log n)
ExplanationStep 1: Quick Sort has average case O(n ) but worst case .
Step 2: Merge Sort has guaranteed O(n ) in all cases.
Step 3: Heap Sort has guaranteed O(n ) in all cases.
Step 4: Merge Sort and Heap Sort have O(n ) worst case, Quick Sort has O(n ) average case.
Answer: All have O(n ) average case
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 2
What is the recurrence relation for the time complexity of Merge Sort?
Answer: B. T(n) = 2T(n/2) + O(n)
ExplanationStep 1: Merge Sort divides the problem into 2 subproblems of size n/2.
Step 2: The conquer step recursively sorts both halves.
Step 3: The merge step combines two sorted halves in O(n) time.
Step 4: Using Master Theorem: , , , so T(n) = 2T(n/2) + .
Answer: T(n) = 2T(n/2) + O(n)
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 3
How does randomized Quick Sort improve performance?
Answer: B. It avoids worst-case by random pivot selection
ExplanationStep 1: Standard Quick Sort can hit with poor pivot choices.
Step 2: Randomized Quick Select selects a random element as pivot.
Step 3: This makes the probability of consistently bad pivots extremely low.
Step 4: Expected time complexity becomes O(n ) regardless of input distribution.
Answer: It avoids worst-case by random pivot selection
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Common mistakes and useful habits
- Always state both time and space complexity in answers — interviewers expect both.
- For sorting questions, mention stability and whether the algorithm is in-place (uses O(1) extra space).
- Binary search can be tricky with boundary conditions — practice finding first/last occurrence and floor/ceiling variants.
- Recognize DP problems by looking for 'optimal' or 'maximum/minimum' with overlapping subproblems — if recursion solves same subproblem multiple times, use memoization.
- When comparing O(n^2) vs O(n log n), remember that for n=10 , n^2 = 10^1^2 operations (too slow) while n log n ~= 2x10 (acceptable).
- For graph problems, BFS gives shortest path in unweighted graphs; Dijkstra for weighted with non-negative edges; Bellman-Ford for weighted with possible negative edges.
Ready to test your understanding?
Work through 185 questions with explanations after each answer.