Fibonacci Number

Problem

Return F(n), where F(0)=0, F(1)=1 and F(n)=F(n-1)+F(n-2).

Examples

Example 1
Input: 10
Output: 55
Example 2
Input: 0
Output: 0

Input and output use JSON-compatible values. For linked-list and tree questions, arrays represent the node structure as described above. Your code should implement solve(input).

Write your solutionImplement solve(input). Your draft is saved in this browser.
Advertisement

How to solve Fibonacci Number

The Fibonacci sequence starts at 0, 1. The recurrence F(n)=F(nāˆ’1)+F(nāˆ’2) makes a simple recursive solution, but naive recursion repeats calls. Store intermediate values or iterate using two variables.

Time and space complexity

O(n) time and O(1) space for the iterative reference.

Practise and improve

Choose JavaScript, TypeScript or Python, complete the starter code, then select Run Tests. Request a hint when you need guidance, or choose View solution and confirm if you would like to study the reference implementation.