How to recognize
While there is a wide range of recurring problems, many
similar recurring problems persist
patterns. A good indication that there is a recurring
problem seems to be built with sub-problems.
Once again, practice makes perfect! The more problems you
have, the easier it will be to identify you
How to get closer
Recursive solutions, by definition, are solved from
subproblems. Many times this
it will simply mean to calculate f (n) by adding,
subtracting or otherwise
changing the solution to f (n-1). In other cases, you may
need to do something more competitive
creased. However, we recommend the following approach:
1. Think about what the subproblem is. How many subproblems
does f (n) depend on?
That is, in a recurring binary tree problem, each part is
likely to be based on two problems
slogans. In a linked list problem, there will probably only
2. Solutions for "lower case". That is, if you
need to calculate f (n), first calculate it for f (0) or
f (1). This is usually a hard-coded value.
3. Solutions for f (2).
the precise process to translate the solutions of the
subproblems into the actual solution.
5. Generalization for f (n).
"Bottom-up recycling" is often the simplest.
Sometimes, however, it can be
useful for tackling "top-down" problems, where you
just jump to a break
f (n) in its subproblems.
Things to keep in mind
1. All problems that can be solved recursively can be solved
in another way (however
the code can be much more complex). Before diving into
recursive code, ask
yourself how difficult it would be to implement this
algorithm otherwise. Discuss the
exchange with your interviewer.
2. Recursive algorithms can be very spatially efficient.
Each recurring call adds a new layer
for stacking, which means if your algorithm has recursive O
(n) calls, you use it
Memory O (n). Oh! Here's a reason an iterative algorithm
might be better.
orting and Searching
How to approach:
It is extremely valuable to understand common ranking
algorithms, as many are ranking or
Search solutions require adjustments to known classification
algorithms. Good focus when
you are given a question like this to run through the
various sorting algorithms and see if
one of them is very good.
Example: You have a very large set of 'Person' objects. Sort
the people in ascending order
Here we are given two interesting facts: (1) It is a large
set, so the efficiency is very good
important. (2) We sort by ages, so we know that the values
are in a small range. You
scanning the various classification algorithms, we might
notice that the classification cube
Be a perfect candidate for this algorithm. In fact, we can
make the small cubes (only 1 year
one) and find O (n) the current time.
Start at the beginning of an array and swap the first two
elements if the first is greater than
the second. Go to the next pair etc, sweeping the array
continuously until sorted.
0 (n A 2).
Find the smallest item using a linear scan and move it
forward. So get the latest
it is smaller and smoother, doing a linear scan again.
Continue doing this until all elements
instead. 0 (n A 2).
Order each pair of items. Then order the four items by
combining the two pairs. Then,
order the 8 elements, etc. 0 (n log n) worst and worst case
Select a random characteristic and divide the matrix so that
all the numbers are smaller than it.
They come in front of every element bigger than it. Then do
that for each half, then each quarry
ter, etc. 0 (n log n) expected, 0 (n A 2) in the worst case.
Divide the matrix into a limited number of deposits and then
order each deposit individually.
This gives time 0 (n + m), where n is the number of elements
and m is the given number