1. Home
  2. Explore
  3. Computer Science
  4. Key Computer Science Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Key Computer Science Algorithms

  • 28Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursion

What is the defining characteristic of a recursive function?

28 questions

  1. 1

    What is the defining characteristic of a recursive function?

    1. A

      It calls itself within its own body

    2. B

      It always returns a base case value

    3. C

      It uses a loop to iterate over data

    4. D

      It divides the problem into smaller independent tasks

    Correct answer
    A

    It 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.

  2. 2

    In writing a recursive algorithm, what two components are essential?

    1. A

      Base case and recursive case

    2. B

      Memoization and iteration

    3. C

      Stack and queue

    4. D

      Loop condition and increment

    Correct answer
    A

    Base case and recursive case

    Explanation

    The video states that recursive algorithms require defining base case(s) and recursive case(s).

  3. 3

    What are the base cases for the Fibonacci recursive function as presented in the video?

    1. A

      f(0) = 0 and f(1) = 1

    2. B

      f(0) = 1 and f(1) = 1

    3. C

      f(1) = 1 and f(2) = 1

    4. D

      f(1) = 1 and f(2) = 2

    Correct answer
    C

    f(1) = 1 and f(2) = 1

    Explanation

    The video defines f(1) = 1 and f(2) = 1 as the two base cases.

  4. 4

    What is the time complexity of the naive recursive Fibonacci algorithm?

    1. A

      O(2^n)

    2. B

      O(n^2)

    3. C

      O(n log n)

    4. D

      O(n)

    Correct answer
    A

    O(2^n)

    Explanation

    The video explicitly states that the naive recursive Fibonacci has a time complexity of O(2^n).

  5. 5

    How does memoization improve the recursive Fibonacci algorithm?

    1. A

      It eliminates the base cases

    2. B

      It uses a stack to manage function calls more efficiently

    3. C

      It caches previously computed values to avoid redundant calculations

    4. D

      It reduces the number of recursive calls by half

    Correct answer
    C

    It 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).

  6. 6

    What is the space complexity of the iterative Fibonacci algorithm compared to the memoized recursive version?

    1. A

      Both use O(1) space

    2. B

      Both use O(n) space

    3. C

      Iterative uses O(1) space, memoized uses O(n) space

    4. D

      Iterative uses O(n) space, memoized uses O(1) space

    Correct answer
    C

    Iterative 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).

  7. 7

    In the recursive string reversal algorithm, what is the base case?

    1. A

      When the string length is 1, return the reversed string

    2. B

      When the string is empty, return an empty string

    3. C

      When the string has one character, return that character

    4. D

      When the string length is 0, return nothing

    Correct answer
    D

    When 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).

  8. 8

    What is the condition for binary search to work correctly?

    1. A

      The array must have an even number of elements

    2. B

      The array must be sorted

    3. C

      The array must be unsorted

    4. D

      The target must be in the middle of the array

    Correct answer
    B

    The 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.

  9. 9

    What is the time complexity of binary search?

    1. A

      O(n log n)

    2. B

      O(n)

    3. C

      O(log n)

    4. D

      O(n^2)

    Correct answer
    C

    O(log n)

    Explanation

    Binary search halves the search space each iteration, resulting in O(log n) time complexity.

  10. 10

    In the recursive implementation of binary search, what is the base case that indicates the element is not found?

    1. A

      When the middle element equals the target

    2. B

      When the start and end indices are equal

    3. C

      When start index is greater than end index

    4. D

      When the array length is zero

    Correct answer
    C

    When 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.

  11. 11

    What data structure does depth-first search (DFS) use to traverse a graph?

    1. A

      A stack

    2. B

      A tree

    3. C

      A min-heap

    4. D

      A queue

    Correct answer
    A

    A stack

    Explanation

    DFS uses a stack (LIFO) to explore as far as possible along a branch before backtracking.

  12. 12

    Why is a visited set necessary in graph traversal algorithms like DFS and BFS?

    1. A

      To sort the nodes by weight

    2. B

      To avoid revisiting nodes and infinite loops

    3. C

      To store the shortest path to each node

    4. D

      To keep track of the order of insertion

    Correct answer
    B

    To 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.

  13. 13

    What is the time complexity of BFS on a grid of width W and height H when using an array-based queue?

    1. A

      O(W*H)

    2. B

      O(W^2 * H^2)

    3. C

      O(WH log(WH))

    4. D

      O((W*H)^2)

    Correct answer
    D

    O((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.

  14. 14

    What is the key difference between DFS and BFS in terms of exploration order?

    1. A

      DFS uses a queue; BFS uses a stack

    2. B

      DFS works only on trees; BFS works on graphs

    3. C

      DFS finds the shortest path; BFS does not

    4. D

      DFS goes deep first; BFS explores level by level

    Correct answer
    D

    DFS 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.

  15. 15

    What does Dijkstra's algorithm find?

    1. A

      The longest path between two vertices

    2. B

      The minimum spanning tree of a graph

    3. C

      The shortest path from a start vertex to all other vertices

    4. D

      The topological ordering of a graph

    Correct answer
    C

    The 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.

  16. 16

    In Dijkstra's algorithm, what data structure is used to efficiently extract the vertex with the smallest distance?

    1. A

      An array

    2. B

      A stack

    3. C

      A queue

    4. D

      A min-heap

    Correct answer
    D

    A min-heap

    Explanation

    A min-heap allows extraction of the minimum distance vertex in O(log V) time, improving efficiency.

  17. 17

    What heuristic is commonly used in A* pathfinding for grid-based graphs?

    1. A

      Euclidean distance

    2. B

      Hamming distance

    3. C

      Cosine similarity

    4. D

      Manhattan distance

    Correct answer
    A

    Euclidean distance

    Explanation

    The video mentions using Euclidean distance as a heuristic to estimate distance to the goal.

  18. 18

    What is the time complexity of Dijkstra's algorithm when implemented with a min-heap?

    1. A

      O(V^2)

    2. B

      O((V+E) log V)

    3. C

      O(E log V)

    4. D

      O(V log V)

    Correct answer
    B

    O((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).

  19. 19

    What is a spanning tree of a graph?

    1. A

      A tree that includes all possible edges

    2. B

      The shortest path between two vertices

    3. C

      A set of edges that forms a cycle covering all vertices

    4. D

      A set of edges that connects all vertices without cycles

    Correct answer
    D

    A 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.

  20. 20

    What is the defining property of a minimum spanning tree?

    1. A

      It is the tree with the smallest height

    2. B

      It includes the vertex with the smallest degree

    3. C

      It has the smallest total weight among all spanning trees

    4. D

      It uses the minimum number of edges possible

    Correct answer
    C

    It has the smallest total weight among all spanning trees

    Explanation

    The minimum spanning tree is the spanning tree with the minimum total edge weight.

  21. 21

    In Kruskal's algorithm, what is the first step?

    1. A

      Initialize a min-heap with all vertices

    2. B

      Select the vertex with the smallest degree

    3. C

      Sort all edges by weight

    4. D

      Add the smallest edge to the spanning tree

    Correct answer
    C

    Sort all edges by weight

    Explanation

    Kruskal's algorithm begins by sorting all edges in ascending order of weight.

  22. 22

    When does Kruskal's algorithm stop adding edges?

    1. A

      When the spanning tree has V-1 edges

    2. B

      When the total weight exceeds a threshold

    3. C

      When all edges have been considered

    4. D

      When the graph becomes disconnected

    Correct answer
    A

    When 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.

  23. 23

    What algorithm is used to detect cycles in Kruskal's algorithm?

    1. A

      Union-find

    2. B

      Depth-first search

    3. C

      Djikstra's algorithm

    4. D

      Breadth-first search

    Correct answer
    A

    Union-find

    Explanation

    The union-find algorithm efficiently detects whether adding an edge creates a cycle.

  24. 24

    What is the time complexity of Kruskal's algorithm?

    1. A

      O(E log E) or O(E log V)

    2. B

      O(V log V)

    3. C

      O(V^2)

    4. D

      O(E)

    Correct answer
    A

    O(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).

  25. 25

    What is the main principle of dynamic programming?

    1. A

      Using brute force to explore all possibilities

    2. B

      Using recursion without base cases

    3. C

      Dividing the problem into independent subproblems

    4. D

      Storing results of subproblems to avoid redundant calculations

    Correct answer
    D

    Storing results of subproblems to avoid redundant calculations

    Explanation

    Dynamic programming stores solutions to subproblems and reuses them to efficiently solve the larger problem.

  26. 26

    In the minimum subarray sum problem, what does the dynamic programming approach do at each element?

    1. A

      It decides whether to include the previous minimum subarray sum or start a new subarray

    2. B

      It swaps the element with the next one

    3. C

      It compares the element with the global minimum

    4. D

      It calculates the sum of all subarrays ending at that element

    Correct answer
    A

    It 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.

  27. 27

    What is the time complexity of the dynamic programming solution for the minimum subarray sum problem?

    1. A

      O(2^n)

    2. B

      O(n^2)

    3. C

      O(n log n)

    4. D

      O(n)

    Correct answer
    D

    O(n)

    Explanation

    The DP solution makes a single pass through the array, resulting in O(n) time complexity.

  28. 28

    In the naive approach to the minimum subarray sum problem, what is the time complexity?

    1. A

      O(n)

    2. B

      O(n log n)

    3. C

      O(n^2)

    4. D

      O(n^3)

    Correct answer
    D

    O(n^3)

    Explanation

    The naive approach uses three nested loops (two for subarray generation and one for summing), leading to O(n^3) time.

Question 1 of 28
Question 1 of 28

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent