Step 01 Beginner Problems — Basic Recursion
Recursion Concepts with Parameters
There are two styles of writing a recursion: return the answer up the stack (functional style), or carry a running answer down through parameters (parameterised style). Real problems — traversing arrays, reversing in place, checking palindromes — usually need parameters that carry position and state, not just a shrinking number. This lesson teaches both styles on the same problem, then the parameter patterns behind this module's recursion exercises.
Two styles, one problem: sum of first N
Functional style — the function returns the answer, and each frame combines its own n with the returned smaller answer:
sum_func(n) = n + sum_func(n - 1), with sum_func(0) = 0.
Parameterised style — an extra parameter acc carries the running total down; when n reaches 0, acc IS the answer and is simply returned (or printed):
sum_param(n, acc): if n == 0, return acc; else sum_param(n - 1, acc + n).
Same complexity — O(n) time, O(n) stack — but different thinking: functional style asks 'how do I build the answer from a smaller answer?', parameterised asks 'what state do I need to carry so the base case can finish the job alone?' The second question is the one that unlocks array and string problems.
def sum_functional(n):
if n == 0:
return 0
return n + sum_functional(n - 1)
def sum_parameterised(n, acc=0):
if n == 0:
return acc
return sum_parameterised(n - 1, acc + n)
print(sum_functional(5), sum_parameterised(5)) # 15 15The index parameter: recursing over arrays
Array problems add an index parameter i that walks the structure while the data itself stays put. Sum of array elements: f(i) returns the sum of elements from i to the end; base case i == len(a) returns 0; combine a[i] + f(i + 1). Sortedness check: f(i) compares a[i] and a[i + 1] and ANDs with f(i + 1) — the base case is reaching the last index.
The discipline: the array is never copied or sliced per call (slicing a[i:] copies O(n) data per frame, quietly making your O(n) recursion O(n^2)); only the index moves. This is the single most important parameter habit.
def array_sum(a, i=0):
if i == len(a):
return 0
return a[i] + array_sum(a, i + 1)
print(array_sum([1, 2, 3, 4])) # 10Two parameters: the two-pointer recursion
Some problems need a window, not a position: reverse an array in place with f(l, r) — swap a[l] and a[r], then call f(l + 1, r - 1), stopping when l >= r. Palindrome checking is the same window with a comparison instead of a swap. Notice the base case is now a relationship between two parameters, and the state being carried is a shrinking interval — the exact skeleton that later becomes binary search (a window over a sorted array) and divide and conquer.
def reverse_rec(a, l, r):
if l >= r:
return
a[l], a[r] = a[r], a[l]
reverse_rec(a, l + 1, r - 1)
data = [1, 2, 3, 4, 5]
reverse_rec(data, 0, len(data) - 1)
print(data) # [5, 4, 3, 2, 1]What belongs in parameters vs the return value
Rule of thumb: parameters carry what the smaller problem needs to know (position, window, accumulated state, choices made so far); the return value carries what the caller needs back (a sum, a boolean, a count). Boolean searches often short-circuit: if the current check fails, return False immediately without exploring further — recursion respects early exits just like loops.
Carrying 'choices made so far' as a parameter (a current list being built) is exactly backtracking, which the Recursion module later in this sheet develops fully: subsequences, permutations and combinations are all parameterised recursions where the parameter is a partial answer.
Common mistakes
Slicing arrays/strings per call (hidden O(n) copy per frame). Mutating a shared accumulator AND returning it, double-counting on the way up. A base case written for one parameter while two are changing (the window recursion that forgets l >= r and only tests l == r walks off the end on even lengths). Default mutable parameters in Python (def f(x, acc=[]) shares one list across calls — use None and initialise inside). And forgetting that parameterised style still pays O(depth) stack: carrying an accumulator does not make Python/C++ tail-call optimise it away.
Key points
- Two styles: functional (answer returns up the stack) vs parameterised (answer accumulates in a parameter).
- Parameters carry position, window and state; the return value carries what the caller needs back.
- Never slice per call — pass an index; slicing copies O(n) per frame and silently makes recursion O(n^2).
- Two-parameter windows (l, r) are the skeleton for reversal, palindrome checks and later binary search.
- Partial-answer parameters are the bridge to backtracking (subsequences, permutations) in the Recursion module.
- Beware mutable default parameters in Python, and base cases that ignore one of two changing parameters.