1. Home
  2. Explore
  3. Computer Science
  4. Understanding Recursion

Study methods

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

Understanding Recursion

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

What is recursion?

Tap to flipClick to flipspace

A method or function that calls itself, usually on a smaller or progressing version of the problem.

Or swipe the card

88 cards

  1. What is recursion?

    A method or function that calls itself, usually on a smaller or progressing version of the problem.

  2. What two questions help design a recursive solution?

    What is the smallest amount of work each call can do, and what condition stops the recursion?

  3. What is a base case in recursion?

    A condition under which a recursive function stops calling itself and returns a result.

  4. What is a recursive step?

    The part of a function that does a small unit of work and calls itself on a subproblem.

  5. Why must recursive calls make progress toward a base case?

    Without progress, the function may keep calling itself without stopping.

  6. How does the ATM line analogy illustrate recursion?

    Each person asks the person ahead for their position; the first person gives the base answer, and each person adds one while the answer returns down the line.

  7. In the ATM line example, what is the base case?

    The first person in line has nobody ahead of them.

  8. How can a recursive line-position function calculate a person's position?

    Return one plus the position of the next person; if there is no next person, use the first-person base case.

  9. What does the essay-revision example show about recursive calls?

    A call can repeat work on the same object while gradually changing it until a completion condition is met.

  10. How does recursion use an implicit stack?

    Each recursive call creates a stack frame that retains its state while waiting for a deeper call to return.

  11. What is one advantage of recursion for inherently recursive data structures?

    It can express traversal or processing with concise code and less complex loop or auxiliary-structure management.

  12. What is a performance cost of recursion compared with iteration?

    Recursive method calls add call-stack and CPU overhead.

  13. What can happen if recursive calls use too much stack memory?

    The program can exceed its allocated call-stack memory and raise a stack overflow error.

  14. When might recursion make a solution unnecessarily complex?

    When the problem does not break naturally into meaningful subproblems or recursive calls are poorly structured.

  15. What information does a call stack frame retain?

    Information such as the return location and the call's parameters and local variables.

  16. In what order do dependent method calls return?

    In reverse order from how they were called: the most recent call returns first.

  17. Why does a recursive call's caller remain on the call stack?

    The caller still has work to finish after the recursive call returns.

  18. What happens to call-stack frames as recursion reaches its base case and returns?

    Frames are removed one at a time as calls return to their callers.

  19. What is a simple recursive strategy for reversing a string?

    Reverse the substring after the first character, then append the first character.

  20. What is a minimal base case for recursive string reversal?

    The empty string returns the empty string.

  21. How does recursive string reversal shrink its problem space?

    It passes the string without its first character to the next call.

  22. Where is each removed character placed when recursive string reversal returns?

    After the result of the recursive call.

  23. What defines a palindrome?

    A string that reads the same forward and backward.

  24. What base cases can identify a string as a palindrome?

    Strings of length zero or one are palindromes.

  25. What comparison drives a recursive palindrome check?

    Compare the first and last characters, then recursively check the substring between them.

  26. What should a recursive palindrome check do when opposite-end characters differ?

    Return false immediately.

  27. What is the recursive rule for converting a positive decimal integer to binary?

    Record the remainder after division by two, then recursively process the integer quotient.

  28. What base case can stop recursive decimal-to-binary conversion?

    Stop when the quotient becomes zero.

  29. Why are remainders important when converting a decimal integer to binary?

    Each remainder from division by two is a binary digit.

  30. How do recursive binary-conversion calls assemble the final binary string?

    They combine the remainders with the result of processing the quotient so the digits appear in the correct order.

  31. What is the binary representation of decimal 233233233?

    111010011110100111101001

  32. What does the sum-of-natural-numbers problem ask when given nnn?

    Find the sum of the integers from 111 through nnn.

  33. What is a recursive rule for summing the natural numbers from 111 through nnn?

    For n>1n>1n>1, return nnn plus the sum from 111 through n−1n-1n−1.

  34. What base case is used in the recursive natural-number sum example?

    When n=1n=1n=1, return 111.

  35. How does the call stack evaluate a recursive sum?

    It reaches the base case first, then adds each pending number as results return up the stack.

  36. What is the Fibonacci recurrence for indices beyond the base cases?

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

  37. What base values are used for the Fibonacci sequence in the example?

    F(0)=0F(0)=0F(0)=0 and F(1)=1F(1)=1F(1)=1.

  38. Why is the straightforward recursive Fibonacci algorithm inefficient?

    It repeatedly computes the same Fibonacci values in multiple branches.

  39. What are the three main phases of divide and conquer?

    Divide a problem into subproblems, solve them independently, and combine their results.

  40. What key input property does binary search require?

    The input list must be sorted.

  41. How does binary search choose its next subproblem?

    It compares the target with the midpoint and searches only the half where the target could occur.

  42. How are binary-search bounds updated when the target is less than the midpoint value?

    Keep the left bound and set the right bound to one position before the midpoint.

  43. How are binary-search bounds updated when the target is greater than the midpoint value?

    Set the left bound to one position after the midpoint and keep the right bound.

  44. What are common stopping conditions for binary search?

    The target is found at the midpoint, or the search bounds show there is no remaining search space.

  45. What does binary search return when it finds a target in the described example?

    The target's index in the array.

  46. What is the base case for splitting in merge sort?

    Stop splitting when a subarray contains one value.

  47. How does merge sort divide an array?

    It repeatedly splits the array into left and right subarrays.

  48. How does merge sort combine sorted subarrays?

    Compare the front values of the two subarrays and repeatedly copy the smaller value into the result.

  49. What happens during a merge when one subarray runs out of values?

    Copy the remaining values from the other subarray into the result.

  50. Why can merge sort use a temporary array during merging?

    The temporary array holds merged values without overwriting source values before they have been compared.

  51. How does recursive merge sort process its left and right subarrays?

    It completes the left recursive call before beginning the right recursive call.

  52. What does the merge step contribute to divide and conquer?

    It combines independently sorted subproblems into a sorted solution for the larger problem.

  53. What is the key ordering rule for a binary search tree?

    Values in a node's left subtree are smaller, and values in its right subtree are greater.

  54. How does recursive linked-list reversal approach the list?

    It recursively advances to the next node until reaching the last node, then reverses pointers while returning.

  55. What are base cases for recursive linked-list reversal?

    Return the head if it is null or if it has no next node.

  56. During linked-list reversal, what does setting the successor's next pointer to the current node accomplish?

    It reverses the link between the two nodes.

  57. Why must the old forward pointer be removed during linked-list reversal?

    To avoid leaving a cycle between the current node and its successor.

  58. Why does recursive linked-list reversal return the last node?

    The last node becomes the head of the reversed list.

  59. What are the base cases for recursively merging two sorted linked lists?

    If one list is null, return the other list.

  60. How does recursive merging choose the next node from two sorted lists?

    Compare the head values and choose the smaller one.

  61. What does a recursive merge call return when list A's head is smaller?

    It makes A's head point to the result of merging A's remainder with list B, then returns A's head.

  62. What does a recursive merge call return when list B's head is smaller?

    It makes B's head point to the result of merging list A with B's remainder, then returns B's head.

  63. When are pointers finalized in recursive merging of sorted linked lists?

    As recursive calls return, each pending call connects its selected head to the merged remainder.

  64. How many children can a binary tree node have at most?

    Two.

  65. Where is a new value inserted in a binary search tree?

    At a leaf position selected by comparisons from the root.

  66. What is the base case for recursive insertion into a binary search tree?

    If the current node is null, create and return a new node containing the value.

  67. How does recursive binary search tree insertion choose a subtree?

    Compare the value to insert with the current node; recurse right if it is greater and left if it is smaller.

  68. Why does recursive tree insertion return the original root?

    The root is the reference to the tree, and returning it preserves access to the updated tree.

  69. What is a leaf node?

    A node with no children.

  70. How can a recursive tree traversal print leaf nodes from left to right?

    Print a node if it has no children; otherwise recursively explore its left subtree before its right subtree.

  71. What should a leaf-printing function do when given a null node?

    Return without printing.

  72. Why does depth-first search finish exploring one branch before another?

    It follows a path recursively to its depth and returns through the call stack before exploring remaining branches.

  73. Why does depth-first search on a graph need a visited set?

    To avoid revisiting nodes and getting stuck following cycles.

  74. What does recursive depth-first search do when it reaches the goal node?

    Return true.

  75. What does recursive depth-first search return if it exhausts the graph without finding the goal?

    Return false.

  76. How does recursive depth-first search process a node's neighbors?

    Skip already visited neighbors; otherwise mark a neighbor visited and recursively search from it.

  77. What is memoization?

    Storing results of completed subproblems so they can be reused instead of recomputed.

  78. How does memoization reduce redundant work in recursive Fibonacci?

    It stores each computed Fibonacci value and reuses it when another call requests the same value.

  79. What data structure does the example use to cache Fibonacci results?

    A hash map.

  80. What lookup performance does the content attribute to retrieving a value from a hash map?

    Constant time, O(1)O(1)O(1).

  81. Why can Fibonacci base cases be preloaded into a memoization cache?

    They are already known results and can be returned directly when requested.

  82. When should a memoized recursive function add a result to its cache?

    After the recursive computation has produced the result.

  83. What is tail-call optimization intended to reduce?

    The number of stack frames retained for eligible recursive calls.

  84. What makes a recursive call a tail call?

    It is the final operation of the function, and its result is returned directly.

  85. Why is a factorial function that multiplies after a recursive call not tail recursive?

    The caller must still multiply the returned recursive result by the current value.

  86. How can a factorial function be restructured to make its recursive call a tail call?

    Pass an accumulator parameter and make the recursive call the final returned expression.

  87. Is tail-call optimization guaranteed in every programming language?

    No; support depends on the language, compiler, and execution environment.

  88. What is backtracking in the recursive problem-solving approach described?

    Explore choices in a decision space recursively, then return to earlier choices when a path does not lead to a solution.

Card 1 of 88
Card 1 of 88

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent