1. Home
  2. Explore
  3. Computer Science
  4. Data Structures and Algorithms

Study methods

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

Data Structures and Algorithms

  • 35Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursion Basics
  • 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 technique in which a function calls itself to solve a problem.

Or swipe the card

35 cards

  1. What is recursion in programming?

    A technique in which a function calls itself to solve a problem.

  2. What is the base case, or termination condition, in a recursive function?

    The condition that stops further recursive calls and returns from the function.

  3. What can happen if a recursive function has no effective termination condition?

    It can call itself indefinitely, eventually exhausting memory and crashing.

  4. What three components should a typical recursive function contain?

    A termination condition, the problem's logic, and a recursive call.

  5. Why should a recursive call usually change or reduce the input?

    So each call makes progress toward the termination condition.

  6. Is recursion specific to one programming language?

    No. The concept can be implemented in languages such as Java, Python, and Scala.

  7. How is recursion related to iteration?

    Both can repeat work; recursion uses repeated function calls, while iteration commonly uses loops.

  8. Does recursion always have better time complexity than an iterative solution?

    No. Recursion is not automatically faster or better in time complexity.

  9. How can a recursive function print a message nnn times?

    Stop when n≤0n \leq 0n≤0; otherwise print the message and call the function with n−1n-1n−1.

  10. In the message-printing example, why does the recursive call use n−1n-1n−1?

    Because the current call has already printed once, leaving one fewer print to perform.

  11. How can recursion print every element of an array in its original order?

    Start at index iii; stop when iii reaches the array length, otherwise print the element and recurse with i+1i+1i+1.

  12. Why is the array-printing base case often written as i≥i \geqi≥ array length?

    It stops when the index has passed the last valid position, including cases where it is already beyond the length.

  13. How can the same recursive array traversal print elements in reverse order?

    Recurse to the next index first, then print the current element while returning.

  14. What determines whether a recursive traversal processes items before or after reaching its deepest call?

    Whether the processing logic is placed before or after the recursive call.

  15. For an array, what is the effect of printing before the recursive call?

    Elements are printed in forward order as the index advances.

  16. For an array, what is the effect of printing after the recursive call?

    Elements are printed in reverse order as recursive calls return.

  17. What is the call stack?

    The stack used to keep track of active function or method calls.

  18. What ordering principle does a stack follow?

    Last in, first out: the most recently added item is removed first.

  19. What information does a recursive call need to preserve on the call stack?

    Its call state, including the inputs needed to resume that call.

  20. What happens to the call stack as a recursive function makes further calls?

    A new call record is added for each active call.

  21. When does a recursive function begin returning from its deepest calls?

    When a call reaches its termination condition and returns.

  22. What does it mean for recursive calls to unwind?

    The deepest call returns first, then earlier calls resume in reverse order of their creation.

  23. Why can recursion be described as moving down and then back up?

    Calls move toward the base case; after it is reached, the calls return in reverse order.

  24. How does recursive execution differ from a simple one-directional loop?

    Recursion can descend through calls and then resume earlier calls while unwinding; a loop typically advances through its iterations in one direction.

  25. Which underlying data structure supports the usual execution of recursion?

    The call stack.

  26. Why can recursive solutions be useful for tree problems?

    They can express repeated operations on a node and its subtrees directly.

  27. What is the basic recursive order for preorder traversal of a binary tree?

    Process the current node, then traverse its left subtree, then its right subtree.

  28. What is a suitable base case for recursive tree traversal?

    If the current node is null, return without processing it.

  29. Which sorting algorithms were identified as using recursion?

    Merge sort and quicksort.

  30. What linked-list operations were mentioned as possible uses of recursion?

    Reversing a linked list or reversing pairs of nodes.

  31. How can recursion relate to dynamic programming?

    Recursive problem-solving is a foundation for understanding and developing many dynamic programming solutions.

  32. What extra memory cost does recursion introduce?

    It uses additional memory for active calls on the call stack.

  33. When might an iterative solution be preferable to a recursive one?

    When memory or call-stack usage is a concern.

  34. What constraints define the Tower of Hanoi problem described?

    Move nnn coins from the first tower to the second using a third tower; move only one coin at a time, and never place a larger coin on a smaller one.

  35. Why was the Tower of Hanoi presented as a recursion problem?

    It illustrates a problem that can be expressed intuitively using recursive reasoning, while an iterative solution can be more complicated.

Card 1 of 35
Card 1 of 35

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent