Data Structures and Algorithms


Data Structures and Algorithms

Anna
Welcome to our second session on data structures and algorithms. Before getting into recursion, let’s set the scene: this course is meant to build practical foundations, not just prepare people for exams. The instructor, Shah Mohan, introduced himself as a senior engineer with experience in database, payments, e-commerce, and professional networking companies.

Michael
And the course is planned to move through foundational topics such as recursion, arrays, sorting, linked lists, and stacks, with an emphasis on how these ideas show up in industry and interviews. Recursion comes early because it supports a lot of what learners meet later, including merge sort, quicksort, linked-list problems, trees, graphs, and dynamic programming.

Anna
So what should we understand by the end of a session on recursion? The goals are to know what recursion is, what supports it behind the scenes, the common structure of a recursive solution, and—often the hardest part—how to think recursively when solving a problem.

Michael
Let’s start with the word itself. Recursion means something repeating or referring back to itself. In programming, a recursive function is a function that calls itself. That sounds simple, but a function that calls itself without a stopping rule can keep going indefinitely.

Anna
That makes recursion sound a bit like a loop. A loop repeats an action until its condition says stop, and a recursive function also repeats work by calling itself. The difference is that recursion expresses the repetition through function calls, which can be especially natural when a problem has a repeating structure.

Michael
Here’s the simplest example of what not to do: imagine a function that prints “Hello, students” and then calls itself again, with no condition to stop. Each call prints the message and creates another call. It never finishes normally, and eventually the program can run out of memory.

Anna
So every recursive solution needs a stopping point. A useful three-part checklist is: first, write a termination condition, also called a base case; second, write the work the function is supposed to do; and third, make a recursive call that moves the problem closer to that base case.

Michael
Let’s use that checklist to print a message ten times. The function can take a number n that represents how many prints remain. If n is less than or equal to zero, return immediately. Otherwise, print the message, then call the function again with n minus one.

Anna
The changing input is crucial. We start with n equal to ten, then nine, then eight, and continue until n reaches zero. If the recursive call kept passing the same n, it wouldn’t move toward the base case, and we’d be back in the never-ending situation.

Michael
Now take an array containing one, two, and three, and suppose we want to print its elements in order. Define a function that takes the array and an index i. Start with i equal to zero. If i is at least the array’s length, return; otherwise, print the element at i and call the function with i plus one.

Anna
The index is the measure of progress here. It goes from zero to one, then two, then three. Since the array length is three, reaching index three means there are no more elements to process, so the base case stops the calls.

Michael
It’s also sensible to check whether the index is greater than or equal to the length, rather than checking only for equality. That covers an index that has gone past the end as well as one exactly at the end. The example’s broader lesson is to think about boundary and invalid-input cases, not just the ordinary path.

Anna
Here’s the surprising part: with almost the same function, we can print the array in reverse. Keep the same base case and the same recursive step, but call the function on the next index before printing the current element.

Michael
Let’s trace that. The first call reaches index zero and immediately calls the next one; then that call reaches index one and calls the next; then index two calls index three, which hits the base case. Only after that deepest call returns does the function print the element at index two, followed by the element at index one, and then the element at index zero. The result is three, two, one.

Anna
That gives us a powerful rule of thumb. If the work happens before the recursive call, it happens while the calls are going deeper. If the work happens after the recursive call, it happens as the calls return. Putting the work on one side or the other can change the order of the result.

Michael
A loop usually makes its progress in one direction: it repeats, updates its state, and continues. Recursion has a descent and a return. First, calls build up until a base case is reached; then the pending work from those calls is completed on the way back.

Anna
That return phase is why recursion can be so useful. For some problems, especially ones with a natural branching or nested pattern, the code can be more direct and intuitive than forcing the same structure into a loop. But recursion does not automatically make a solution faster or more efficient.

Michael
In fact, each active recursive call needs space in the call stack. So recursion uses extra memory to remember where each call came from and what still needs to happen. If memory is a concern, an iterative solution may be preferable; the choice depends on the problem and its constraints.

Anna
Let’s make the stack concrete. A stack is a last-in, first-out structure: the last item placed on it is the first item removed. Think of plates stacked on a table. The last plate added is the one you can take off first.

Michael
Programming languages use a call stack to keep track of function calls. Each time a function is called, information about that call is placed on the stack. When the function finishes and returns, its information is removed, and execution resumes in the function that called it.

Anna
For our reverse-array example, the calls store the array and their index: first index zero, then one, then two, and finally three. Index three reaches the base case and returns. Then the index-two call resumes and prints three; next the index-one call prints two; finally the index-zero call prints one.

Michael
That’s the internal reason for the return order. The call at index three is the last one added, so it finishes first. Then the earlier calls finish in reverse order. When every call has returned and the stack is empty, the overall operation is done.

Anna
Trees are a good example of where this pattern can make code feel natural. Suppose each tree node has a left child and a right child, and we want a preorder traversal. If the node is null, return. Otherwise, visit the current node, then recursively traverse its left child, then recursively traverse its right child.

Michael
That short description mirrors the structure of the tree: do the work for this node, and ask the same question about each child. The session pointed out that recursion is also useful in sorting, linked lists, trees, graphs, and dynamic programming. It’s a foundational technique that can reappear as these topics get more advanced.

Anna
And recursion is language-neutral. The examples may be shown in Java, but the same ideas apply in Python, Scala, and other languages. The key concepts are the base case, the work for the current call, and a recursive call that makes progress—not a particular programming-language syntax.

Michael
One challenge introduced for further practice is the Tower of Hanoi. There are three towers, and a stack of coins of different sizes starts on the first tower, with the largest at the bottom and the smallest at the top. The goal is to move the stack to another tower, using the third as an auxiliary tower.

Anna
There are two constraints: move only one coin at a time, and never place a larger coin on top of a smaller one. The challenge is to work out how to move all n coins and how many moves it takes. It’s a classic example of a problem whose repeating structure makes recursion a useful way to reason about a solution.

Michael
The suggested approach was to try solving it before looking up a solution, then return to it in the next session. That practice matters: recognizing a recursive pattern yourself is part of learning to think recursively, rather than just recognizing code after someone else has written it.

Anna
So here’s the complete mental checklist: identify the smallest case that can stop immediately; decide what work belongs to the current call; make a recursive call on a smaller or otherwise simpler version of the problem; and check that the new input really moves toward the stopping case.

Michael
Then trace both directions. Follow the calls down to the base case, and don’t stop there—trace what happens as they return. That’s when you’ll see whether work happens before or after the recursive call, and why the output order may be forward or reversed.

Anna
Finally, ask whether recursion is the right tool for the problem. It can make a structured solution cleaner and more intuitive, but its call stack takes extra space, and it isn’t automatically faster than iteration. Understanding both the structure and the trade-offs is what makes the technique useful in practice.

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