-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from sir-gon/feature/ctci_fibonacci_numbers
[Hacker Rank] Interview Preparation Kit: Recursion: Fibonacci Numbers…
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
.../interview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# [Recursion: Fibonacci Numbers](https://www.hackerrank.com/challenges/ctci-fibonacci-numbers) | ||
|
||
- Difficulty: `#easy` | ||
- Category: `#ProblemSolvingBasic` | ||
|
||
## The Fibonacci Sequence | ||
|
||
The Fibonacci sequence appears in nature all around us, | ||
in the arrangement of seeds in a sunflower and the spiral of a nautilus for example. | ||
|
||
The Fibonacci sequence begins with `fibonacci(0) = 0` and `fibonacci(1) = 1` | ||
as its first and second terms. After these first two elements, | ||
each subsequent element is equal to the sum of the previous two elements. | ||
|
||
Programmatically: | ||
|
||
- `fibonacci(0) = 0` | ||
- `fibonacci(1) = 1` | ||
- `fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)` | ||
|
||
Given , return the number in the sequence. | ||
|
||
## Example | ||
|
||
The Fibonacci sequence to `6` is `fs = [0, 1, 1, 2, 3, 5, 8]`. | ||
With zero-based indexing, `fs[5] = 5`. | ||
|
||
## Function Description | ||
|
||
Complete the recursive function `fibonacci` in the editor below. | ||
|
||
fibonacci has the following parameter(s): | ||
|
||
- `int n`: the index of the sequence to return | ||
|
||
## Returns | ||
|
||
- `int`: the element in the Fibonacci sequence | ||
|
||
## Input Format | ||
|
||
The integer `n`. | ||
|
||
## Constraints | ||
|
||
- $ 0 \leq n \leq 30 $ | ||
|
||
## Sample Input | ||
|
||
```text | ||
STDIN Function | ||
----- -------- | ||
3 n = 3 | ||
``` | ||
|
||
## Sample Output | ||
|
||
```text | ||
2 | ||
``` | ||
|
||
## Explanation | ||
|
||
The Fibonacci sequence begins as follows: | ||
|
||
```C | ||
fibonacci(0) = 0 | ||
fibonacci(1) = 1 | ||
fibonacci(2) = (0 + 1) = 1 | ||
fibonacci(3) = (1 + 1) = 2 | ||
fibonacci(4) = (1 + 2) = 3 | ||
fibonacci(5) = (2 + 3) = 5 | ||
fibonacci(6) = (3 + 5) = 8 | ||
``` | ||
... | ||
In the sequence above, `fibonacci(3)` is `2`. |
19 changes: 19 additions & 0 deletions
19
...rrank/interview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { logger as console } from '../../../logger'; | ||
|
||
import { fibonacci } from './ctci_fibonacci_numbers'; | ||
import TEST_CASES from './ctci_fibonacci_numbers.testcases.json'; | ||
|
||
describe('ctci_fibonacci_numbers', () => { | ||
it('fibonacci test cases', () => { | ||
expect.assertions(3); | ||
|
||
TEST_CASES.forEach((test) => { | ||
const answer = fibonacci(test.input); | ||
|
||
console.debug(`fibonacci(${test.input}) solution found: ${answer}`); | ||
|
||
expect(answer).toStrictEqual(test.expected); | ||
}); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
...nterview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.testcases.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[ | ||
{ | ||
"title": "Sample Test case 0", | ||
"input": 5, | ||
"expected": 5 | ||
}, | ||
{ | ||
"title": "Sample Test case 1", | ||
"input": 12, | ||
"expected": 144 | ||
}, | ||
{ | ||
"title": "Sample Test case 2", | ||
"input": 17, | ||
"expected": 1597 | ||
} | ||
] |
16 changes: 16 additions & 0 deletions
16
...hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.md]] | ||
*/ | ||
|
||
export function fibonacci(n: number): number { | ||
if (n == 0) { | ||
return 0; | ||
} | ||
if (n == 1) { | ||
return 1; | ||
} | ||
|
||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
} | ||
|
||
export default { fibonacci }; |