Compute five factorial recursively.
Acceptance criteria
- 1.#out should show result 120.
DSA Tutorial · Recursion
These 10 tasks go with the Recursion Explained chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Every recursive function is two things: a case small enough to answer outright, and a step that makes the problem smaller. Miss either one and it never ends.” .
1.Factorialexercise · easy · 10 min
Compute five factorial recursively.
Passes when: #out should show result 120.
2.Sum a listexercise · easy · 10 min · practice plan
Add every value using recursion.
Passes when: #out should show sum 6.
3.Fibonacci with a cacheexercise · easy · 10 min · practice plan
Compute the tenth Fibonacci number using memoisation.
Passes when: #out should show fib 55.
4.Count downexercise · easy · 10 min · practice plan
Recurse to zero and report the depth reached.
Passes when: #out should show depth 4.
5.Reverse recursivelyexercise · medium · 10 min · practice plan
Reverse a string with recursion.
Passes when: #out should show cba.
6.Fix the missing base casebug fix · easy · 8 min
Without a stopping condition the recursion runs away, so a guard is used here instead.
Passes when: #out should show result 120.
7.Fix the base case returning the wrong valuebug fix · easy · 8 min · practice plan
Returning one from an empty sum adds a phantom element.
Passes when: #out should show sum 6.
8.Fix the cache never readbug fix · medium · 8 min · practice plan
Storing results without checking them first gains nothing and here returns early.
Passes when: #out should show fib 55.
9.Fix the recursion that never shrinksbug fix · easy · 8 min · practice plan
Passing the same value each time makes no progress, so a guard stops it.
Passes when: #out should show depth 4.
10.Fix the reverse appending the wrong waybug fix · easy · 8 min · practice plan
Adding the character before the recursion rebuilds the original order.
Passes when: #out should show cba.
Between them these tasks cover Base cases, Recursive step and Memoisation.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.