Recursion Flashcards

1
Q

What use Recursion?

A

Allows solving complex problem with simple solution, reduces coding, and leads to efficient programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the recursive definition of factorial?

A

factorial(n) = n * factorial(n-1) with factorial(0) = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what happens in the first call of factorial(4)?

A

Calls factorial(3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does is factorial(4) broken down further?

A

4 * factorial(3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Expand factorial (4) to the next step

A

43(2* factorial(1))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the recursion continue after 2 * factorial (1)?

A

2*(1 * Factorial(0))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is factorial (0)?

A

1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does factorial(1) resolve to?

A

1 × 1 = 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Simplify 4 × 3 × 2.

A

24

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Final result of factorial(4)?

A

24

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is shown when tracing recursive factorial?

A

Each function call and its return path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the role of the stack in recursion?

A

It stores the state of each function call until it resolves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does space usage grow during recursion?

A

Each recursive call adds a new frame to the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the recursive formula for sum(n)?

A

sum(n) = n + sum(n-1), with sum(0) = 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is sum implemented in Java?

A

Recursively adding n to sum(n-1) until n = 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the recursive formula for Fibonacci numbers?

A

fib(n) = fib(n-1) + fib(n-2), with fib(0)=0, fib(1)=1.

17
Q

How does recursion flow for fib(4)?

A

Breaks into calls to fib(3) and fib(2) and so on.

18
Q

How is Fibonacci implemented in Java?

A

Recursively, with base cases for 0 and 1.

19
Q

What are two key features of a recursive method?

A

A base case and a reduction toward the base case.

20
Q

How can recursion be used to print a message n times?

A

Print once, then call recursively with (n-1) times.

21
Q

What is an example of solving a problem both iteratively and recursively?

A

Checking if a string is a palindrome.

22
Q

How does the non-recursive palindrome check work?

A

Using a while loop comparing characters from ends.

23
Q

What is a simple recursive palindrome check?

A

Compare first and last characters and recurse on substring.

24
Q

How can you improve the recursive palindrome check?

A

Use helper methods to avoid creating new substrings.

25
What are examples of problems easier with recursion?
Binary search, selection sort, directory size, Tower of Hanoi.
26
How does recursive binary search work?
Check middle, recurse on left or right half.
27
What happens when recursive binary search fails to find a key?
Returns a negative insertion point.
28
How does recursive selection sort work?
Find the smallest element, swap, then sort the rest recursively.
29
What does the recursive selection sort code do after swapping?
Sorts the sublist from (low+1) to high.
30
How is directory size computed recursively?
Sum sizes of files and recursively add sizes of subdirectories.
31
In the getSize method, what is the base case?
When the file is not a directory.
32
What is the Tower of Hanoi problem?
Move disks from one tower to another following rules.
33
How is the Tower of Hanoi problem decomposed?
Move n-1 disks, move the largest disk, then move n-1 disks again.
34
What is the recursive structure of moveDisks in Tower of Hanoi?
Move (n-1) disks, move disk n, move (n-1) disks again.
35
What is a Sierpinski Triangle?
A fractal created by recursively subdividing triangles.
36
How is a Sierpinski Triangle formed?
Connect midpoints and recurse on the three new triangles.
37
Name 3 additional recursive problems discussed.
Sum of digits, reverse a string, compute GCD.
38
What are the drawbacks of recursion?
High memory usage and extra time due to many stack frames.
39
What is tail recursion?
A recursion where no pending operations remain after a recursive call.