Skip to content

Commit 7c69524

Browse files
authored
Merge pull request #496 from sir-gon/feature/ctci_fibonacci_numbers
[Hacker Rank] Interview Preparation Kit: Recursion: Fibonacci Numbers…
2 parents db39c0b + 3728a64 commit 7c69524

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [Recursion: Fibonacci Numbers](https://www.hackerrank.com/challenges/ctci-fibonacci-numbers)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingBasic`
5+
6+
## The Fibonacci Sequence
7+
8+
The Fibonacci sequence appears in nature all around us,
9+
in the arrangement of seeds in a sunflower and the spiral of a nautilus for example.
10+
11+
The Fibonacci sequence begins with `fibonacci(0) = 0` and `fibonacci(1) = 1`
12+
as its first and second terms. After these first two elements,
13+
each subsequent element is equal to the sum of the previous two elements.
14+
15+
Programmatically:
16+
17+
- `fibonacci(0) = 0`
18+
- `fibonacci(1) = 1`
19+
- `fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)`
20+
21+
Given , return the number in the sequence.
22+
23+
## Example
24+
25+
The Fibonacci sequence to `6` is `fs = [0, 1, 1, 2, 3, 5, 8]`.
26+
With zero-based indexing, `fs[5] = 5`.
27+
28+
## Function Description
29+
30+
Complete the recursive function `fibonacci` in the editor below.
31+
32+
fibonacci has the following parameter(s):
33+
34+
- `int n`: the index of the sequence to return
35+
36+
## Returns
37+
38+
- `int`: the element in the Fibonacci sequence
39+
40+
## Input Format
41+
42+
The integer `n`.
43+
44+
## Constraints
45+
46+
- $ 0 \leq n \leq 30 $
47+
48+
## Sample Input
49+
50+
```text
51+
STDIN Function
52+
----- --------
53+
3 n = 3
54+
```
55+
56+
## Sample Output
57+
58+
```text
59+
2
60+
```
61+
62+
## Explanation
63+
64+
The Fibonacci sequence begins as follows:
65+
66+
```C
67+
fibonacci(0) = 0
68+
fibonacci(1) = 1
69+
fibonacci(2) = (0 + 1) = 1
70+
fibonacci(3) = (1 + 1) = 2
71+
fibonacci(4) = (1 + 2) = 3
72+
fibonacci(5) = (2 + 3) = 5
73+
fibonacci(6) = (3 + 5) = 8
74+
```
75+
76+
...
77+
78+
In the sequence above, `fibonacci(3)` is `2`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.md]]
3+
*/
4+
5+
export function fibonacci(n) {
6+
if (n === 0) {
7+
return 0;
8+
}
9+
if (n === 1) {
10+
return 1;
11+
}
12+
13+
return fibonacci(n - 1) + fibonacci(n - 2);
14+
}
15+
16+
export default { fibonacci };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { fibonacci } from './ctci_fibonacci_numbers.js';
5+
import TEST_CASES from './ctci_fibonacci_numbers.testcases.json';
6+
7+
describe('ctci_fibonacci_numbers', () => {
8+
it('fibonacci test cases', () => {
9+
expect.assertions(3);
10+
11+
TEST_CASES.forEach((test) => {
12+
const answer = fibonacci(test.input);
13+
14+
console.debug(`fibonacci(${test.input}) solution found: ${answer}`);
15+
16+
expect(answer).toStrictEqual(test.expected);
17+
});
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"input": 5,
5+
"expected": 5
6+
},
7+
{
8+
"title": "Sample Test case 1",
9+
"input": 12,
10+
"expected": 144
11+
},
12+
{
13+
"title": "Sample Test case 2",
14+
"input": 17,
15+
"expected": 1597
16+
}
17+
]

0 commit comments

Comments
 (0)