N-th Tribonacci Number - Problem

The Tribonacci sequence Tn is defined as follows:

  • T0 = 0
  • T1 = 1
  • T2 = 1
  • Tn+3 = Tn + Tn+1 + Tn+2 for n ≥ 0

Given n, return the value of Tn.

Note: This is a variation of the famous Fibonacci sequence where each term is the sum of the three preceding ones instead of two.

Input & Output

Example 1 — Small Value
$ Input: n = 4
Output: 4
💡 Note: T(4) = T(1) + T(2) + T(3) = 1 + 1 + 2 = 4. The sequence is: T(0)=0, T(1)=1, T(2)=1, T(3)=2, T(4)=4.
Example 2 — Base Case
$ Input: n = 0
Output: 0
💡 Note: T(0) = 0 by definition. This is one of the base cases.
Example 3 — Another Base Case
$ Input: n = 2
Output: 1
💡 Note: T(2) = 1 by definition. Both T(1) and T(2) equal 1 as base cases.

Constraints

  • 0 ≤ n ≤ 37
  • The answer is guaranteed to fit in a 32-bit integer

Visualization

Tap to expand
Tribonacci Sequence: Each Term = Sum of Previous 3011247T(0)T(1)T(2)T(3)T(4)T(5)BaseBaseBase0+1+1=21+1+2=41+2+4=7Example: Find T(4)T(4) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4🎯 Key: Each number depends on exactly 3 previous values
Understanding the Visualization
1
Base Cases
T(0)=0, T(1)=1, T(2)=1 are given
2
Pattern
Each T(n) = T(n-1) + T(n-2) + T(n-3)
3
Result
Build sequence up to T(n)
Key Takeaway
🎯 Key Insight: Each Tribonacci number is the sum of the three preceding numbers, making it perfect for dynamic programming optimization
Asked in
Facebook 15 Google 12 Amazon 8
180.0K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen