From dbfe9f144fce111e6e4d1433116d3dacc341328b Mon Sep 17 00:00:00 2001 From: maharshigor Date: Tue, 8 Oct 2019 18:09:34 +0530 Subject: [PATCH 01/21] return list instead of console.log --- src/_DataStructures_/DoublyLinkedList/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_DataStructures_/DoublyLinkedList/index.js b/src/_DataStructures_/DoublyLinkedList/index.js index 522924b9..40d6bf3e 100644 --- a/src/_DataStructures_/DoublyLinkedList/index.js +++ b/src/_DataStructures_/DoublyLinkedList/index.js @@ -55,10 +55,12 @@ class DoublyLinkedList { display() { let address = this.head.next; + let addresses = [] while (address !== this.tail) { - console.log(address.data); + addresses.push(address.data) address = address.next; } + return addresses } } From 7cfb06fd3f3f3f9ceec6b2b4046bc32f99474140 Mon Sep 17 00:00:00 2001 From: Glenn Forrest Date: Tue, 8 Oct 2019 21:38:33 +0800 Subject: [PATCH 02/21] Adds testing for the get mazePath problem #48 --- README.md | 2 +- .../get-mazePath/get-mazePath.test.js | 41 +++++++++++++++++++ src/_Problems_/get-mazePath/index.js | 9 +--- 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 src/_Problems_/get-mazePath/get-mazePath.test.js diff --git a/README.md b/README.md index 665bc09c..c1a3374a 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [FizzBuzz](src/_Problems_/fizzbuzz) - [String Permutaions](src/_Problems_/get-string-permutations) - [Get Subsequence](src/_Problems_/get_subsequence) -- [Get Maze Path](src/_Problems_/get_subsequence) +- [Get Maze Path](src/_Problems_/get-mazePath) - [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s) - [Get Max Char](src/_Problems_/maxchar) - [Get Smallest Common Number](src/_Problems_/get-smallest-common-number) diff --git a/src/_Problems_/get-mazePath/get-mazePath.test.js b/src/_Problems_/get-mazePath/get-mazePath.test.js new file mode 100644 index 00000000..b30f50af --- /dev/null +++ b/src/_Problems_/get-mazePath/get-mazePath.test.js @@ -0,0 +1,41 @@ +const { getMazePath } = require('.'); + +describe('Get maze path', () => { + it('returns all possible solutions for a 2x2 grid', () => { + const expectedSolutions = ['HHVV', 'HVHV', 'HVVH', 'VHHV', 'VHVH', 'VVHH']; + + expect(getMazePath(0, 0, 2, 2)).toEqual(expectedSolutions); + }); + + it('returns an even amount of horizontal and vertical movements', () => { + const solutions = getMazePath(0, 0, 3, 3); + + solutions.forEach(solution => { + expect(solution.length).toEqual(6); + + expect(solution.match(/H/g).length).toEqual(3); + expect(solution.match(/V/g).length).toEqual(3); + }); + }); + + it('returns the expected number of solutions based on given grids', () => { + expect(getMazePath(0, 0, 1, 1).length).toEqual(2); + expect(getMazePath(0, 0, 2, 2).length).toEqual(6); + expect(getMazePath(0, 0, 3, 3).length).toEqual(20); + expect(getMazePath(0, 0, 4, 4).length).toEqual(70); + + expect(getMazePath(1, 1, 4, 4).length).toEqual(20); + }); + + it('returns an empty array when the start and end coordinates are equal', () => { + const solutions = getMazePath(2, 2, 2, 2); + + expect(solutions).toEqual(['']); + }); + + it('returns an empty array when the start coordinates are greater than the end coordinates', () => { + const solutions = getMazePath(2, 2, 1, 1); + + expect(solutions).toEqual([]); + }); +}); \ No newline at end of file diff --git a/src/_Problems_/get-mazePath/index.js b/src/_Problems_/get-mazePath/index.js index 99aead53..cff29bee 100644 --- a/src/_Problems_/get-mazePath/index.js +++ b/src/_Problems_/get-mazePath/index.js @@ -7,10 +7,7 @@ // --->> er = end row // --->> ec = end column - - - -let getMazePath = (cr, cc, er, ec) => { +const getMazePath = (cr, cc, er, ec) => { if(cr == er && cc == ec) { //============POSITIVE BASE CASE=========== let br = []; br.push(''); @@ -37,6 +34,4 @@ let getMazePath = (cr, cc, er, ec) => { return myResult; } - -let path = getMazePath(0, 0, 2, 2); -console.log(path); \ No newline at end of file +module.exports = { getMazePath }; \ No newline at end of file From d153c6c668e6f3f57ee99f089dec21e0c4c67eb5 Mon Sep 17 00:00:00 2001 From: Kaspar Arme Date: Wed, 9 Oct 2019 09:10:22 +0300 Subject: [PATCH 03/21] Add unit tests to postfix expression evaluation function. Resolves #34 --- .../postfix-expression-evaluation/index.js | 8 ++- .../postfix-expression-evaluation.test.js | 55 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js index 3441abb5..9db393e9 100644 --- a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js @@ -15,9 +15,9 @@ function evaluatePostfixExpression(expression) { s.push(Number(char)); } else { // if char is an operator then pop two elements from stack, evaluate them accordingly based on operator. - //push the result to stack + //push the result to stack let val1 = s.pop(); - let val2 = s.pop() + let val2 = s.pop(); switch (char) { case '+': s.push(val2 + val1); @@ -38,3 +38,7 @@ function evaluatePostfixExpression(expression) { //pop the value of postfix expression return s.pop(); } + +module.exports = { + evaluatePostfixExpression, +}; diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js new file mode 100644 index 00000000..47e0de42 --- /dev/null +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js @@ -0,0 +1,55 @@ +const { evaluatePostfixExpression } = require('.'); + +describe('Postfix expression evaluation', function () { + it('should be a function', function () { + expect(typeof evaluatePostfixExpression).toEqual('function'); + }); + + it('should return a number', function () { + const expression = '11+'; + + expect(typeof evaluatePostfixExpression(expression)).toEqual('number') + }); + + it('should handle addition', function () { + const expression = '23+'; + const expected = 5; + + expect(evaluatePostfixExpression(expression)).toEqual(expected); + }); + + it('should handle subtraction', function () { + const expression = '54-'; + const expected = 1; + + expect(evaluatePostfixExpression(expression)).toEqual(expected); + }); + + it('should handle multiplication', function () { + const expression = '34*'; + const expected = 12; + + expect(evaluatePostfixExpression(expression)).toEqual(expected); + }); + + it('should handle division', function () { + const expression = '62/'; + const expected = 3; + + expect(evaluatePostfixExpression(expression)).toEqual(expected); + }); + + it('should handle negative numbers', function () { + const expression = '25-'; + const expected = -3; + + expect(evaluatePostfixExpression(expression)).toEqual(expected); + }); + + it('should handle multiple operators', function () { + const expression = '123*+'; + const expected = 7; + + expect(evaluatePostfixExpression(expression)).toEqual(expected); + }); +}); From 05184c42686e0e9d56e23706a39cc9a881a0fadc Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 16:16:41 +0530 Subject: [PATCH 04/21] update: find k-th max in BST --- .../Trees/BST/find-kth-max/index.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/_DataStructures_/Trees/BST/find-kth-max/index.js diff --git a/src/_DataStructures_/Trees/BST/find-kth-max/index.js b/src/_DataStructures_/Trees/BST/find-kth-max/index.js new file mode 100644 index 00000000..67f7aea2 --- /dev/null +++ b/src/_DataStructures_/Trees/BST/find-kth-max/index.js @@ -0,0 +1,35 @@ +const BST = require('../index'); + +// Inorder traversal returns a sorted array +function inOrderTraversal(root) { + if (root === null) return []; + let arr = []; + // traverse left + const left = inOrderTraversal(root.leftChild); + arr = [...left, root.value]; + const right = inOrderTraversal(root.rightChild); + return [...arr, ...right]; +} + +function findKthMax(rootNode, k) { + const arr = inOrderTraversal(rootNode); + return arr[arr.length - k]; +} + +// // create a BST +// const myBST = new BST(6); + +// myBST.add(2); +// myBST.add(19); +// myBST.add(14); +// myBST.add(8); +// myBST.add(5); +// myBST.add(12); +// myBST.add(33); +// myBST.add(52); +// myBST.add(1); + +// // find 3rd max +// console.log(findKthMax(myBST.root, 3)); + +module.exports = findKthMax; From 4a751475a28607f04cd03c57b95efe71ad644e34 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 16:17:28 +0530 Subject: [PATCH 05/21] update: entry in readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 665bc09c..bd35ee53 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) + - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From f3761352bd4dadaad45302391083ca5b8aa60579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B1=E2=88=82?= Date: Thu, 10 Oct 2019 16:24:35 +0530 Subject: [PATCH 06/21] fix: closing of `` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8389dab..d0120523 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) - - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) + - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From 0ff8d471dbb0a02dc0f857775929a930a09a49cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B1=E2=88=82?= Date: Thu, 10 Oct 2019 16:31:29 +0530 Subject: [PATCH 07/21] fix: typo error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d0120523..0bdad6f2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) - - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) + - [Find kth maximum in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From 0bf9860d52a0883ac7782bdda28ac31083862a4f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 16:36:06 +0530 Subject: [PATCH 08/21] update: find kth min & throw error for invalid K --- .../Trees/BST/find-kth-max/index.js | 3 ++ .../Trees/BST/find-kth-minimum/index.js | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/_DataStructures_/Trees/BST/find-kth-minimum/index.js diff --git a/src/_DataStructures_/Trees/BST/find-kth-max/index.js b/src/_DataStructures_/Trees/BST/find-kth-max/index.js index 67f7aea2..fd798fcd 100644 --- a/src/_DataStructures_/Trees/BST/find-kth-max/index.js +++ b/src/_DataStructures_/Trees/BST/find-kth-max/index.js @@ -13,6 +13,9 @@ function inOrderTraversal(root) { function findKthMax(rootNode, k) { const arr = inOrderTraversal(rootNode); + if (k < 0 || k > arr.lenth) { + throw new Error('Invalid value for K'); + } return arr[arr.length - k]; } diff --git a/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js b/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js new file mode 100644 index 00000000..a28115ad --- /dev/null +++ b/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js @@ -0,0 +1,40 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +// Inorder traversal returns a sorted array +function inOrderTraversal(root) { + if (root === null) return []; + let arr = []; + // traverse left + const left = inOrderTraversal(root.leftChild); + arr = [...left, root.value]; + const right = inOrderTraversal(root.rightChild); + return [...arr, ...right]; +} + +function findKthMin(rootNode, k) { + const arr = inOrderTraversal(rootNode); + if (k < 0 || k > arr.lenth) { + throw new Error('Invalid value for K'); + } + return arr[k - 1]; +} + +// // create a BST +// const myBST = new BST(6); + +// myBST.add(2); +// myBST.add(19); +// myBST.add(14); +// myBST.add(8); +// myBST.add(5); +// myBST.add(12); +// myBST.add(33); +// myBST.add(52); +// myBST.add(1); +// myBST.add(0); + +// // find 3rd max +// console.log(findKthMin(myBST.root, 3)); + +module.exports = findKthMin; From ca93db32f78a85c295fa95c405cdac516cd79a85 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 16:37:26 +0530 Subject: [PATCH 09/21] update: entry in README & folder rename --- README.md | 1 + .../Trees/BST/{find-kth-minimum => find-kth-min}/index.js | 0 2 files changed, 1 insertion(+) rename src/_DataStructures_/Trees/BST/{find-kth-minimum => find-kth-min}/index.js (100%) diff --git a/README.md b/README.md index 0bdad6f2..83f38022 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) - [Find kth maximum in a BST](src/_DataStructures_/Trees/BST/find-kth-max) + - [Find kth minimum in a BST](src/_DataStructures_/Trees/BST/find-kth-min) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems diff --git a/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js b/src/_DataStructures_/Trees/BST/find-kth-min/index.js similarity index 100% rename from src/_DataStructures_/Trees/BST/find-kth-minimum/index.js rename to src/_DataStructures_/Trees/BST/find-kth-min/index.js From b4507f155a1ac9effe467d2b4eb9a389363ec6ee Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 16:46:06 +0530 Subject: [PATCH 10/21] update: rename folder and fix condition for K --- src/_DataStructures_/Trees/{BST => BinarySearchTree}/Node.js | 0 .../Trees/{BST => BinarySearchTree}/find-kth-max/index.js | 2 +- .../Trees/{BST => BinarySearchTree}/find-kth-min/index.js | 2 +- src/_DataStructures_/Trees/{BST => BinarySearchTree}/index.js | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/Node.js (100%) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/find-kth-max/index.js (96%) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/find-kth-min/index.js (96%) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/index.js (100%) diff --git a/src/_DataStructures_/Trees/BST/Node.js b/src/_DataStructures_/Trees/BinarySearchTree/Node.js similarity index 100% rename from src/_DataStructures_/Trees/BST/Node.js rename to src/_DataStructures_/Trees/BinarySearchTree/Node.js diff --git a/src/_DataStructures_/Trees/BST/find-kth-max/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js similarity index 96% rename from src/_DataStructures_/Trees/BST/find-kth-max/index.js rename to src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js index fd798fcd..fb306ca1 100644 --- a/src/_DataStructures_/Trees/BST/find-kth-max/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js @@ -13,7 +13,7 @@ function inOrderTraversal(root) { function findKthMax(rootNode, k) { const arr = inOrderTraversal(rootNode); - if (k < 0 || k > arr.lenth) { + if (k <= 0 || k > arr.lenth) { throw new Error('Invalid value for K'); } return arr[arr.length - k]; diff --git a/src/_DataStructures_/Trees/BST/find-kth-min/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js similarity index 96% rename from src/_DataStructures_/Trees/BST/find-kth-min/index.js rename to src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js index a28115ad..e62468fc 100644 --- a/src/_DataStructures_/Trees/BST/find-kth-min/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js @@ -14,7 +14,7 @@ function inOrderTraversal(root) { function findKthMin(rootNode, k) { const arr = inOrderTraversal(rootNode); - if (k < 0 || k > arr.lenth) { + if (k <= 0 || k > arr.lenth) { throw new Error('Invalid value for K'); } return arr[k - 1]; diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BinarySearchTree/index.js similarity index 100% rename from src/_DataStructures_/Trees/BST/index.js rename to src/_DataStructures_/Trees/BinarySearchTree/index.js From ab8b0389a4e6448aa266a4d20a10ae79057aaa01 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 16:46:36 +0530 Subject: [PATCH 11/21] update: fix entries in readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 83f38022..545981ac 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,9 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) - [Trees](src/_DataStructures_/Trees) - - [Binary Search Tree](src/_DataStructures_/Trees/BST) - - [Find kth maximum in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - - [Find kth minimum in a BST](src/_DataStructures_/Trees/BST/find-kth-min) + - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) + - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) + - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From 81c88b86256862202501f41716b1aea5859cd5ce Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 19:06:51 +0530 Subject: [PATCH 12/21] update: find all ancestors of a node --- .../BinarySearchTree/find-ancestors/index.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js new file mode 100644 index 00000000..9ae63c08 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -0,0 +1,43 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +function findAncestors(root, value) { + /** + * search the given node and meanwhile + * keep pushing the visited nodes + */ + let arr = []; + if (root === null) return []; + if (value > root.value) { + // traverse right + const left = findAncestors(root.rightChild, value); + arr = [...arr, ...left]; + } + if (value < root.value) { + // traverse left + const right = findAncestors(root.leftChild, value); + arr = [...arr, ...right]; + } + if (root.value === value) return arr; + arr = [root.value, ...arr]; + return arr; +} + +// create a BST +// const myBST = new BST(6); +// myBST.add(4); +// myBST.add(9); +// myBST.add(2); +// myBST.add(5); +// myBST.add(14); +// myBST.add(8); +// myBST.add(12); +// myBST.add(10); + +// // find 3rd max +// // console.log(myBST.root); +// console.log(myBST.traversePreorder()); +// // console.log(myBST.root.rightChild); +// console.log(findAncestors(myBST.root, 10)); + +module.exports = findAncestors; From 252e912b853995b3d28a5982cb4ac8955ea2e039 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 19:07:38 +0530 Subject: [PATCH 13/21] update: entry in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 545981ac..0bb877c0 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) + - [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From c9e3a6a05d2139a425d787b4d7f3def99be6dfd3 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 19:43:45 +0530 Subject: [PATCH 14/21] update: reverse order of pushing nodes --- .../Trees/BinarySearchTree/find-ancestors/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js index 9ae63c08..f316400d 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -11,15 +11,15 @@ function findAncestors(root, value) { if (value > root.value) { // traverse right const left = findAncestors(root.rightChild, value); - arr = [...arr, ...left]; + arr = [...left, ...arr]; } if (value < root.value) { // traverse left const right = findAncestors(root.leftChild, value); - arr = [...arr, ...right]; + arr = [...right, ...arr]; } if (root.value === value) return arr; - arr = [root.value, ...arr]; + arr = [...arr, root.value]; return arr; } From c4a733a5198816e922bf6845df8b2ede8a52d00f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 19:59:57 +0530 Subject: [PATCH 15/21] update: height of BST --- .../BinarySearchTree/height-of-bst/index.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js new file mode 100644 index 00000000..88a5d6c5 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js @@ -0,0 +1,35 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +function findHeightOfBST(root) { + let leftHeight = 0; + let rightHeight = 0; + + if (root === null) return 0; + leftHeight = findHeightOfBST(root.leftChild); + rightHeight = findHeightOfBST(root.rightChild); + + if (leftHeight > rightHeight) { + return leftHeight + 1; + } + return rightHeight + 1; +} + +// create a BST +// const myBST = new BST(6); +// myBST.add(4); +// myBST.add(9); +// myBST.add(2); +// myBST.add(5); +// myBST.add(14); +// myBST.add(8); +// myBST.add(12); +// myBST.add(10); + +// // find 3rd max +// // console.log(myBST.root); +// console.log(myBST.traversePreorder()); +// // console.log(myBST.root.rightChild); +// console.log(findHeightOfBST(myBST.root)); + +module.exports = findHeightOfBST; From c95821c55534f929a73116d95cb0d280f36ecdd9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 20:00:38 +0530 Subject: [PATCH 16/21] update: entry in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0bb877c0..257bf17b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) - [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors) + - [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From a75e9506663a3e9e35ab90488c152cd34936a09c Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 21:38:30 +0530 Subject: [PATCH 17/21] update: Find k nodes from the root --- .../find-k-nodes-from-root/index.js | 31 +++++++++++++++++++ .../BinarySearchTree/find-kth-max/index.js | 1 + .../BinarySearchTree/find-kth-min/index.js | 1 - .../BinarySearchTree/height-of-bst/index.js | 2 -- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js new file mode 100644 index 00000000..79259a00 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js @@ -0,0 +1,31 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +function findKNodes(root, k) { + let arr = []; + + if (root === null) return []; + if (k === 0) return [...arr, root.value]; + + const left = findKNodes(root.leftChild, k - 1); + arr = [...arr, ...left]; + + const right = findKNodes(root.rightChild, k - 1); + arr = [...arr, ...right]; + return arr; +} + +// create a BST +// const myBST = new BST(6); + +// myBST.add(2); +// myBST.add(19); +// myBST.add(14); +// myBST.add(8); +// myBST.add(5); +// myBST.add(12); +// myBST.add(33); +// myBST.add(52); +// myBST.add(1); + +// console.log(findKNodes(myBST.root, 2)); diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js index fb306ca1..21ba18f7 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js @@ -1,3 +1,4 @@ +// eslint-disable-next-line no-unused-vars const BST = require('../index'); // Inorder traversal returns a sorted array diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js index e62468fc..ad18cdea 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js @@ -34,7 +34,6 @@ function findKthMin(rootNode, k) { // myBST.add(1); // myBST.add(0); -// // find 3rd max // console.log(findKthMin(myBST.root, 3)); module.exports = findKthMin; diff --git a/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js index 88a5d6c5..ad4f1ee7 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js @@ -26,10 +26,8 @@ function findHeightOfBST(root) { // myBST.add(12); // myBST.add(10); -// // find 3rd max // // console.log(myBST.root); // console.log(myBST.traversePreorder()); -// // console.log(myBST.root.rightChild); // console.log(findHeightOfBST(myBST.root)); module.exports = findHeightOfBST; From 6416cfcb7c9a3b94e307432795d4cc3769c21787 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Thu, 10 Oct 2019 21:39:29 +0530 Subject: [PATCH 18/21] update: entry in README --- README.md | 1 + .../Trees/BinarySearchTree/find-k-nodes-from-root/index.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index 257bf17b..37708c9e 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) - [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors) - [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst) + - [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js index 79259a00..25aa70d1 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js @@ -29,3 +29,5 @@ function findKNodes(root, k) { // myBST.add(1); // console.log(findKNodes(myBST.root, 2)); + +module.exports = findKNodes; From 597cd0e4c83a8e2fc5a659df512c4786abf36949 Mon Sep 17 00:00:00 2001 From: Sumeet Haryani Date: Fri, 11 Oct 2019 00:18:44 +0530 Subject: [PATCH 19/21] Fix Fibonacci problem for negative numbers (#55) * handling fibonacci for negative numbers and 0 * fix fibonacci problem for negative numbers --- README.md | 1 + src/_Classics_/fibonacci/index.js | 13 ++++++++++--- src/_DataStructures_/Queue/index.js | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 545981ac..0c52c6f5 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) + - [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) diff --git a/src/_Classics_/fibonacci/index.js b/src/_Classics_/fibonacci/index.js index 6c61bdc1..979fb5c8 100644 --- a/src/_Classics_/fibonacci/index.js +++ b/src/_Classics_/fibonacci/index.js @@ -2,8 +2,10 @@ // the algorithm has time complexity of O(n^2), very bad! function fibonacci(position) { // if position is 1 or 2, the number in fibonacci sequence will be 1 - if (position <= 1) { + if (position === 1 || position === 0) { return position; + } else if (position < 0) { + throw new Error('Invalid Position'); } // else the element in fibonacci sequence will be the sum of @@ -26,8 +28,11 @@ function fibonacciMemoized(index, cache) { if (cache[index]) { return cache[index]; } else { - if (index <=1) { + if (index === 1 || index === 0) { return index; + } else if (index < 0) { + throw new Error('Invalid Position'); + } else { cache[index] = fibonacciMemoized(index - 1, cache) + @@ -43,8 +48,10 @@ function fibonacciMemoized(index, cache) { function fibonacciTabular(n) { const table = [0, 1]; - if (n <= 1) { + if (n === 1 || n === 0) { return n; + } else if (n < 0) { + throw new Error('Invalid Position'); } for (let i = 2; i <= n; i += 1) { table[i] = table[i - 1] + table[i - 2]; diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index 99d1861c..d51bcab0 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -17,4 +17,4 @@ class Queue { } } -module.exports = Queue; +module.exports = Queue; \ No newline at end of file From 97f8f9489a0c1a964038168f1b0ad4297c6724e9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Fri, 11 Oct 2019 11:32:28 +0530 Subject: [PATCH 20/21] fix: change in code, return null when node not found --- .../BinarySearchTree/find-ancestors/index.js | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js index f316400d..e45cec7b 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -1,26 +1,31 @@ // eslint-disable-next-line no-unused-vars const BST = require('../index'); -function findAncestors(root, value) { +function searchAndPush(root, value, result) { /** * search the given node and meanwhile * keep pushing the visited nodes */ - let arr = []; - if (root === null) return []; - if (value > root.value) { - // traverse right - const left = findAncestors(root.rightChild, value); - arr = [...left, ...arr]; + if (root == null) { + return false; + } + if (root.value === value) { + return true; } - if (value < root.value) { - // traverse left - const right = findAncestors(root.leftChild, value); - arr = [...right, ...arr]; + if ( + searchAndPush(root.leftChild, value, result) + || searchAndPush(root.rightChild, value, result) + ) { + result.push(root.value); + return true; } - if (root.value === value) return arr; - arr = [...arr, root.value]; - return arr; + return false; +} + +function findAncestors(root, value) { + const result = []; + searchAndPush(root, value, result); + return result; } // create a BST @@ -34,10 +39,7 @@ function findAncestors(root, value) { // myBST.add(12); // myBST.add(10); -// // find 3rd max -// // console.log(myBST.root); -// console.log(myBST.traversePreorder()); -// // console.log(myBST.root.rightChild); // console.log(findAncestors(myBST.root, 10)); +// console.log(findAncestors(myBST.root, 101)); module.exports = findAncestors; From 28e73b5ae692a7344a8d1978a28589a76be29473 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Fri, 11 Oct 2019 12:12:32 +0530 Subject: [PATCH 21/21] update: fix the edge case using array approach --- .../BinarySearchTree/find-ancestors/index.js | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js index e45cec7b..4ccea5f9 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -1,31 +1,35 @@ // eslint-disable-next-line no-unused-vars const BST = require('../index'); -function searchAndPush(root, value, result) { +/** You should go through this conversation here: + * https://github.com/knaxus/problem-solving-javascript/pull/63 + */ + +function findAncestors(root, value) { /** * search the given node and meanwhile * keep pushing the visited nodes */ - if (root == null) { + if (root === null) return false; + + if (value < root.value) { + const left = findAncestors(root.leftChild, value); + if (left) { + return [...left, root.value]; + } return false; } - if (root.value === value) { - return true; - } - if ( - searchAndPush(root.leftChild, value, result) - || searchAndPush(root.rightChild, value, result) - ) { - result.push(root.value); - return true; + + if (value > root.value) { + const right = findAncestors(root.rightChild, value); + if (right) { + return [...right, root.value]; + } + return false; } - return false; -} -function findAncestors(root, value) { - const result = []; - searchAndPush(root, value, result); - return result; + if (value === root.value) return []; + return false; } // create a BST