Skip to content
Veltoryx Technologies
FreeProblem 132● Easyleetcode ↗3 min readUpdated 30 October 2026

N-th Tribonacci Number

Compute the n-th Tribonacci number in O(n) time and O(1) space using three rolling variables.

01The problem

The Tribonacci sequence T is defined as follows:

T(0) = 0, T(1) = 1, T(2) = 1, and T(n) = T(n - 1) + T(n - 2) + T(n - 3) for n >= 3.

Given n, return the value of T(n).

02How to spot it

  • The problem directly specifies a linear recurrence where each term is the sum of the preceding three terms.
  • Plain recursion has exponential O(3^n) time complexity due to redundant subproblem evaluations.
  • Because only the last three terms are required to compute the next, three rolling variables give an optimal O(n) time and O(1) space solution.

03Pseudocode

text
if n == 0: return 0
if n <= 2: return 1
a, b, c = 0, 1, 1
repeat n - 2 times:
  a, b, c = b, c, a + b + c
return c

04Watch it work

Visualizer · step through the algorithmn = 4

sliding window [a, b, c]

00
11
12

computed

T(2) = 1

Initialize window with base terms: a = T(0) = 0, b = T(1) = 1, c = T(2) = 1.

Pseudocode

  1. if n == 0: return 0
  2. if n <= 2: return 1
  3. a, b, c = 0, 1, 1
  4. repeat n - 2 times:
  5. a, b, c = b, c, a + b + c
  6. return c

Variables

a
0
b
1
c
1
step 1 / 4

05Step-by-step dry run

For n = 4:

  1. Base cases check: n > 2.
  2. Initialize a = 0 (T0), b = 1 (T1), c = 1 (T2).
  3. Step 1 (i = 3):
    • next = a + b + c = 0 + 1 + 1 = 2.
    • Slide window: a = 1, b = 1, c = 2 (T3).
  4. Step 2 (i = 4):
    • next = a + b + c = 1 + 1 + 2 = 4.
    • Slide window: a = 1, b = 2, c = 4 (T4).
  5. Loop finishes (n - 2 = 2 iterations done). Return c = 4.

06Java solution

java
public class Main {
    public static int tribonacci(int n) {
        if (n == 0) return 0;
        if (n <= 2) return 1;
        int a = 0, b = 1, c = 1;
        for (int i = 3; i <= n; i++) {
            int next = a + b + c;
            a = b;
            b = c;
            c = next;
        }
        return c;
    }

    public static void main(String[] args) {
        System.out.println(tribonacci(4));
        System.out.println(tribonacci(25));
    }
}
Expected output
4
1389537

07Why it works

Each Tribonacci number depends strictly on the three preceding values. By maintaining variables a, b, and c for T(i-3), T(i-2), and T(i-1), computing next = a + b + c evaluates T(i) in O(1) time. Shifting variables forward prepares the state for step i + 1.

08Time and space

  • Time complexity: O(n) because the loop executes n - 2 times.
  • Space complexity: O(1) using three scalar integer registers.

09Edge cases

  • n = 0: Handled immediately by base check, returns 0.
  • n = 1 or n = 2: Handled immediately, returns 1.
  • n = 37: Max constraint; result fits comfortably in a standard 32-bit signed integer.

10Common mistakes

  • Using naive recursion: tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3) without memoization triggers time-limit exceeded (O(3^n)).
  • Allocating an array of size n: While O(n) space passes, using an array is unnecessary when only the previous 3 terms matter.

11Other approaches

  • Matrix Exponentiation: Can compute the n-th term in O(log n) time using [[1,1,1],[1,0,0],[0,1,0]]^(n-2), useful when n is up to 10^9.

12In the interview

  1. Point out that this is Fibonacci extended to 3 terms.
  2. Emphasize space optimization: "I will use 3 variables instead of an array of size n to achieve O(1) extra memory."

13Quiz

What is the space complexity of computing the n-th Tribonacci number using rolling variables?

  • (Recommended) O(1)
  • O(n)
  • O(log n)
  • O(3^n)

Answer: O(1). Only three scalar variables are updated on each step regardless of n.

14Solve it yourself

Open LeetCode #1137 and submit the rolling variable solution.