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

  • 44Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursion and Fibonacci
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is recursion in programming?

Tap to flipClick to flipspace

A function that calls itself within its own body.

Or swipe the card

44 cards

  1. What is recursion in programming?

    A function that calls itself within its own body.

  2. What are the two components required in any recursive algorithm?

    Base case(s) and recursive case(s). The base case provides a direct answer without further recursion, while the recursive case breaks the problem into smaller instances.

  3. Explain the divide-and-conquer strategy in recursion.

    It involves dividing a large problem into smaller subproblems, solving each subproblem recursively, and combining the results to solve the overall problem.

  4. What is the recursive formulation for reversing a string?

    reverse(s) = reverse(s.slice(1)) + s[0], with base case when string length is 0 (return empty string).

  5. What are the base cases for a recursive Fibonacci function?

    F(1) = 1 and F(2) = 1.

  6. What is the recursive case for computing the nth Fibonacci number?

    F(n) = F(n-1) + F(n-2)

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

    O(2^n)

  8. How does memoization improve the performance of recursive Fibonacci?

    It caches previously computed results, reducing the time complexity from O(2^n) to O(n).

  9. What are the time and space complexities of the iterative Fibonacci algorithm?

    Time: O(n), Space: O(1)

  10. What is the order of node visitation in an inorder traversal of a binary tree?

    Left subtree, then the node itself, then the right subtree.

  11. What is the order of node visitation in a preorder traversal?

    Visit the node first, then the left subtree, then the right subtree.

  12. What is the order of node visitation in a postorder traversal?

    Left subtree, then the right subtree, then the node itself.

  13. What data structure is used to implement Depth-First Search (DFS)?

    A stack (LIFO).

  14. What data structure is used to implement Breadth-First Search (BFS)?

    A queue (FIFO).

  15. Why is a visited set needed in graph traversals like DFS and BFS?

    To avoid revisiting nodes and prevent infinite loops, especially in graphs with cycles.

  16. What is the time complexity of DFS or BFS on a grid of width W and height H?

    O(W * H)

  17. Why can BFS implemented with an array queue have worse time complexity?

    Because the shift() operation on an array takes O(n) time, potentially degrading total time to O((W*H)^2).

  18. What is a stable sorting algorithm?

    A stable sorting algorithm maintains the relative order of elements with equal keys from the original array.

  19. What is an in-place sorting algorithm?

    An in-place sorting algorithm sorts the array by mutating the original array, requiring only O(1) extra space.

  20. Name three sorting algorithms that run in O(n^2) time.

    Selection sort, insertion sort, bubble sort.

  21. Name three sorting algorithms that run in O(n log n) time.

    Merge sort, heap sort, quicksort.

  22. Explain the selection sort algorithm.

    Repeatedly select the smallest (or largest) element from the unsorted portion and swap it with the first element of that portion.

  23. Is selection sort stable?

    No, selection sort is unstable.

  24. Explain the insertion sort algorithm.

    The array is split into sorted and unsorted parts. Elements from the unsorted part are taken one by one and inserted into the correct position in the sorted part.

  25. Is insertion sort stable?

    Yes, insertion sort is stable.

  26. Explain the bubble sort algorithm.

    Repeatedly step through the array, swapping adjacent elements if they are in the wrong order. The larger elements 'bubble' to the end.

  27. Is bubble sort stable?

    Yes, bubble sort is stable.

  28. Explain the merge sort algorithm.

    Divide the array into two halves, recursively sort each half, then merge the two sorted halves together.

  29. Is merge sort stable?

    Yes, merge sort is stable.

  30. Explain the heap sort algorithm.

    Insert all elements into a heap, then repeatedly extract the root (min or max) and place it into the sorted array.

  31. Is heap sort stable?

    No, heap sort is unstable.

  32. What is the key idea of the quicksort algorithm?

    Select a pivot element, partition the array so that elements smaller than the pivot go left and larger go right, then recursively sort the partitions.

  33. Is quicksort stable?

    No, quicksort is unstable.

  34. What does Dijkstra's algorithm compute?

    The shortest path from a starting vertex to every other vertex in a weighted graph.

  35. Describe the steps of Dijkstra's algorithm.

    1. Initialize distance[start]=0, all others to infinity. 2. Use a min-heap to select the unvisited vertex with smallest distance. 3. For each neighbor, update its distance if a shorter path is found. 4. Mark the vertex as visited. 5. Repeat until all vertices are processed.
  36. What is the time complexity of Dijkstra's algorithm when implemented with a binary min-heap?

    O((V + E) log V)

  37. How does the A* algorithm improve upon Dijkstra's algorithm?

    A* uses a heuristic function (e.g., Euclidean distance) to estimate the remaining distance to the goal, prioritizing nodes that are closer to the target.

  38. What is a spanning tree of a graph?

    A subgraph that connects all vertices of the original graph with exactly V-1 edges and contains no cycles.

  39. What is a minimum spanning tree (MST)?

    A spanning tree with the smallest possible total edge weight among all spanning trees.

  40. Explain Kruskal's algorithm for finding the minimum spanning tree.

    1. Sort all edges by weight. 2. Starting from the smallest edge, add it to the MST if it does not create a cycle. 3. Stop when there are V-1 edges.
  41. What is the time complexity of Kruskal's algorithm?

    O(E log E) for sorting plus O(E log V) for union-find operations, typically simplified to O(E log E).

  42. What is dynamic programming?

    An algorithmic technique that solves optimization problems by breaking them into overlapping subproblems and storing results to avoid redundant computations.

  43. How can dynamic programming be used to find the minimum sum of a contiguous subarray?

    For each element, compute the minimum sum of a subarray ending at that element: min with previous best or start new. Keep track of the overall minimum.

  44. What are the time and space complexities of the dynamic programming solution for the minimum subarray sum problem?

    Time: O(n), Space: O(1) if only storing the previous result.

Card 1 of 44
Card 1 of 44

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent