Recursion depth and the call stack
Intermediate
Recursion & structural decomposition
Created by Pavel
· 29.04.2026 at 19:04 UTC
Every recursive call consumes a stack frame in CPython: local variables, return address, and bookkeeping. Depth d means d frames simultaneously—fine when d is the logarithm of n (balanced divide-and-conquer), painful when d is proportional to n (deep chains of nested dicts or skewed trees).
sys.setrecursionlimit exists but is a band-aid: it does not change algorithmic depth, only how late the interpreter gives up. For data pipelines that process huge sequential structures, prefer loops, iterators, or chunked processing.
Euclidean GCD is a classic example of logarithmic-depth recursion: arguments shrink quickly. Naive recursion down a linked list of length one million is not.
sys.setrecursionlimit reference: [1].
Sources
University approvals: 0
Tasks
Card Info
- Topic: Recursion & structural decomposition
- Difficulty: Intermediate
- Completed: 0 users
Creator
Pavel