Skip to main content
Back to problems
Leetcode
Easy
Dynamic Programming
Recursion
Math
Fibonacci Number

Compute the n-th Fibonacci number in the sequence defined by the sum of the two previous terms.

Acceptance 0%
Problem Statement

Given a non-negative integer nn, return the nn-th Fibonacci number.

The Fibonacci sequence is defined as:

  • F(0)=0F(0) = 0
  • F(1)=1F(1) = 1
  • F(n)=F(n1)+F(n2)F(n) = F(n - 1) + F(n - 2) for n2n \ge 2

This is a classic warm-up problem for practicing iterative computation, recursion, and dynamic programming.

Input Format

  • One integer nn.
  • nn is the index in the Fibonacci sequence.

Output Format

  • Return a single integer: F(n)F(n).

Constraints

  • 0n0 \le n
  • The exact upper bound is platform-specific and not relied upon here.
  • Use a solution that is efficient enough for typical interview constraints.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 2

Output

1

Explanation

F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2

Input

n = 5

Output

5

Explanation

The sequence starts 0, 1, 1, 2, 3, 5, so F(5) = 5.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.