Skip to content

Fibonacci Number

EasyPython 3Dynamic programmingRecursionMath

The Fibonacci sequence starts with F(1) = 1 and F(2) = 1, and every later term is the sum of the two preceding ones: F(n) = F(n-1) + F(n-2).

You are given a positive integer n. Return F(n).

Constraints:
  - 1 <= n <= 10000
  - a naive doubly-recursive solution is exponential and will time out; your
    solution should run in O(n)
Examples:
  fibonacci(1)  == 1
  fibonacci(2)  == 1
  fibonacci(3)  == 2
  fibonacci(10) == 55

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…