Week 3 - Recursive Thinking and Stacks Flashcards

1
Q

What is an example of a problem and recursive solution?

A

Problem: search for a key in a box that contains many smaller boxes.

Recursive Solution:
Inspect a box

If another box is found, repeat step 1 (recursive call)

If the key is found, the search ends

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

What is Recursion?

A

Recursion is a process where a function calls itself to solve smaller instances of a problem. It continues until a base case is met, which stops the recursion. They always contain one base case and one recursive case.

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

What is a good rule of thumb for algorithms that can be implemented as a loop?

A

If an algorithm can be implemented as a loop, there is a way to implement it recursively.

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

What is a base case in a recursive function?

A

A base case is the condition in a recursive function that stops further recursive calls. It prevents infinite recursion by providing a direct solution for the simplest instance of the problem.

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

What is a recursive case in a recursive function?

A

A recursive case is the part of a recursive function that includes a call to the function itself to solve a smaller instance of the problem. It breaks down the problem until the base case is reached.

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

What is a Stack?

A

A stack is a basic data structure that follows the Last-In, First-Out (LIFO) principle, where the last item added is the first one removed. It can be implemented using either an array or a linked list.

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

What is the difference between the array and linked lists implementation of stacks?

A

Linked list implementation is easier than array, because they simply insert/remove the head of the list.

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

What is a Call Stack?

A

A call stack is a data structure that tracks active function calls, including each function’s local variables, parameters, and the location for resuming execution. It follows the LIFO principle, managing the execution flow and ensuring functions return to the correct place in the code.

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

What is the difference between “Push” and “Pop” in relation to call stacks?

A

“Push” refers to adding a function call to the top of the call stack when the function is invoked. “Pop” refers to removing the most recent function call from the stack when the function completes execution.

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

Why should you be careful when using call stacks?

A

Using the call stack simplifies managing recursion but consumes memory, as each function call requires its own allocation. To avoid memory issues from large call stacks, alternatives like loops or tail recursion can be used.

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

What is a parser?

A

A Parser is a program that takes text as input and evaluates the text based on a set of syntactic rules.

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