Factorial
Iteration / RecursionSolved Oct 27, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a non-negative integer N, calculate its factorial, denoted N!. N! is the product of all positive integers less than or equal to N. (0! = 1).ReadHide
Problem Description
Given a non-negative integer N, calculate its factorial, denoted N!. N! is the product of all positive integers less than or equal to N. (0! = 1).
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use an iterative loop starting from 1 up to N, multiplying the result at each step. Must handle data type overflow for large N.
Approaches
1. Iterative Method (Optimal) - O(N) ⭐
Idea: Iteration is generally preferred over recursion to avoid potential stack overflow issues and the overhead of function calls, while maintaining O(N) time complexity.
- Initialize result variable to 1.
- Loop from
i=1up toN. - In each step, multiply the result by
i. - Critical Note: Use a large enough data type (e.g.,
long longin C++,longin Java) to prevent integer overflow.
2. Recursive Method - O(N)
Idea: Define N! = N × (N-1)! with the base case 0! = 1. This is elegant but may have performance/stack depth issues for very large N.
Solution code
import java.util.Scanner;
class Solution {
// O(N) Time, O(1) Space - Iterative (Optimal)
public static long calculateFactorial_iterative(int N) {
if (N < 0) return -1;
if (N == 0 || N == 1) return 1;
// Use 'long' to prevent overflow for larger factorials
long result = 1;
for (int i = 2; i <= N; i++) {
result *= i;
}
return result;
}
// O(N) Time, O(N) Stack Space - Recursive (Brute Force/Alternative)
public static long calculateFactorial_recursive(int N) {
if (N < 0) return -1;
if (N == 0 || N == 1) return 1;
// Recursive call: N * (N-1)!
return (long)N * calculateFactorial_recursive(N - 1);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (!scanner.hasNextInt()) return;
int N = scanner.nextInt();
// Use the iterative O(N) approach
System.out.println(calculateFactorial_iterative(N));
scanner.close();
}
}Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language