Fibonacci Numbers - Coderust_ Hacking the Coding Interview
Fibonacci Numbers - Coderust_ Hacking the Coding Interview
Fibonacci Numbers
Find the nth Fibonacci number in the Fibonacci sequence.
• Description
• Hints
• Try it yourself
• Solution 1: recursive
• Runtime complexity
• Memory complexity
• Solution 2: iterative
• Runtime complexity
• Memory complexity
Description #
The Fibonacci numbers form a sequence, known as the Fibonacci
sequence where each number is the sum of two preceding ones, starting
from 0 and 1.
F0 = 0
F1 = 1
Fn = Fn−1 + Fn−2 , for n ≥ 2
Write a code to find the nth Fibonacci number in the Fibonacci sequence.
https://www.educative.io/courses/coderust-hacking-the-coding-interview/lAEM 1/6
02/11/2020 Fibonacci Numbers - Coderust: Hacking the Coding Interview
Hints #
You may use recursion for a brute force solution.
Think along the lines of dynamic programming for an optimized
solution.
Try it yourself #
C++ Java Python JS Ruby
int get_fibonacci(int n) {
//TODO: Write - Your - Code
return -1;
}
Solution 1: recursive #
Runtime complexity #
The runtime complexity of this solution is exponential, O(2n ).
Memory complexity #
The memory complexity of this solution is linear, O(n).
Let’s see how we can calculate the 4th Fibonacci number using this
approach:
https://www.educative.io/courses/coderust-hacking-the-coding-interview/lAEM 2/6
02/11/2020 Fibonacci Numbers - Coderust: Hacking the Coding Interview
fib(4)
1 of 7
int get_fibonacci(int n) {
if (n == 0 || n == 1) {
return n;
}
return get_fibonacci(n - 1) + get_fibonacci(n - 2);
}
int main() {
vector<int> inputs = {1, 5, 7, 10};
Solution 2: iterative #
Runtime complexity #
The runtime complexity of this solution is linear, O(n).
Memory complexity #
https://www.educative.io/courses/coderust-hacking-the-coding-interview/lAEM 3/6
02/11/2020 Fibonacci Numbers - Coderust: Hacking the Coding Interview
fib(0) = 0
fib(4) fib(2)
fib(1) = 1
fib(0) = 0
fib(3) fib(2)
fib(1) = 1
fib(1) = 1
For calculating Fn , we only need to track Fn−1 and Fn−2 , and we will have
a linear solution.
Initial state
n_1 = 1 n_2 = 0
1 of 4
https://www.educative.io/courses/coderust-hacking-the-coding-interview/lAEM 4/6
02/11/2020 Fibonacci Numbers - Coderust: Hacking the Coding Interview
int get_fibonacci(int n) {
if (n == 0 || n == 1) {
return n;
}
int n_1 = 1;
int n_2 = 0;
int res = 0;
n_2 = n_1;
n_1 = res;
}
return res;
}
int main() {
vector<int> inputs = {1, 5, 7, 10};
Back Next
Mark as Completed
23% completed, meet the criteria and claim your course certi cate!
Buy Certificate
Ask a Question
Report an
(https://discuss.educative.io/tag/ bonacci-numbers__dynamic-
Issue
programming__coderust-hacking-the-coding-interview)
https://www.educative.io/courses/coderust-hacking-the-coding-interview/lAEM 5/6
02/11/2020 Fibonacci Numbers - Coderust: Hacking the Coding Interview
https://www.educative.io/courses/coderust-hacking-the-coding-interview/lAEM 6/6