Skip to main content
All Questions

Fibonacci Numbers

Easy

Write a function fib(n) that returns the nth Fibonacci number. The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1, which produces the following pattern:

1, 1, 2, 3, 5, 8, 13 ...

Examples

Python
fib(0) # => 1 fib(1) # => 1 fib(2) # => 2 fib(3) # => 3 ... fib(10) # => 89 fib(20) # => 10946

Related courses

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.

Course

SQL Interview Prep Course

Prepare for SQL interviews with lessons on joins, aggregations, and window functions, then practice real SQL interview questions in an interactive editor with expert solutions.