Give a real-world example of recursion
Filed under Optum (UnitedHealth Group)
Free read 2 of 3Recursion is when a function solves a problem by calling itself on smaller versions of the same problem, with a base case that stops the calls.
The question
Give a real-world example of recursion.
Approach
Recursion is when a function solves a problem by calling itself on smaller versions of the same problem, with a base case that stops the calls.
Russian nesting dolls: open a doll, find a smaller doll inside, until the smallest one (base case). Folders containing folders: to count all files, count the files here and recursively in each subfolder.
Factorial: n! = n * (n-1)! with 0! = 1. File-system traversal, tree traversal, merge sort and the Fibonacci sequence are classic uses.
Always have a base case, otherwise you hit stack overflow. Recursion uses call-stack memory; some problems are better iterative or with memoization.
Tips
- What they are testing: Interviewers like the folder-tree example because it is a real programming use, not only factorial.
- Common mistake: Forgetting to mention the base case, or giving a loop example that is not self-referential.
Related Optum (UnitedHealth Group) questions
Collected from public candidate posts and coding forums. Updated Nov 2026.