Key Computer Science Algorithms
- 28Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the defining characteristic of a recursive function?
What is the defining characteristic of a recursive function?
28 questions
- 1
What is the defining characteristic of a recursive function?
- A
It calls itself within its own body
- B
It always returns a base case value
- C
It uses a loop to iterate over data
- D
It divides the problem into smaller independent tasks
Correct answer
AIt calls itself within its own body
Explanation
Recursion is defined as a function that calls itself within its own definition, as explained in the video.
- A
- 2
In writing a recursive algorithm, what two components are essential?
- A
Base case and recursive case
- B
Memoization and iteration
- C
Stack and queue
- D
Loop condition and increment
Correct answer
ABase case and recursive case
Explanation
The video states that recursive algorithms require defining base case(s) and recursive case(s).
- A
- 3
What are the base cases for the Fibonacci recursive function as presented in the video?
- A
f(0) = 0 and f(1) = 1
- B
f(0) = 1 and f(1) = 1
- C
f(1) = 1 and f(2) = 1
- D
f(1) = 1 and f(2) = 2
Correct answer
Cf(1) = 1 and f(2) = 1
Explanation
The video defines f(1) = 1 and f(2) = 1 as the two base cases.
- A
- 4
What is the time complexity of the naive recursive Fibonacci algorithm?
- A
O(2^n)
- B
O(n^2)
- C
O(n log n)
- D
O(n)
Correct answer
AO(2^n)
Explanation
The video explicitly states that the naive recursive Fibonacci has a time complexity of O(2^n).
- A
- 5
How does memoization improve the recursive Fibonacci algorithm?
- A
It eliminates the base cases
- B
It uses a stack to manage function calls more efficiently
- C
It caches previously computed values to avoid redundant calculations
- D
It reduces the number of recursive calls by half
Correct answer
CIt caches previously computed values to avoid redundant calculations
Explanation
Memoization stores results in a cache so that each Fibonacci term is computed only once, reducing time to O(n).
- A
- 6
What is the space complexity of the iterative Fibonacci algorithm compared to the memoized recursive version?
- A
Both use O(1) space
- B
Both use O(n) space
- C
Iterative uses O(1) space, memoized uses O(n) space
- D
Iterative uses O(n) space, memoized uses O(1) space
Correct answer
CIterative uses O(1) space, memoized uses O(n) space
Explanation
The iterative version uses constant space because it only keeps track of two variables, while the memoized version stores all computed values in a cache of size O(n).
- A
- 7
In the recursive string reversal algorithm, what is the base case?
- A
When the string length is 1, return the reversed string
- B
When the string is empty, return an empty string
- C
When the string has one character, return that character
- D
When the string length is 0, return nothing
Correct answer
DWhen the string length is 0, return nothing
Explanation
The video states the base case is when the string length is equal to 0, then simply return (or nothing).
- A
- 8
What is the condition for binary search to work correctly?
- A
The array must have an even number of elements
- B
The array must be sorted
- C
The array must be unsorted
- D
The target must be in the middle of the array
Correct answer
BThe array must be sorted
Explanation
Binary search requires a sorted array because it relies on the property that elements to the left are smaller and elements to the right are larger.
- A
- 9
What is the time complexity of binary search?
- A
O(n log n)
- B
O(n)
- C
O(log n)
- D
O(n^2)
Correct answer
CO(log n)
Explanation
Binary search halves the search space each iteration, resulting in O(log n) time complexity.
- A
- 10
In the recursive implementation of binary search, what is the base case that indicates the element is not found?
- A
When the middle element equals the target
- B
When the start and end indices are equal
- C
When start index is greater than end index
- D
When the array length is zero
Correct answer
CWhen start index is greater than end index
Explanation
The video's base case is if start is greater than end, return false, meaning the target is not in the array.
- A
- 11
What data structure does depth-first search (DFS) use to traverse a graph?
- A
A stack
- B
A tree
- C
A min-heap
- D
A queue
Correct answer
AA stack
Explanation
DFS uses a stack (LIFO) to explore as far as possible along a branch before backtracking.
- A
- 12
Why is a visited set necessary in graph traversal algorithms like DFS and BFS?
- A
To sort the nodes by weight
- B
To avoid revisiting nodes and infinite loops
- C
To store the shortest path to each node
- D
To keep track of the order of insertion
Correct answer
BTo avoid revisiting nodes and infinite loops
Explanation
The visited set prevents revisiting nodes that have already been processed, which would cause cycles and infinite loops.
- A
- 13
What is the time complexity of BFS on a grid of width W and height H when using an array-based queue?
- A
O(W*H)
- B
O(W^2 * H^2)
- C
O(WH log(WH))
- D
O((W*H)^2)
Correct answer
DO((W*H)^2)
Explanation
The video notes that using an array queue makes shift O(n) time, leading to O((W*H)^2) time complexity.
- A
- 14
What is the key difference between DFS and BFS in terms of exploration order?
- A
DFS uses a queue; BFS uses a stack
- B
DFS works only on trees; BFS works on graphs
- C
DFS finds the shortest path; BFS does not
- D
DFS goes deep first; BFS explores level by level
Correct answer
DDFS goes deep first; BFS explores level by level
Explanation
DFS exhausts one path as far as possible before moving to another, while BFS expands outward layer by layer.
- A
- 15
What does Dijkstra's algorithm find?
- A
The longest path between two vertices
- B
The minimum spanning tree of a graph
- C
The shortest path from a start vertex to all other vertices
- D
The topological ordering of a graph
Correct answer
CThe shortest path from a start vertex to all other vertices
Explanation
Dijkstra's algorithm finds the shortest distance and path from a starting vertex to every other vertex in a weighted graph.
- A
- 16
In Dijkstra's algorithm, what data structure is used to efficiently extract the vertex with the smallest distance?
- A
An array
- B
A stack
- C
A queue
- D
A min-heap
Correct answer
DA min-heap
Explanation
A min-heap allows extraction of the minimum distance vertex in O(log V) time, improving efficiency.
- A
- 17
What heuristic is commonly used in A* pathfinding for grid-based graphs?
- A
Euclidean distance
- B
Hamming distance
- C
Cosine similarity
- D
Manhattan distance
Correct answer
AEuclidean distance
Explanation
The video mentions using Euclidean distance as a heuristic to estimate distance to the goal.
- A
- 18
What is the time complexity of Dijkstra's algorithm when implemented with a min-heap?
- A
O(V^2)
- B
O((V+E) log V)
- C
O(E log V)
- D
O(V log V)
Correct answer
BO((V+E) log V)
Explanation
The video states the complexity is (V+E) multiplied by log base 2 of V, i.e., O((V+E) log V).
- A
- 19
What is a spanning tree of a graph?
- A
A tree that includes all possible edges
- B
The shortest path between two vertices
- C
A set of edges that forms a cycle covering all vertices
- D
A set of edges that connects all vertices without cycles
Correct answer
DA set of edges that connects all vertices without cycles
Explanation
A spanning tree is a set of edges that connects all vertices and contains no cycles.
- A
- 20
What is the defining property of a minimum spanning tree?
- A
It is the tree with the smallest height
- B
It includes the vertex with the smallest degree
- C
It has the smallest total weight among all spanning trees
- D
It uses the minimum number of edges possible
Correct answer
CIt has the smallest total weight among all spanning trees
Explanation
The minimum spanning tree is the spanning tree with the minimum total edge weight.
- A
- 21
In Kruskal's algorithm, what is the first step?
- A
Initialize a min-heap with all vertices
- B
Select the vertex with the smallest degree
- C
Sort all edges by weight
- D
Add the smallest edge to the spanning tree
Correct answer
CSort all edges by weight
Explanation
Kruskal's algorithm begins by sorting all edges in ascending order of weight.
- A
- 22
When does Kruskal's algorithm stop adding edges?
- A
When the spanning tree has V-1 edges
- B
When the total weight exceeds a threshold
- C
When all edges have been considered
- D
When the graph becomes disconnected
Correct answer
AWhen the spanning tree has V-1 edges
Explanation
The algorithm stops once there are V-1 edges in the spanning tree, which is the minimum number to connect all vertices.
- A
- 23
What algorithm is used to detect cycles in Kruskal's algorithm?
- A
Union-find
- B
Depth-first search
- C
Djikstra's algorithm
- D
Breadth-first search
Correct answer
AUnion-find
Explanation
The union-find algorithm efficiently detects whether adding an edge creates a cycle.
- A
- 24
What is the time complexity of Kruskal's algorithm?
- A
O(E log E) or O(E log V)
- B
O(V log V)
- C
O(V^2)
- D
O(E)
Correct answer
AO(E log E) or O(E log V)
Explanation
Sorting edges takes O(E log E), and union-find operations are O(log V), leading to O(E log E) which simplifies to O(E log V).
- A
- 25
What is the main principle of dynamic programming?
- A
Using brute force to explore all possibilities
- B
Using recursion without base cases
- C
Dividing the problem into independent subproblems
- D
Storing results of subproblems to avoid redundant calculations
Correct answer
DStoring results of subproblems to avoid redundant calculations
Explanation
Dynamic programming stores solutions to subproblems and reuses them to efficiently solve the larger problem.
- A
- 26
In the minimum subarray sum problem, what does the dynamic programming approach do at each element?
- A
It decides whether to include the previous minimum subarray sum or start a new subarray
- B
It swaps the element with the next one
- C
It compares the element with the global minimum
- D
It calculates the sum of all subarrays ending at that element
Correct answer
AIt decides whether to include the previous minimum subarray sum or start a new subarray
Explanation
At each element, the algorithm computes the minimum sum subarray ending at that element by taking the minimum of the element itself or the element plus the previous minimum sum.
- A
- 27
What is the time complexity of the dynamic programming solution for the minimum subarray sum problem?
- A
O(2^n)
- B
O(n^2)
- C
O(n log n)
- D
O(n)
Correct answer
DO(n)
Explanation
The DP solution makes a single pass through the array, resulting in O(n) time complexity.
- A
- 28
In the naive approach to the minimum subarray sum problem, what is the time complexity?
- A
O(n)
- B
O(n log n)
- C
O(n^2)
- D
O(n^3)
Correct answer
DO(n^3)
Explanation
The naive approach uses three nested loops (two for subarray generation and one for summing), leading to O(n^3) time.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.