Skip to content

Commit ad476e8

Browse files
committed
2 parents bf5a309 + 3d9b5c5 commit ad476e8

7 files changed

Lines changed: 180 additions & 0 deletions

File tree

interview/fibonacci-sum/Readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Write a program that
2+
prints 1 2 3 4 5 6 7 ... up to some +ve integer N
3+
the natural number series, but
4+
with all numbers that also show up in the fibonacci series replaced with -1
5+
so a refresher on fibonacci if you need it:
6+
the fibonacci series starts with the numbers 0 and 1
7+
and then you can generate all subsequent numbers by summing the previous 2 numbers in the series
8+
so the series goes 0 1 1 2 3 5 8 13 21 .. etc
9+
so in your program, it should print the natural number series, but any number that also occurs in the fibonacci series needs to be replaced with -1
10+
the output should look like -1 -1 -1 4 -1 6 7 -1 9 10 ... etc
11+
12+
## Example
13+
14+
```
15+
Given N = 7, arr will be [1,2,3,4,5,6,7] and output [-1, -1, -1, 4, -1, 6, 7]
16+
```
17+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var fibnnacciSumSeries = function(limit) {
2+
const list = [];
3+
const fibSequence = { 1: true };
4+
let left = 1, right = 1;
5+
for (let i = 1; i <= limit; i++) {
6+
if (fibSequence[i]) {
7+
list.push(-1);
8+
} else {
9+
list.push(i);
10+
}
11+
const temp = left;
12+
left = right;
13+
right = temp + right;
14+
fibSequence[right] = true;
15+
}
16+
return list;
17+
}
18+
19+
module.exports = fibnnacciSumSeries;
20+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var fibnnacciSumSeries = function(N) {
2+
const toReturn = [];
3+
let prevInt = 0, currentInt = 1, nextTerm;
4+
for (let i = 1; i <= N; i++) {
5+
toReturn.push(i);
6+
}
7+
for (let i = 1; i <= N; i++) {
8+
nextTerm = prevInt + currentInt;
9+
prevInt = currentInt;
10+
currentInt = nextTerm;
11+
const indexOfFibnaciNumber = toReturn.findIndex(each => each === nextTerm);
12+
if (indexOfFibnaciNumber > -1) {
13+
toReturn[indexOfFibnaciNumber] = -1;
14+
}
15+
}
16+
return toReturn;
17+
};
18+
19+
module.exports = fibnnacciSumSeries;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function isPerfectSquare(x)
2+
{
3+
let s = parseInt(Math.sqrt(x));
4+
return (s * s == x);
5+
}
6+
7+
// Returns true if n is a Fibinacci Number, else false
8+
function isFibonacci(n)
9+
{
10+
// n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both
11+
// is a perferct square
12+
return isPerfectSquare(5*n*n + 4) ||
13+
isPerfectSquare(5*n*n - 4);
14+
}
15+
16+
17+
var fibnnacciSumSeries = function(N) {
18+
const toReturn = [];
19+
for (let i = 1; i <= N; i++) {
20+
toReturn.push(isFibonacci(i) ? -1 : i);
21+
}
22+
return toReturn;
23+
};
24+
25+
module.exports = fibnnacciSumSeries;

interview/fibonacci-sum/test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var fibnnacciSumSeries = require('./fibnocci-sum-3');
2+
var expect = require('chai').expect;
3+
var assert = require('assert');
4+
5+
//add object to add custom tests
6+
let testSamples = [
7+
// {
8+
// N: 4,
9+
// output: [0, 1, 1, 2]
10+
// },
11+
{
12+
N: 7,
13+
output: [-1, -1, -1, 4, -1, 6, 7]
14+
},
15+
{
16+
N: 5,
17+
output: [-1, -1, -1, 4, -1]
18+
}
19+
];
20+
21+
//don't modify this
22+
testSamples.forEach((eachSample,index) => {
23+
let output = fibnnacciSumSeries(eachSample.N);
24+
describe('Function output should be equal to test sample output', function() {
25+
it('Test # ' + index, function() {
26+
assert.deepEqual(output,eachSample.output);
27+
});
28+
});
29+
});
30+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
var binarySearch = function(arr, valueToSearch, lowerIndex, upperIndex) {
3+
if (lowerIndex > upperIndex) {
4+
return -1;
5+
}
6+
const mid = parseInt((upperIndex + lowerIndex)/2);
7+
if (valueToSearch === arr[mid]) {
8+
console.log('found at ' + mid);
9+
return mid;
10+
}
11+
if (valueToSearch > arr[mid]) {
12+
lowerIndex = mid + 1;
13+
return binarySearch(arr, valueToSearch, lowerIndex, upperIndex);
14+
} else {
15+
upperIndex = mid - 1;
16+
return binarySearch(arr, valueToSearch, lowerIndex, upperIndex);
17+
}
18+
19+
}
20+
21+
const arr = [6, 8, 19, 20, 23, 41, 49, 53, 56];
22+
let indexOfValue = binarySearch(arr, 6, 0, arr.length - 1);
23+
console.log(indexOfValue, 'index of value');
24+
module.exports = binarySearch;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// O (n 2) worst case
2+
3+
function partition(arr, first, last) {
4+
const pivot = first;
5+
let lowerIndex = first + 1;
6+
let upperIndex = last;
7+
// call first element as pivot
8+
// loop till the higher and lower indices cross each other
9+
while (upperIndex >= lowerIndex) {
10+
if (arr[pivot] > arr[lowerIndex]) {
11+
lowerIndex++;
12+
} else if (arr[upperIndex] > arr[pivot]) {
13+
upperIndex--;
14+
} else if (arr[pivot] > arr[upperIndex] && arr[lowerIndex] > arr[upperIndex]) {
15+
// now lower index value is greater than pivot so we stop incrementing the lowerindex
16+
// we will decrement the upperIndex until we find a value that is less than pivot and lowerindex value we will switch array indices positions
17+
const temp = arr[upperIndex];
18+
arr[upperIndex] = arr[lowerIndex];
19+
arr[lowerIndex] = temp;
20+
upperIndex--;
21+
lowerIndex++;
22+
}
23+
}
24+
// swap pivot with uper
25+
const temp = arr[upperIndex];
26+
arr[upperIndex] = arr[pivot];
27+
arr[pivot] = temp;
28+
return upperIndex;
29+
}
30+
31+
function quickSort(arr, first, last) {
32+
if (first < last) {
33+
const pivot = partition(arr, first, last);
34+
quickSort(arr, first, pivot - 1);
35+
quickSort(arr, pivot + 1, last);
36+
}
37+
}
38+
39+
40+
const arr = [20, 6, 8, 53, 23, 87, 42, 19];
41+
console.log(arr, 'orignal');
42+
quickSort(arr, 0, (arr.length - 1));
43+
console.log(arr, 'sorted');
44+
45+
module.exports = quickSort;

0 commit comments

Comments
 (0)