Skip to content

Commit 42034d9

Browse files
authored
Merge pull request knaxus#1 from knaxus/master
Pulling latest changes
2 parents fc1f466 + e78eae5 commit 42034d9

File tree

15 files changed

+319
-16
lines changed

15 files changed

+319
-16
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
3030
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits)
3131
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
3232

33+
3334
- [Queue](src/_DataStructures_/Queue)
3435

3536
- [Weave](src/_DataStructures_/Queue/weave)
3637

3738
- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)
3839

3940
- [Trees](src/_DataStructures_/Trees)
40-
- [Binary Search Tree](src/_DataStructures_/Trees/BST)
41+
- [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree)
42+
- [Find k<sup>th</sup> maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max)
43+
- [Find k<sup>th</sup> minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min)
44+
- [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors)
45+
- [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst)
46+
- [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root)
4147
- [Suffix Tree](src/_DataStructures_/SuffixTree)
4248

4349
### Logical Problems
@@ -50,7 +56,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
5056
- [FizzBuzz](src/_Problems_/fizzbuzz)
5157
- [String Permutaions](src/_Problems_/get-string-permutations)
5258
- [Get Subsequence](src/_Problems_/get_subsequence)
53-
- [Get Maze Path](src/_Problems_/get_subsequence)
59+
- [Get Maze Path](src/_Problems_/get-mazePath)
5460
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
5561
- [Get Max Char](src/_Problems_/maxchar)
5662
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)

src/_Classics_/fibonacci/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// the algorithm has time complexity of O(n^2), very bad!
33
function fibonacci(position) {
44
// if position is 1 or 2, the number in fibonacci sequence will be 1
5-
if (position <= 1) {
5+
if (position === 1 || position === 0) {
66
return position;
7+
} else if (position < 0) {
8+
throw new Error('Invalid Position');
79
}
810

911
// else the element in fibonacci sequence will be the sum of
@@ -26,8 +28,11 @@ function fibonacciMemoized(index, cache) {
2628
if (cache[index]) {
2729
return cache[index];
2830
} else {
29-
if (index <=1) {
31+
if (index === 1 || index === 0) {
3032
return index;
33+
} else if (index < 0) {
34+
throw new Error('Invalid Position');
35+
3136
} else {
3237
cache[index] =
3338
fibonacciMemoized(index - 1, cache) +
@@ -43,8 +48,10 @@ function fibonacciMemoized(index, cache) {
4348

4449
function fibonacciTabular(n) {
4550
const table = [0, 1];
46-
if (n <= 1) {
51+
if (n === 1 || n === 0) {
4752
return n;
53+
} else if (n < 0) {
54+
throw new Error('Invalid Position');
4855
}
4956
for (let i = 2; i <= n; i += 1) {
5057
table[i] = table[i - 1] + table[i - 2];

src/_DataStructures_/DoublyLinkedList/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ class DoublyLinkedList {
5555

5656
display() {
5757
let address = this.head.next;
58+
let addresses = []
5859
while (address !== this.tail) {
59-
console.log(address.data);
60+
addresses.push(address.data)
6061
address = address.next;
6162
}
63+
return addresses
6264
}
6365
}
6466

src/_DataStructures_/Queue/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ class Queue {
1717
}
1818
}
1919

20-
module.exports = Queue;
20+
module.exports = Queue;

src/_DataStructures_/Stack/postfix-expression-evaluation/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function evaluatePostfixExpression(expression) {
1515
s.push(Number(char));
1616
} else {
1717
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
18-
//push the result to stack
18+
//push the result to stack
1919
let val1 = s.pop();
20-
let val2 = s.pop()
20+
let val2 = s.pop();
2121
switch (char) {
2222
case '+':
2323
s.push(val2 + val1);
@@ -38,3 +38,7 @@ function evaluatePostfixExpression(expression) {
3838
//pop the value of postfix expression
3939
return s.pop();
4040
}
41+
42+
module.exports = {
43+
evaluatePostfixExpression,
44+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { evaluatePostfixExpression } = require('.');
2+
3+
describe('Postfix expression evaluation', function () {
4+
it('should be a function', function () {
5+
expect(typeof evaluatePostfixExpression).toEqual('function');
6+
});
7+
8+
it('should return a number', function () {
9+
const expression = '11+';
10+
11+
expect(typeof evaluatePostfixExpression(expression)).toEqual('number')
12+
});
13+
14+
it('should handle addition', function () {
15+
const expression = '23+';
16+
const expected = 5;
17+
18+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
19+
});
20+
21+
it('should handle subtraction', function () {
22+
const expression = '54-';
23+
const expected = 1;
24+
25+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
26+
});
27+
28+
it('should handle multiplication', function () {
29+
const expression = '34*';
30+
const expected = 12;
31+
32+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
33+
});
34+
35+
it('should handle division', function () {
36+
const expression = '62/';
37+
const expected = 3;
38+
39+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
40+
});
41+
42+
it('should handle negative numbers', function () {
43+
const expression = '25-';
44+
const expected = -3;
45+
46+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
47+
});
48+
49+
it('should handle multiple operators', function () {
50+
const expression = '123*+';
51+
const expected = 7;
52+
53+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
54+
});
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
/** You should go through this conversation here:
5+
* https://github.com/knaxus/problem-solving-javascript/pull/63
6+
*/
7+
8+
function findAncestors(root, value) {
9+
/**
10+
* search the given node and meanwhile
11+
* keep pushing the visited nodes
12+
*/
13+
if (root === null) return false;
14+
15+
if (value < root.value) {
16+
const left = findAncestors(root.leftChild, value);
17+
if (left) {
18+
return [...left, root.value];
19+
}
20+
return false;
21+
}
22+
23+
if (value > root.value) {
24+
const right = findAncestors(root.rightChild, value);
25+
if (right) {
26+
return [...right, root.value];
27+
}
28+
return false;
29+
}
30+
31+
if (value === root.value) return [];
32+
return false;
33+
}
34+
35+
// create a BST
36+
// const myBST = new BST(6);
37+
// myBST.add(4);
38+
// myBST.add(9);
39+
// myBST.add(2);
40+
// myBST.add(5);
41+
// myBST.add(14);
42+
// myBST.add(8);
43+
// myBST.add(12);
44+
// myBST.add(10);
45+
46+
// console.log(findAncestors(myBST.root, 10));
47+
// console.log(findAncestors(myBST.root, 101));
48+
49+
module.exports = findAncestors;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
function findKNodes(root, k) {
5+
let arr = [];
6+
7+
if (root === null) return [];
8+
if (k === 0) return [...arr, root.value];
9+
10+
const left = findKNodes(root.leftChild, k - 1);
11+
arr = [...arr, ...left];
12+
13+
const right = findKNodes(root.rightChild, k - 1);
14+
arr = [...arr, ...right];
15+
return arr;
16+
}
17+
18+
// create a BST
19+
// const myBST = new BST(6);
20+
21+
// myBST.add(2);
22+
// myBST.add(19);
23+
// myBST.add(14);
24+
// myBST.add(8);
25+
// myBST.add(5);
26+
// myBST.add(12);
27+
// myBST.add(33);
28+
// myBST.add(52);
29+
// myBST.add(1);
30+
31+
// console.log(findKNodes(myBST.root, 2));
32+
33+
module.exports = findKNodes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
// Inorder traversal returns a sorted array
5+
function inOrderTraversal(root) {
6+
if (root === null) return [];
7+
let arr = [];
8+
// traverse left
9+
const left = inOrderTraversal(root.leftChild);
10+
arr = [...left, root.value];
11+
const right = inOrderTraversal(root.rightChild);
12+
return [...arr, ...right];
13+
}
14+
15+
function findKthMax(rootNode, k) {
16+
const arr = inOrderTraversal(rootNode);
17+
if (k <= 0 || k > arr.lenth) {
18+
throw new Error('Invalid value for K');
19+
}
20+
return arr[arr.length - k];
21+
}
22+
23+
// // create a BST
24+
// const myBST = new BST(6);
25+
26+
// myBST.add(2);
27+
// myBST.add(19);
28+
// myBST.add(14);
29+
// myBST.add(8);
30+
// myBST.add(5);
31+
// myBST.add(12);
32+
// myBST.add(33);
33+
// myBST.add(52);
34+
// myBST.add(1);
35+
36+
// // find 3rd max
37+
// console.log(findKthMax(myBST.root, 3));
38+
39+
module.exports = findKthMax;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
// Inorder traversal returns a sorted array
5+
function inOrderTraversal(root) {
6+
if (root === null) return [];
7+
let arr = [];
8+
// traverse left
9+
const left = inOrderTraversal(root.leftChild);
10+
arr = [...left, root.value];
11+
const right = inOrderTraversal(root.rightChild);
12+
return [...arr, ...right];
13+
}
14+
15+
function findKthMin(rootNode, k) {
16+
const arr = inOrderTraversal(rootNode);
17+
if (k <= 0 || k > arr.lenth) {
18+
throw new Error('Invalid value for K');
19+
}
20+
return arr[k - 1];
21+
}
22+
23+
// // create a BST
24+
// const myBST = new BST(6);
25+
26+
// myBST.add(2);
27+
// myBST.add(19);
28+
// myBST.add(14);
29+
// myBST.add(8);
30+
// myBST.add(5);
31+
// myBST.add(12);
32+
// myBST.add(33);
33+
// myBST.add(52);
34+
// myBST.add(1);
35+
// myBST.add(0);
36+
37+
// console.log(findKthMin(myBST.root, 3));
38+
39+
module.exports = findKthMin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
function findHeightOfBST(root) {
5+
let leftHeight = 0;
6+
let rightHeight = 0;
7+
8+
if (root === null) return 0;
9+
leftHeight = findHeightOfBST(root.leftChild);
10+
rightHeight = findHeightOfBST(root.rightChild);
11+
12+
if (leftHeight > rightHeight) {
13+
return leftHeight + 1;
14+
}
15+
return rightHeight + 1;
16+
}
17+
18+
// create a BST
19+
// const myBST = new BST(6);
20+
// myBST.add(4);
21+
// myBST.add(9);
22+
// myBST.add(2);
23+
// myBST.add(5);
24+
// myBST.add(14);
25+
// myBST.add(8);
26+
// myBST.add(12);
27+
// myBST.add(10);
28+
29+
// // console.log(myBST.root);
30+
// console.log(myBST.traversePreorder());
31+
// console.log(findHeightOfBST(myBST.root));
32+
33+
module.exports = findHeightOfBST;

0 commit comments

Comments
 (0)