0% found this document useful (0 votes)
2 views

Fibonacci Numbers - Coderust_ Hacking the Coding Interview

The document discusses how to find the nth Fibonacci number in the Fibonacci sequence, which is defined recursively. It presents two solutions: a recursive approach with exponential runtime complexity and linear memory complexity, and an iterative approach with linear runtime complexity and constant memory complexity. The document also includes code examples in multiple programming languages.

Uploaded by

AshishManchanda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Fibonacci Numbers - Coderust_ Hacking the Coding Interview

The document discusses how to find the nth Fibonacci number in the Fibonacci sequence, which is defined recursively. It presents two solutions: a recursive approach with exponential runtime complexity and linear memory complexity, and an iterative approach with linear runtime complexity and constant memory complexity. The document also includes code examples in multiple programming languages.

Uploaded by

AshishManchanda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

02/11/2020 Fibonacci Numbers - Coderust: Hacking the Coding Interview

Fibonacci Numbers
Find the nth Fibonacci number in the Fibonacci sequence.

We'll cover the following

• 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.

The Fibonacci numbers are defined as:

F0 = 0
F1 = 1
Fn = Fn−1 + Fn−2 , for n ≥ 2

By using the defintion above, the first 10 Fibonacci numbers are:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34

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).

The memory complexity of this recursive solution is O(n) as each


recursive call consumes memory on the stack.

A simplistic approach to get the nth Fibonacci number is to implement the


recurrence relation. Here is how it would look like for n = 4. This
solution is simple to implement but it is highly inefficient.

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

C++ Java Python JS Ruby

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};

for (int i = 0; i < inputs.size(); i++){


cout<< "get_fibonacci(" << inputs[i] << ") = " << get_fibonacci(inputs[i])<<e
}
}

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

The memory complexity of this solution is constant O(1).

There is a lot of duplication in the recursive solution as shown in the


highlighted area of the figure below:

fib(0) = 0
fib(4) fib(2)
fib(1) = 1

fib(0) = 0
fib(3) fib(2)
fib(1) = 1
fib(1) = 1

While calculating F4 , we are calculating F2 twice. This problem exhibits


the properties of overlapping subproblems and optimal substructure,
which can be solved using dynamic programming.

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

i = 2, represents the result = 0


Fibonacci number.
We will iterate till n=4.

1 of 4

C++ Java Python JS Ruby

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;

for (int i = 2; i <= n; ++i) {

res = n_1 + n_2;

n_2 = n_1;
n_1 = res;
}

return res;
}

int main() {
vector<int> inputs = {1, 5, 7, 10};

for (int i = 0; i < inputs.size(); i++){


cout<< "get_fibonacci(" << inputs[i] << ") = " << get_fibonacci(inputs[i])<<e
}
}

Back Next

Find K-Sum Subsets Largest Sum Subarray

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

You might also like