Search⌘ K
AI Features

N-th Tribonacci Number

Explore how to calculate the N-th Tribonacci number, a sequence defined by the sum of the three preceding numbers. Learn to apply dynamic programming methods like memoization and tabulation to optimize computation. This lesson helps you understand problem constraints and implement solutions efficiently, preparing you for coding interview challenges.

Statement

Given a number n, calculate the corresponding Tribonacci number. The Tribonacci sequence TnT_n is defined as:

T0=0, T1=1, T2=1T_0 = 0,\space T_1 = 1,\space T_2 = 1, and  Tn+3=Tn+Tn+1+Tn+2, \space T_{n+3} = T_n + T_{n+1} + T_{n+2},\space for n>=0n >= 0

The input number, n, is a non-negative integer.

Constraints:

  • 00 \leq n 37\leq 37
  • The answer is guaranteed to fit within a 32-bit integer, i.e., answer 2311\leq 2 ^{31} - 1

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

N-th Tribonacci Number

1.

What is the 5th Tribonacci number?

A.

5

B.

7

C.

15

D.

4


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3

Try it yourself

Implement your solution in the following coding playground:

Go
usercode > main.go
package main
func tribonacci(n int) int {
// Replace this placeholder return statement with your code
return -1
}
N-th Tribonacci Number