Skip to main content
All Questions

Fibonacci Numbers

Easy
Unlock detailed company stats for this questionUpgrade

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 Interviews

Prepare for SQL interviews by practicing syntax basics, aggregations, and window functions. Learn how to manipulate data, write complex queries, and analyze information from diverse data sets.