Posts

Showing posts from February, 2024

Codingbat - Recursion

Image
Recursion - Recursion == Induction. - When dealing with recursion, think of proof by induction.     => Base case / Assumption that it is true at k / prove at k+1. How to tackle recursion. 1) Come up with the base cases.       - When to end the function? At which condition should it end? When will this function stop?           => Think the condition of arguments when the function is ended.      - Obvious base cases like when n == 0, string's length is 0 or string.length() < 2.      - Try to be as specific as possible when in comes to the base case.          => Reach the very end of execution. 2) Starting from the base case, try to solve the question when n == 2 or 3 .     - Approach with small n and outline the pattern. 2) Find a pattern and think what the new argument of next execution would be by the pattern . This will help outline the flow of the...