Hi, I'm Gayle Laakmann McDowell, author of Cracking the Coding Interview. In this video we're
going to cover some of the basics of
recursion. Recursion is a concept, not a
data structure nor an algorithm, but a way
that we solve a problem. So let's suppose
we have this problem. You have a computer
and you want to know how many dot txt
files are there on this computer?
Well you can certainly go into the root
directory and count how many are there
but then it has folders and each of these folders has subfolders and so on and
so on. So you need to actually go out and ask
all of them, and find all the dot txt
files across the whole computer which is
a bit annoying until we do this
recursively. So when we do it recursively
what we do is we say okay we might count
how many files are in our root directory
but then we go and add up how many are
in each folder and we go and ask them in
the very same process hey how many dot
txt files do you have? And they then know
to go out and ask their subfolders and
so on and so on. And that's what recursion is. So in this case what happens is the
following. We first call my computer, and
I ask my computer how many dot
txt files do you have? It says well I have
2 in my immediate folder, but then, in order to
get my final answer, I need to go after
homework, pictures, and music. So first we
go ask homework, and we say, "homework, how
many files do you have?" and that's what's
called recursing. And homework says "well let's see, I have one txt file but
let me go check with project 1 and
project 2." So we go to project 1 and we
do the same thing and eventually if we
do this enough times we'll go through all
of our folders and all of their
subfolders and so on and so on until we
get through all of the files and get our
final answer which in this case is 8. So
recursion is just a way of taking a
problem and breaking it down into
subproblems and then each of those
subproblems is generally broken down
into more and more and more sub problems.
Of course if we did this forever and we
didn't have a stopping case, then well we
would do it forever. So every recursion
must have what's called a base case or
like a stopping point. In this case the
base case is when we have a folder with
no subfolders then we'll just return
once we count how many files it has. But
let's look at another example. The
Fibonacci sequence is one of the
simplest examples of recursion. In fact
we often say that the Fibonacci sequence
is defined recursively. But this
definition in and of itself doesn't make a
lot of sense.
We need a starting point, we need to know
what is f of 0 or f of 1. Implementing this
recursively is now actually very natural.
So this code we'll call fib of n and that will then recur to fib of n minus 1
and fib of n minus 2. Eventually it'll
get back those answers and add those up
and return that value. When we call say, fib of n minus 1, it'll then do the same
thing and recursive and recurse and recurse. We'll
stop when we get down to the base cases
which are when n is 0 or 1.
Let's walk through this with a more
concrete example. So suppose we call this
fib of 4. Well fib of 4 will call fib of 3 and fib of 2. Fib of 3 will call fib of 2
and fib of 1. Fib of 2 will call
fib of 1 and fib of 0.  Ok now this fib of 1 will
return 1, fib of 0 will return 0 and fib of 2
will get back an answer of 1. Now fib
of 3 will get this answer
back of 1
this other fib of 1 will return 1
also and it will return 2, fib of 3.
Then fib of 4 has called fib of 3, now it has to
compute fib of 2. Fib of 2 goes out to
call fib of 1 and that hits the
base case, so returns 1, fib of 0
also hits a base case in returns 0. So fib of 2 returns 1 and then fib of 4 just has to add up
this 2 and this 1 to get back an
answer of 3. And so that computes the
Fibonacci, the fourth value Fibonacci
sequence of three. So one thing you might
have noticed is that we're repeating
work. We compute fib of 2 here, and
then we get back the value of 1 there. But then we also repeat the work
here and that's true we do. And so this
Fibonacci, this implementation Fibonacci
can get very very inefficient.
Now there's two things to know here. One
is there is a way of optimizing this
using a technique called memoization and
we'll cover that in a different section.
Another thing to note is that there is a
very well-known theorem that basically
states that anything that is in implemented
recursively can be implemented
iteratively. The reason why we often use
recursion is that for certain
problems the recursive implementation is
a lot easier to read and understand and
write than interative explanation.
Doesn't mean that we always want to use
recursion or we always want to use
iteration, it really just depends on the
problem. So this means for you and your
interview is it's very much a
case-by-case decision. Sometimes you want
to implement the iterative solution and
other times you want to implement the
recursive solution. But generally your
interviewer will be in there in the room
with you and so you don't have to decide
by yourself, you can ask your interviewer.
Recursion is a super popular topic for
interviews so I really really encourage
you to spend a lot of time really
practicing and making sure you
understand it. So with that said why
don't you try out some recursive
problems yourself.
Good luck.
