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

  • 94Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursion Foundations

What is recursion in programming?

94 questions

  1. 1

    What is recursion in programming?

    Model answer

    Recursion is a method or function calling itself, usually on a smaller version of the problem, until a base case stops further calls.

    Explanation

    Each recursive call should make progress toward a condition that ends the process.

  2. 2

    How does recursion break a large problem into manageable parts?

    Model answer

    It performs a small unit of work and calls itself on a subproblem that is closer to the solution.

    Explanation

    Repeatedly reducing the problem leads toward a simpler case that can be answered directly.

  3. 3

    What is a base case, and what role does it play?

    Model answer

    A base case is a condition under which a recursive function stops calling itself and returns an answer.

    Explanation

    Without a stopping condition, recursive calls can continue until the program runs out of stack space.

  4. 4

    Why must a recursive call make progress toward its base case?

    Model answer

    Progress ensures that repeated calls eventually reach the stopping condition instead of repeating the same unsolved problem indefinitely.

    Explanation

    A changing parameter or smaller problem space can move each call closer to termination.

  5. 5

    In the line-of-people example, how can someone find their position without counting everyone themselves?

    Model answer

    They ask the person ahead for their position, then add one to that person’s answer; the first person in line provides the base case.

    Explanation

    Each person’s answer depends on the answer from the next person, and the results build back toward the original asker.

  6. 6

    In the essay-revision example, what changes from one recursive call to the next, and what ends the process?

    Model answer

    The essay is revised on each call, bringing it closer to completion; the process ends when it no longer needs revisions.

    Explanation

    The same essay may be passed along, but its state changes toward the stopping condition.

  7. 7

    How does recursion differ when a call passes a smaller input versus when it updates the same object?

    Model answer

    A call can shrink its input, as when processing the rest of a string, or update the same object, as when revising an essay. In either case, it must progress toward a base case.

    Explanation

    The parameter or object does not have to change in the same way for recursion to make progress.

  8. 8

    What are the two main questions to ask when designing a recursive solution?

    Model answer

    Ask what the base case is and what small unit of work each call can do to move toward the solution.

    Explanation

    These identify how recursion stops and how each call reduces or advances the problem.

  9. 9

    Why can recursion be a good fit for trees and graphs?

    Model answer

    Trees and graphs can be explored one node or substructure at a time, making it natural to solve a larger task through recursive subproblems.

    Explanation

    Their hierarchical or connected structure often supports repeated exploration of smaller parts.

  10. 10

    How can recursion simplify code compared with an imperative approach?

    Model answer

    It can replace complex loops and auxiliary data structures with shorter code that expresses the problem in terms of smaller subproblems.

    Explanation

    Recursive calls provide an implicit stack that can manage parts of the process.

  11. 11

    What makes a recursive solution a poor choice for a problem?

    Model answer

    If the problem does not break naturally into useful subproblems, recursion can make the code unnecessarily complicated.

    Explanation

    Recursion is most suitable when its repeated calls match the structure of the problem.

  12. 12

    Why can recursively defined data structures be especially suitable for recursive algorithms?

    Model answer

    They can be handled one small unit at a time, with each unit leading naturally to similar smaller structures.

    Explanation

    This structure lets a recursive function apply the same operation repeatedly.

  13. 13

    What is the relationship between a recursive function’s base case and the number of calls stored in memory?

    Model answer

    Reaching the base case stops new recursive calls, allowing existing calls to return and their stack frames to be removed.

    Explanation

    The call stack grows during descent and shrinks as calls complete.

  14. 14

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

    Model answer

    Backtracking explores a decision space recursively, evaluates whether a choice works, and returns to try another choice when needed.

    Explanation

    It extends recursion by revisiting decisions to search for an alternative solution.

  15. 15

    What information does a call stack frame store for a method invocation?

    Model answer

    It stores information such as the invocation’s parameters and local variables, as well as where the method should return its result.

    Explanation

    The stack helps the program resume the correct method call with its data.

  16. 16

    What happens to the call stack when one method calls another method whose result it needs?

    Model answer

    A new stack frame is pushed for the called method, and the earlier method waits until that call returns a result.

    Explanation

    A method cannot finish a computation that depends on another call until the dependency has been evaluated.

  17. 17

    In what order do dependent method calls return their results?

    Model answer

    They return in reverse order from the order in which the calls were made: the most recently called method returns first.

    Explanation

    The call stack follows a last-in, first-out order.

  18. 18

    Why can a method not return its final result while a dependent call is still unfinished?

    Model answer

    It needs the dependent call’s result to complete its own computation, so it must wait for that call to return.

    Explanation

    For example, a method cannot concatenate a called method’s string result before receiving it.

  19. 19

    What is a stack overflow in the context of recursion?

    Model answer

    A stack overflow occurs when method calls exceed the program’s available call-stack memory, potentially causing the program to crash.

    Explanation

    Unending recursion or excessive recursive depth can keep adding stack frames until the allocated space is exceeded.

  20. 20

    Why does recursion have CPU overhead compared with a loop?

    Model answer

    Each recursive call adds method-invocation work, so recursion can be slower than iterating through a loop.

    Explanation

    The additional calls create a time tradeoff to consider when choosing an approach.

  21. 21

    What memory tradeoff can recursion introduce?

    Model answer

    Each active recursive call uses a stack frame, so deep recursion can consume substantial memory and may cause a stack overflow.

    Explanation

    Unlike a simple loop, recursive calls can remain active while waiting for deeper calls to finish.

  22. 22

    What advantage does recursion’s implicit stack provide?

    Model answer

    It can manage the sequence of unfinished subproblems without requiring the programmer to build and maintain an explicit stack.

    Explanation

    This can reduce auxiliary data structures and simplify an algorithm.

  23. 23

    Why do recursive calls build up before returning in many recursive algorithms?

    Model answer

    Each call makes another call that it depends on, so it must remain on the stack until the deeper call returns.

    Explanation

    Once a base case is reached, the calls can return one by one and complete their pending work.

  24. 24

    What is the smallest useful base case for recursive string reversal described in the content?

    Model answer

    The empty string is a base case because reversing it still produces the empty string.

    Explanation

    A single-character string is also a valid base case, but the empty string is an even smaller input.

  25. 25

    How does a recursive string-reversal function reduce its input?

    Model answer

    It removes the first character and recursively reverses the remaining substring.

    Explanation

    Removing one character at each call shrinks the input toward the empty-string base case.

  26. 26

    Why is the first character added after the recursive call in the string-reversal method?

    Model answer

    The deeper call reverses the remaining substring first, so adding the removed character afterward places it at the end of the reversed result.

    Explanation

    The characters return in reverse order as the recursive calls unwind.

  27. 27

    How does the call stack help a recursive string-reversal function remember removed characters?

    Model answer

    Each stack frame retains its removed character while waiting for the recursive call, then adds that character to the returned string.

    Explanation

    The function does not need a separate structure to store those characters.

  28. 28

    What are the base cases for checking a palindrome by recursively shrinking a string?

    Model answer

    Strings of length zero or one are palindromes.

    Explanation

    There are no opposing characters to disprove a palindrome in an empty string, and one character reads the same forward and backward.

  29. 29

    How does a recursive palindrome check test the string?

    Model answer

    It compares the first and last characters, then recursively checks the substring between them if they match.

    Explanation

    The process repeatedly narrows the string until it reaches a base case or finds a mismatch.

  30. 30

    What should a recursive palindrome check return when the two outer characters do not match?

    Model answer

    It should return false immediately.

    Explanation

    A mismatch at any pair of opposite positions disproves the palindrome property.

  31. 31

    How is the result of a recursive palindrome check propagated after the innermost call?

    Model answer

    If the innermost substring is a palindrome and all outer character pairs matched, true returns through the earlier calls; a false result similarly propagates outward.

    Explanation

    Each call depends on the result for the smaller substring.

  32. 32

    How can a decimal integer be converted to binary using repeated division?

    Model answer

    Repeatedly divide the integer by two, recording each remainder, and continue with the integer quotient until it reaches zero.

    Explanation

    Each remainder contributes a binary digit, while each quotient is a smaller problem.

  33. 33

    Why does the decimal-to-binary process use the remainder after division by two?

    Model answer

    The remainder is either zero or one and supplies a binary digit for the result.

    Explanation

    It indicates whether the current value is even or odd.

  34. 34

    What is the base case for the recursive decimal-to-binary method described?

    Model answer

    When the input value reaches zero, the function stops dividing and returns the accumulated result.

    Explanation

    At zero, there is no further quotient to process.

  35. 35

    How does the recursive decimal-to-binary method reduce the problem size?

    Model answer

    It recursively processes the integer quotient after dividing the current value by two.

    Explanation

    The quotient is smaller than the current value, moving the calculation toward zero.

  36. 36

    How does the recursive summation method calculate the sum from one to a positive integer nnn?

    Model answer

    It returns nnn plus the recursive sum from one to n−1n-1n−1.

    Explanation

    Each call contributes its current number and passes a smaller number to the next call.

  37. 37

    What base case is used for summing the natural numbers from one to nnn?

    Model answer

    When the input is one, the function returns one.

    Explanation

    The sum from one to one is simply one.

  38. 38

    How does recursive summation use the call stack to produce its result?

    Model answer

    The calls descend to the base case, then add their current values as they return up the stack.

    Explanation

    For example, the result for five is assembled from five plus the result for four, continuing down to one.

  39. 39

    What defines a divide-and-conquer algorithm?

    Model answer

    It divides a large problem into smaller subproblems, solves those subproblems, and combines their results into an overall solution.

    Explanation

    The divide, solve, and combine steps form a common recursive strategy.

  40. 40

    What important input condition does binary search require?

    Model answer

    The list of values must be sorted.

    Explanation

    Sorted order lets the algorithm determine which half can be discarded after checking the midpoint.

  41. 41

    How does binary search choose the next part of the array to examine?

    Model answer

    It checks the midpoint and, based on the target’s comparison with that value, recursively searches either the left or right half.

    Explanation

    The sorted order makes it possible to rule out the other half.

  42. 42

    How does binary search shrink its search range when the target is less than the midpoint value?

    Model answer

    It keeps the left portion and sets the upper bound to the midpoint index minus one.

    Explanation

    The sorted order means the target cannot be in the midpoint or the right portion.

  43. 43

    How does binary search shrink its search range when the target is greater than the midpoint value?

    Model answer

    It sets the lower bound to the midpoint index plus one and keeps the right portion.

    Explanation

    The target cannot be in the midpoint or the left portion.

  44. 44

    What does binary search return when it finds the target at the midpoint?

    Model answer

    It returns the target’s index in the array.

    Explanation

    Finding the target is a stopping condition for the search.

  45. 45

    What recurrence describes the Fibonacci sequence in the content?

    Model answer

    For indices after the base cases, each Fibonacci value is the sum of the values at the two preceding indices.

    Explanation

    The recursive calls calculate the values for n−1n-1n−1 and n−2n-2n−2 and add them.

  46. 46

    What are the Fibonacci base cases given in the content?

    Model answer

    The values at indices zero and one are zero and one, respectively.

    Explanation

    These values stop the recursive calls and provide the starting values for the sequence.

  47. 47

    In the basic recursive Fibonacci method, why is the right recursive call evaluated after the left one?

    Model answer

    The method must finish evaluating the first recursive call before it can use that result and proceed to the second call in the addition.

    Explanation

    The call stack resolves the dependent work before completing the surrounding expression.

  48. 48

    Why is the basic recursive Fibonacci method inefficient?

    Model answer

    It recalculates the same Fibonacci values many times, creating repeated work in overlapping subproblems.

    Explanation

    For example, the recursive calculation can evaluate the same smaller Fibonacci value in multiple branches.

  49. 49

    What is the stopping condition for splitting a subarray in merge sort?

    Model answer

    Splitting stops when a subarray contains a single value.

    Explanation

    A one-element subarray is already sorted.

  50. 50

    How does merge sort divide an array?

    Model answer

    It finds a midpoint and recursively divides the array into left and right subarrays.

    Explanation

    The subarrays are handled independently before their results are merged.

  51. 51

    Why does merge sort finish processing the left subarray before the right subarray in the described recursive execution?

    Model answer

    The left recursive call must return before execution continues to the right recursive call.

    Explanation

    This follows the order in which the dependent recursive calls are executed.

  52. 52

    How does merge sort combine two sorted subarrays?

    Model answer

    It repeatedly compares the next available value in each subarray and places the smaller value next in the merged result.

    Explanation

    Comparing the front values preserves ascending order.

  53. 53

    What should merge sort do with values remaining in one subarray after the other has run out?

    Model answer

    It should copy all remaining values into the merged result in their existing order.

    Explanation

    The remaining subarray is already sorted, so no more comparisons are needed.

  54. 54

    Why does the merge procedure use a temporary array?

    Model answer

    The temporary array holds the merged values before they are copied back into the corresponding section of the original array.

    Explanation

    Using a temporary buffer avoids overwriting values that are still needed for comparisons.

  55. 55

    How does the merge procedure copy its result back into the original array?

    Model answer

    It copies the temporary values into the original array’s current range, using an offset from the range’s starting index.

    Explanation

    The offset maps the temporary array’s indices to the correct subsection of the original array.

  56. 56

    What makes the merge operation a linear comparison process over its two sorted subarrays?

    Model answer

    It advances a pointer through each subarray, comparing the current values and placing one value at a time into the result.

    Explanation

    Each value is considered as the pointers move through the subarrays.

  57. 57

    What does linked-list reversal aim to change?

    Model answer

    It changes the direction of the links so that each node points to its previous node, making the original tail the new head.

    Explanation

    For example, a list linked from one to six is reversed to link from six back to one.

  58. 58

    What are the base cases for recursive linked-list reversal?

    Model answer

    A null head and a node whose next pointer is null can both be returned directly.

    Explanation

    An empty list and a single-node list are already reversed.

  59. 59

    How does recursive linked-list reversal reach the end of the list?

    Model answer

    It calls itself on the current head’s next node until it reaches the last node.

    Explanation

    The last node satisfies the single-node base case.

  60. 60

    Once the recursive reversal reaches the last node, how does that node become the new head?

    Model answer

    The last node is returned by the base case and propagated unchanged through the earlier calls.

    Explanation

    Each call returns the same new-head reference while rewiring links.

  61. 61

    During recursive linked-list reversal, how is the backward link created?

    Model answer

    For the current node, the node after it is made to point back to the current node.

    Explanation

    This reverses the direction of the connection between adjacent nodes.

  62. 62

    Why must the current node’s old next pointer be cleared during linked-list reversal?

    Model answer

    Clearing it prevents the reversed links from forming a cycle with the old forward connection.

    Explanation

    Without removing the old link, nodes could point around in a cycle instead of forming a reversed list.

  63. 63

    What does recursive merging of two sorted linked lists return when the first list is null?

    Model answer

    It returns the second list.

    Explanation

    Merging an empty list with the other list leaves the other list unchanged.

  64. 64

    What does recursive merging return when the second list is null?

    Model answer

    It returns the first list.

    Explanation

    The remaining non-null list is already sorted and needs no further merging.

  65. 65

    How does recursive merging choose which node comes next in the merged list?

    Model answer

    It compares the two head values and chooses the smaller one.

    Explanation

    Because both input lists are sorted, their smaller head is the smallest currently available value.

  66. 66

    After choosing the smaller head during a recursive linked-list merge, what happens next?

    Model answer

    The chosen node’s next pointer is set to the result of recursively merging its remaining list with the other list.

    Explanation

    The recursive result supplies the rest of the merged list.

  67. 67

    Why can recursive merging of two sorted linked lists produce an ascending list?

    Model answer

    Each step selects the smaller of the two available head values, then recursively merges what remains.

    Explanation

    Repeatedly choosing the smallest available value preserves ascending order.

  68. 68

    What is the key ordering property of a binary search tree around each node?

    Model answer

    Values in a node’s left subtree are smaller, and values in its right subtree are greater; this property applies recursively within the subtrees.

    Explanation

    A value in a subtree must satisfy the ordering constraints imposed by its ancestors as well as its parent.

  69. 69

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

    Model answer

    At most two children.

    Explanation

    They are the node’s left and right children.

  70. 70

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

    Model answer

    It is inserted at a leaf position reached by following left or right links according to comparisons.

    Explanation

    The search continues until it reaches a null child where the new node belongs.

  71. 71

    What is the base case when inserting a value into an empty binary search tree position?

    Model answer

    Create a new node containing the value and return it.

    Explanation

    A null position means there is no existing node to compare against.

  72. 72

    How does binary search tree insertion choose whether to recurse left or right?

    Model answer

    It compares the value being inserted with the current node and recurses left if it is smaller or right if it is greater.

    Explanation

    This follows the binary search tree’s ordering rule.

  73. 73

    Why does a recursive insertion method return the original root after inserting a node?

    Model answer

    Returning the root preserves the caller’s reference to the tree while the recursive calls connect the new node at the appropriate position.

    Explanation

    The inserted node is linked into the existing structure as calls return.

  74. 74

    What is a leaf node in a tree?

    Model answer

    A leaf node has no children.

    Explanation

    In a binary tree, neither its left nor its right child exists.

  75. 75

    How does a recursive tree traversal print leaf nodes from left to right?

    Model answer

    It explores the left subtree first, prints a node if it is a leaf, and then explores the right subtree.

    Explanation

    The left subtree is fully explored before the right subtree is considered.

  76. 76

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

    Model answer

    It should return without printing anything.

    Explanation

    A null node contains no leaf to process.

  77. 77

    How is depth-first search’s order of exploration related to recursion?

    Model answer

    It explores one path and its descendants deeply before returning to explore other paths.

    Explanation

    Recursive calls must finish exploring a subtree or branch before execution resumes at the previous node.

  78. 78

    Why does recursive depth-first search on a graph track visited nodes?

    Model answer

    It tracks visited nodes so the search does not visit the same node repeatedly or get trapped in cycles.

    Explanation

    Graphs can contain cycles, unlike the tree structure used in the earlier examples.

  79. 79

    What are two stopping conditions for the recursive graph search described?

    Model answer

    It returns false for a null node and true when the current node matches the goal.

    Explanation

    A null node cannot contain the goal, while a matching node proves that the goal was found.

  80. 80

    What does the graph search do when it reaches a node that has already been visited?

    Model answer

    It skips that node rather than exploring it again.

    Explanation

    Skipping visited nodes prevents repeated work and helps avoid cycles.

  81. 81

    How does the recursive graph search explore a node’s neighbors?

    Model answer

    It recursively searches each unvisited neighbor and returns true as soon as one of those searches finds the goal.

    Explanation

    The search can stop early when any branch succeeds.

  82. 82

    What does the recursive graph search return if it has explored all neighbors without finding the goal?

    Model answer

    It returns false.

    Explanation

    If every reachable branch fails, the search has not found the goal in the graph.

  83. 83

    What is memoization?

    Model answer

    Memoization stores results of previously computed subproblems so they can be reused instead of calculated again.

    Explanation

    It is useful when recursive calls repeat the same work.

  84. 84

    How does memoization improve the basic recursive Fibonacci method?

    Model answer

    It saves each computed Fibonacci value and reuses it when another call needs that value.

    Explanation

    This prevents repeated calculations of overlapping subproblems.

  85. 85

    How is a cache used during a memoized recursive calculation?

    Model answer

    The function checks the cache first, returns a saved result if present, and stores a newly computed result before returning it.

    Explanation

    This lets later calls reuse work already completed.

  86. 86

    Why can a hashmap be useful as a memoization cache?

    Model answer

    It allows stored results to be retrieved in constant time on average, according to the content.

    Explanation

    The input value can be used as a key to look up its previously computed result.

  87. 87

    Why can memoization prevent a recursive computation from growing many repeated subtrees?

    Model answer

    Once a subproblem has been computed, later calls can retrieve its cached result instead of expanding that subproblem again.

    Explanation

    Reusing stored results avoids redundant recursive work.

  88. 88

    What is tail-call optimization intended to reduce?

    Model answer

    It is intended to reduce the number of stack frames used by certain recursive calls and thereby help avoid stack overflows.

    Explanation

    Some compilers can optimize eligible calls rather than retaining a new frame for every call.

  89. 89

    What makes a recursive call a tail call?

    Model answer

    The recursive call is the final operation, and its result is returned directly without additional work afterward.

    Explanation

    No pending computation needs the result after the call returns.

  90. 90

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

    Model answer

    The function still has to multiply the recursive result after the call returns, so the recursive call is not its final operation.

    Explanation

    The pending multiplication prevents the call from being a direct tail return.

  91. 91

    How can a factorial calculation be rearranged to make its recursive call tail-recursive?

    Model answer

    It can carry the accumulating product as a parameter and return the recursive call directly.

    Explanation

    The multiplication is performed before the next call, leaving no work after that call returns.

  92. 92

    What is a practical limitation of tail-call optimization?

    Model answer

    Compiler support varies by language and environment, so the optimization cannot be assumed to be available everywhere.

    Explanation

    The content notes that it is common in functional languages but not consistently supported in languages such as Python, Java, and JavaScript.

  93. 93

    What should a programmer check before relying on tail-call optimization?

    Model answer

    They should check whether the language, compiler, and execution environment support it.

    Explanation

    Support is not uniform across languages or platforms.

  94. 94

    What is the rule of thumb for writing a tail-recursive call?

    Model answer

    Make the recursive call the last instruction, with no additional operation applied to its result.

    Explanation

    This is the form that enables a compiler to recognize a tail call for optimization.

1 of 94
Question 1 of 94

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent