Factorial
An easy Recursion problem included in Apna College, Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Recursion
- Sheets
- 2
- Core for
- 11 roles
- Platform
- GeeksforGeeks
The problem
Compute the factorial of a given non-negative integer.
Example 1
- Input
- n = 5
- Output
- 120
- Why
- 5! = 5 * 4 * 3 * 2 * 1 = 120.
Example 2
- Input
- n = 0
- Output
- 1
- Why
- The factorial of 0 is defined as 1.
Constraints
- 0 <= n <= 20
How to think about it
Updated 2026-09-09Multiplication accumulates backward from 1. The definition recurses toward zero, but because every recursive call suspends until its child returns, the work happens on the unwind. The base case gives you the starting identity, and each returning frame folds one multiplier into the running product.
Approaches, worst first
Direct recursion
time O(n) · space O(n)
Recurse on n - 1 and multiply the result by n on return. Simple and matches the mathematical formula directly, but allocates a call frame for each decrement and risks stack exhaustion on deep inputs.
Iterative accumulatorWrite this one
time O(n) · space O(1)
Maintain a running product starting at 1 and multiply upward through n in a loop. Eliminates call stack overhead completely while keeping arithmetic identical.
Where people lose marks · 2
- Returning 0 instead of 1 for n = 0. The definition specifies 0! = 1, and returning 0 collapses all higher recursive multiplications to 0 as well.
- Using a signed 32-bit integer accumulator. 20! is roughly 2.43 * 10^18, which overflows standard 32-bit integers at n = 13 and requires a 64-bit unsigned integer or BigInt.
The theory behind it
Recursion — the ground this problem stands on. All Recursion problems
What Recursion is
Recursion is a nesting doll that opens to reveal an identical smaller doll inside. In programming, a function solves a substantial problem by delegating smaller versions of the exact same question to fresh invocations of itself. Each invocation operates on shrunken input until hitting an irreducible foundation called a base case, which returns an immediate answer and permits the waiting cascade to resolve backwards.
When to reach for it
Reach for recursion when a problem possesses self-similar subproblems, such as traversing branched tree structures, exploring graph pathways, or generating combinations. Phrases asking for all permutations, subset generation, exhaustive maze navigation, or hierarchical file system traversals signal recursive decomposition. It is natural whenever the answer to a large instance depends on assembling identical solutions for smaller subsets.
How the pattern works
Structure every recursive method around two mandatory stages: the termination stop and the shrinking recurrence. Write the base condition first so the function exits before attempting further execution. Next, trust the recursive call to return valid answers for smaller inputs without mentally unwinding every level at once. Pass accumulation state forward through parameters, or combine child return values on the ascent phase once deeper calls return.
What each operation costs
| Operation | Time |
|---|---|
| call stack memory allocation per frame | O(d) |
| traversal of branching recursive call tree | O(b^d) |
| single branch linear recursive unwind | O(n) |
What usually goes wrong with Recursion
- Omitting a base case or writing a condition that input values leap over without triggering, triggering fatal call stack overflow crashes.
- Modifying shared mutable containers across sibling branches without undoing edits on backtracking steps, contaminating alternative search paths.
- Recomputing duplicate subproblems inside branching calls without memoizing past returns, causing execution times to explode exponentially.
Which roles need this problem
Recursion is a core topic for these 11 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including SDE / Backend Engineer, Full-Stack Developer, Security Engineer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Recursion problems
Problem set and role mapping as of .