Algorithms Beginner Quiz
30 questions on Algorithms Analysis.
Question 1: What is an algorithm in computer science?
- A. A mathematical error
- B. A step-by-step set of instructions designed to perform a specific task or solve a problem β (correct answer)
- C. A type of hardware component
- D. A graphical design pattern
Explanation: Algorithms are unambiguous rules or instructions that take an input, process it, and produce an output.
Question 2: Which of the following sorting algorithms works by repeatedly swapping adjacent elements if they are in the wrong order?
- A. Selection Sort
- B. Insertion Sort
- C. Bubble Sort β (correct answer)
- D. Quick Sort
Explanation: Bubble sort "bubbles" the largest elements to the end of the array by constantly comparing and swapping adjacent items.
Question 3: Which searching algorithm requires the dataset to be strictly sorted before it can be used?
- A. Linear Search
- B. Binary Search β (correct answer)
- C. Breadth-First Search
- D. Depth-First Search
Explanation: Binary search compares the target value to the middle element, eliminating half the search space at each step. This logic completely fails if the array is unsorted.
Question 4: What is the primary advantage of Binary Search over Linear Search?
- A. It uses less memory
- B. It is significantly faster on large, sorted datasets β (correct answer)
- C. It works on unsorted datasets
- D. It doesn't require loops
Explanation: Linear search takes O(n) time, while binary search takes O(log n). For 1,000,000 items, binary search takes at most 20 steps, while linear search might take 1,000,000.
Question 5: What is "Recursion" in algorithm design?
- A. When an array is reversed
- B. When a function calls itself to solve smaller instances of the same problem β (correct answer)
- C. When a variable is deleted
- D. When a loop runs infinitely
Explanation: Recursion breaks down a problem into smaller, identical problems until it hits a terminating "base case".
Question 6: What is absolutely required in any recursive algorithm to prevent infinite loops (Stack Overflow)?
- A. A for loop
- B. A base case (exit condition) β (correct answer)
- C. A global variable
- D. An array
Explanation: A base case stops the recursion from continuing infinitely. Without it, the function calls itself forever until memory runs out.
Question 7: Which algorithm paradigm divides a problem into smaller sub-problems, solves them independently, and then combines their solutions?
- A. Greedy Algorithm
- B. Divide and Conquer β (correct answer)
- C. Dynamic Programming
- D. Brute Force
Explanation: Divide and Conquer (used in Merge Sort and Quick Sort) breaks data into halves, sorts them, and merges them back together.
Question 8: Which algorithmic paradigm builds up a solution piece by piece, always choosing the next piece that offers the most immediate benefit?
- A. Dynamic Programming
- B. Divide and Conquer
- C. Greedy Algorithm β (correct answer)
- D. Backtracking
Explanation: A greedy algorithm makes the locally optimal choice at each stage with the hope of finding a global optimum (e.g., giving change with the largest coins first).
Question 9: Which sorting algorithm is generally considered the fastest for large, random datasets in practice?
- A. Bubble Sort
- B. Insertion Sort
- C. Selection Sort
- D. Quick Sort β (correct answer)
Explanation: Quick Sort (average O(n log n)) is highly efficient and uses an in-place partitioning mechanism, making it the default sorting algorithm in many language standard libraries.
Question 10: How does Linear Search work?
- A. It splits the array in half repeatedly
- B. It checks every single element sequentially from the beginning until the target is found β (correct answer)
- C. It uses a hash function
- D. It jumps randomly
Explanation: Linear search iterates through the array one index at a time. It works on both sorted and unsorted data.
Question 11: Which sorting algorithm maintains a "sorted" sub-list and repeatedly takes the next element from the "unsorted" sub-list and places it into its correct position?
- A. Insertion Sort β (correct answer)
- B. Bubble Sort
- C. Merge Sort
- D. Selection Sort
Explanation: Insertion sort mimics how you might sort playing cards in your hands, pulling one card at a time and inserting it into the correct spot among the already sorted cards.
Question 12: Which sorting algorithm finds the absolute minimum element from the unsorted part of the array and puts it at the beginning?
- A. Insertion Sort
- B. Bubble Sort
- C. Selection Sort β (correct answer)
- D. Quick Sort
Explanation: Selection sort "selects" the smallest remaining element and swaps it into the first unsorted position.
Question 13: What is a "Brute Force" algorithm?
- A. An algorithm that uses physical hardware acceleration
- B. A straightforward approach that tries all possible solutions to find the correct one, without optimization β (correct answer)
- C. An algorithm that compresses data
- D. An algorithm that skips elements
Explanation: Brute force exhaustively checks every possibility. It is simple to write but generally very slow for large inputs (e.g., guessing a password).
Question 14: What is Dynamic Programming (DP)?
- A. Writing code dynamically at runtime
- B. An optimization technique that solves complex problems by breaking them into overlapping sub-problems and storing the results (memoization) to avoid redundant calculations β (correct answer)
- C. Using global variables
- D. Writing recursive functions without base cases
Explanation: DP trades memory for speed. By remembering the answers to smaller sub-problems (like calculating Fibonacci numbers), it prevents the algorithm from recalculating them later.
Question 15: What is the primary characteristic of the Fibonacci sequence that makes it a classic example for Recursion and Dynamic Programming?
- A. Each number is a prime number
- B. Each number is the sum of the two preceding numbers (F_n = F_{n-1} + F_{n-2}) β (correct answer)
- C. It uses multiplication
- D. It only works on even numbers
Explanation: Because the formula inherently references its own previous states, it maps perfectly to recursive algorithm design.
Question 16: In Binary Search, if the target value is *less* than the middle element, what happens next?
- A. The search stops and returns false
- B. The search continues on the right half of the array
- C. The search continues on the left half of the array β (correct answer)
- D. The array is sorted again
Explanation: Because the array is sorted, if the target is less than the midpoint, it is mathematically impossible for the target to be on the right side. The right half is discarded.
Question 17: Which graph algorithm explores all neighbor nodes at the present depth before moving on to the nodes at the next depth level?
- A. Depth-First Search (DFS)
- B. Breadth-First Search (BFS) β (correct answer)
- C. Binary Search
- D. Linear Search
Explanation: BFS explores layer-by-layer like ripples in a pond, making it ideal for finding the shortest path in unweighted graphs.
Question 18: Which graph algorithm explores as far as possible along each branch before backtracking?
- A. Breadth-First Search (BFS)
- B. Depth-First Search (DFS) β (correct answer)
- C. Dijkstra's Algorithm
- D. A* Search
Explanation: DFS dives deep down a single path until it hits a dead end, then backtracks. It is ideal for maze solving and topological sorting.
Question 19: What is "Memoization" in Dynamic Programming?
- A. Writing comments in code
- B. Storing the results of expensive function calls and returning the cached result when the same inputs occur again β (correct answer)
- C. Deleting old variables to free memory
- D. Using recursive loops
Explanation: Memoization is a top-down caching technique that drastically reduces the time complexity of overlapping recursive algorithms.
Question 20: Which algorithmic technique is generally used to generate all permutations of a string or solve a Sudoku puzzle?
- A. Greedy
- B. Backtracking β (correct answer)
- C. Linear Search
- D. Bubble Sort
Explanation: Backtracking is a refinement of brute force. It incrementally builds candidates to the solutions, and abandons ("backtracks") a candidate as soon as it determines it cannot succeed.
Question 21: What is Dijkstra's Algorithm used for?
- A. Sorting an array of integers
- B. Finding the shortest path between nodes in a graph with non-negative edge weights β (correct answer)
- C. Compressing files
- D. Reversing a linked list
Explanation: Dijkstra's is the classic algorithm used in GPS routing to find the fastest path between cities (nodes) based on distance/time (edge weights).
Question 22: Why is Bubble Sort generally not used in production software?
- A. It is too complicated to implement
- B. It requires too much memory
- C. Its O(n^2) time complexity makes it extremely slow for large datasets compared to O(n log n) algorithms β (correct answer)
- D. It only works on strings
Explanation: Bubble Sort is excellent for teaching the concept of sorting, but its performance scales horribly, making it impractical for real-world use.
Question 23: Which algorithm relies heavily on a "Pivot" element to partition the data?
- A. Merge Sort
- B. Quick Sort β (correct answer)
- C. Insertion Sort
- D. Selection Sort
Explanation: Quick Sort selects a pivot, moves all smaller elements to the left, and all larger elements to the right, then recursively sorts the two sub-arrays.
Question 24: Which sorting algorithm is "Stable"?
- A. One that never crashes
- B. One that preserves the relative order of equal elements in the sorted output β (correct answer)
- C. One that uses exactly O(1) memory
- D. One that sorts letters alphabetically only
Explanation: If you have two identical items (e.g., two "5"s) at index 1 and 4, a stable sort guarantees the first "5" remains before the second "5" in the final result. Merge Sort and Insertion Sort are stable.
Question 25: What is the difference between Top-Down and Bottom-Up Dynamic Programming?
- A. Top-Down uses recursion with memoization; Bottom-Up uses iteration (loops) with tabulation β (correct answer)
- B. Top-Down is for Trees; Bottom-Up is for Graphs
- C. Top-Down is faster than Bottom-Up always
- D. Top-Down uses Queues; Bottom-Up uses Stacks
Explanation: Top-Down starts at the main problem and recursively breaks it down. Bottom-Up starts at the smallest base cases and iterates upward to build the final solution.
Question 26: What defines the "Space Complexity" of an algorithm?
- A. How much hard drive storage the program takes
- B. The amount of extra working memory (RAM) an algorithm needs to execute, relative to the input size β (correct answer)
- C. The physical size of the source code
- D. The number of cloud servers required
Explanation: Algorithms often trade space for time. For example, Merge Sort is fast but requires O(n) extra space to hold the temporary arrays, unlike in-place sorts.
Question 27: Why does Merge Sort always perform consistently, regardless of whether the array is completely random or already sorted?
- A. Because it is a Greedy algorithm
- B. Because it always divides the array completely down to single elements and merges them back up, unconditionally β (correct answer)
- C. Because it uses random pivots
- D. Because it skips already sorted data
Explanation: Merge Sort has a strictly bounded time complexity of O(n log n) in the best, average, and worst cases because its mechanical divide-and-merge process never changes.
Question 28: What is the classic "Traveling Salesperson Problem" (TSP)?
- A. Finding the most expensive item in a database
- B. An algorithmic problem seeking the shortest possible route that visits every node exactly once and returns to the origin city β (correct answer)
- C. Sorting an array of prices
- D. A database querying problem
Explanation: TSP is an NP-Hard problem. As the number of cities increases, the brute force factorial permutations (O(n!)) make it impossible to solve perfectly in reasonable time.
Question 29: If an algorithm solves a problem by picking a random solution and checking if it's correct over and over again, what is it called?
- A. Dynamic Programming
- B. Divide and Conquer
- C. Bogo Sort / Monte Carlo algorithm β (correct answer)
- D. Greedy Algorithm
Explanation: Bogo Sort (or Stupid Sort) randomizes the array and checks if it happens to be sorted. It is used as a joke in computer science to demonstrate horribly inefficient algorithms.
Question 30: When is Insertion Sort highly efficient?
- A. When the array is completely reversed
- B. When the dataset is massive and random
- C. When the array is already mostly sorted or very small β (correct answer)
- D. It is never efficient
Explanation: If an array is mostly sorted, Insertion Sort only needs to perform a few shifts, running in near O(n) time. It is often used as a sub-routine inside advanced algorithms like Timsort.