From 97b727bd95af2b825926ce3ca733214c115683c6 Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Mon, 7 Oct 2019 22:53:05 +0530 Subject: [PATCH 1/8] handling fibonacci for negative numbers and 0 --- src/_Classics_/fibonacci/index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/_Classics_/fibonacci/index.js b/src/_Classics_/fibonacci/index.js index e0852c5f..6c61bdc1 100644 --- a/src/_Classics_/fibonacci/index.js +++ b/src/_Classics_/fibonacci/index.js @@ -1,9 +1,11 @@ +//The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21 // 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 < 3) { - return 1; + if (position <= 1) { + return position; } + // else the element in fibonacci sequence will be the sum of // element at position(p) (p -1) and (p - 2) return fibonacci(position - 2) + fibonacci(position - 1); @@ -24,8 +26,8 @@ function fibonacciMemoized(index, cache) { if (cache[index]) { return cache[index]; } else { - if (index < 3) { - return 1; + if (index <=1) { + return index; } else { cache[index] = fibonacciMemoized(index - 1, cache) + @@ -41,7 +43,9 @@ function fibonacciMemoized(index, cache) { function fibonacciTabular(n) { const table = [0, 1]; - + if (n <= 1) { + return n; + } for (let i = 2; i <= n; i += 1) { table[i] = table[i - 1] + table[i - 2]; } @@ -54,4 +58,4 @@ function fibonacciTabular(n) { // console.log(`Fib normal - ${fibonacci(number)}`); // console.log('--'); // console.log(`Fib memo - ${fibonacciMemoized(number)}`); -// console.log(`Fib table - ${fibonacciTabular(number)}`); +// console.log(`Fib table - ${fibonacciTabular(number)}`); \ No newline at end of file From b3139d7e10592cb32625bf30b010ccb1f2298cb9 Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Tue, 8 Oct 2019 21:32:23 +0530 Subject: [PATCH 2/8] fix fibonacci problem for negative numbers --- src/_Classics_/fibonacci/index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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]; From aa84fdcc22047ca89f052ef50a7a05f0272540ef Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Wed, 9 Oct 2019 02:23:15 +0530 Subject: [PATCH 3/8] add reverse first k elements of queue problem --- README.md | 2 + src/_DataStructures_/Queue/index.js | 11 +++- .../index.js | 61 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js diff --git a/README.md b/README.md index 665bc09c..c058dd8d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation) - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) + - [Reverse First k Elements of Queue](src/_DataStructures_/Stack/reverse-first-k-elements-of-queue) + - [Queue](src/_DataStructures_/Queue) diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index 99d1861c..67a4189d 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -15,6 +15,15 @@ class Queue { remove() { return this.data.pop(); } + isEmpty() { + return this.data.length == 0; + } + enqueue(element) { + this.data.push(element); + } + length() { + return this.data.length; + } } -module.exports = Queue; +module.exports = Queue; \ No newline at end of file diff --git a/src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js b/src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js new file mode 100644 index 00000000..8a724038 --- /dev/null +++ b/src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js @@ -0,0 +1,61 @@ +/** + * Reverse first K elements of a Queue + * Given an integer K and a queue of integers,reverse the order of the first K elements of the queue, leaving the other elements in the same relative order. + * Input : Q = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] k = 5 + * Output : Q = [ 20, 19, 18, 17, 16, 11, 12, 13, 14, 15] + * + */ +//const Queue = require.resolve('Queue'); + +const Queue = require('../../Queue/index'); + +const Stack = require('../index'); + +function reverseQueueElements(queue, k) { + let s = new Stack(); + + if (queue.isEmpty()) { + throw new Error("Empty Queue"); + } + if(k<=0 || k>queue.length()){ + throw new Error("Invalid K value"); + } + + for (let i = 0; i < k; i++) { + //push first k elements from queue and push in stack + s.push(queue.remove()); + } + + for (let i = 0; i < k; i++) { + //we reverse the k elements with the help of stack. + //pop all the k elements from stack and add to queue. + let no = s.pop(); + queue.add(no); + } + + for (let i = 0; i < queue.length() - k; i++) { + //bring the k elments again back to front to maintain the queue order. + queue.add(queue.peek()); + queue.remove(); + + } + return queue; +} + +/* +let queue = new Queue(); +queue.add(11); +queue.add(12); +queue.add(13); +queue.add(14); +queue.add(15); +queue.add(16); +queue.add(17); +queue.add(18); +queue.add(19); +queue.add(20); +console.log("Queue before:", queue.data); +let k = 5; +let reversedQueue=reverseQueueElements(queue, k); +console.log("Queue after:", queue.data); +*/ \ No newline at end of file From 820368d8325f60a6af4257aa5b219a224bebae31 Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Thu, 10 Oct 2019 23:05:27 +0530 Subject: [PATCH 4/8] update files --- README.md | 1 - .../index.js | 61 ------------------- 2 files changed, 62 deletions(-) delete mode 100644 src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js diff --git a/README.md b/README.md index 2508196c..0c52c6f5 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation) - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) - - [Reverse First k Elements of Queue](src/_DataStructures_/Stack/reverse-first-k-elements-of-queue) - [Queue](src/_DataStructures_/Queue) diff --git a/src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js b/src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js deleted file mode 100644 index 8a724038..00000000 --- a/src/_DataStructures_/Stack/reverse-first-k-elements-of-queue/index.js +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Reverse first K elements of a Queue - * Given an integer K and a queue of integers,reverse the order of the first K elements of the queue, leaving the other elements in the same relative order. - * Input : Q = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] k = 5 - * Output : Q = [ 20, 19, 18, 17, 16, 11, 12, 13, 14, 15] - * - */ -//const Queue = require.resolve('Queue'); - -const Queue = require('../../Queue/index'); - -const Stack = require('../index'); - -function reverseQueueElements(queue, k) { - let s = new Stack(); - - if (queue.isEmpty()) { - throw new Error("Empty Queue"); - } - if(k<=0 || k>queue.length()){ - throw new Error("Invalid K value"); - } - - for (let i = 0; i < k; i++) { - //push first k elements from queue and push in stack - s.push(queue.remove()); - } - - for (let i = 0; i < k; i++) { - //we reverse the k elements with the help of stack. - //pop all the k elements from stack and add to queue. - let no = s.pop(); - queue.add(no); - } - - for (let i = 0; i < queue.length() - k; i++) { - //bring the k elments again back to front to maintain the queue order. - queue.add(queue.peek()); - queue.remove(); - - } - return queue; -} - -/* -let queue = new Queue(); -queue.add(11); -queue.add(12); -queue.add(13); -queue.add(14); -queue.add(15); -queue.add(16); -queue.add(17); -queue.add(18); -queue.add(19); -queue.add(20); -console.log("Queue before:", queue.data); -let k = 5; -let reversedQueue=reverseQueueElements(queue, k); -console.log("Queue after:", queue.data); -*/ \ No newline at end of file From b4c9d03f60c2bbdd0b8f0b04ce0d2f6a47021ae5 Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Thu, 10 Oct 2019 23:07:34 +0530 Subject: [PATCH 5/8] remove queue updates --- src/_DataStructures_/Queue/index.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index 67a4189d..d51bcab0 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -15,15 +15,6 @@ class Queue { remove() { return this.data.pop(); } - isEmpty() { - return this.data.length == 0; - } - enqueue(element) { - this.data.push(element); - } - length() { - return this.data.length; - } } module.exports = Queue; \ No newline at end of file From 9549c5fa0dc5c94d8998d107dfd7bf1c2f936d17 Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Sun, 13 Oct 2019 23:10:38 +0530 Subject: [PATCH 6/8] add solution to sort a stack problem --- .../Stack/sort-a-stack/index.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/_DataStructures_/Stack/sort-a-stack/index.js diff --git a/src/_DataStructures_/Stack/sort-a-stack/index.js b/src/_DataStructures_/Stack/sort-a-stack/index.js new file mode 100644 index 00000000..2f5f7767 --- /dev/null +++ b/src/_DataStructures_/Stack/sort-a-stack/index.js @@ -0,0 +1,35 @@ +/** + * Sort a stack with the help of a temporary stack. + * Input:[1,10,21,3,9,-11,32] + * Output:[32,21,10,9,3,1,-11] +*/ +const Stack = require('../index'); + +function sortStack(stack) { + const tempStack = new Stack(); + while (!stack.isEmpty()) { + //pop the first element from stack + let temp = stack.pop(); + //for ascending order (tempStack.peek() < temp) + while (!tempStack.isEmpty() && tempStack.peek() > temp) { + stack.push(tempStack.pop()); + } + //push the first element(temp) onto tempStack if tempStack.peek() Date: Sun, 13 Oct 2019 23:17:17 +0530 Subject: [PATCH 7/8] update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dc772666..54971425 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation) - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) + - [Sort a Stack](src/_DataStructures_/Stack/sort-a-stack) + - [Queue](src/_DataStructures_/Queue) From bca7041e9971ea59dd1d773802d3177b696d7087 Mon Sep 17 00:00:00 2001 From: SumeetHaryani Date: Mon, 14 Oct 2019 22:02:33 +0530 Subject: [PATCH 8/8] add time compexity --- src/_DataStructures_/Stack/sort-a-stack/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/_DataStructures_/Stack/sort-a-stack/index.js b/src/_DataStructures_/Stack/sort-a-stack/index.js index 2f5f7767..cb166bc0 100644 --- a/src/_DataStructures_/Stack/sort-a-stack/index.js +++ b/src/_DataStructures_/Stack/sort-a-stack/index.js @@ -2,6 +2,7 @@ * Sort a stack with the help of a temporary stack. * Input:[1,10,21,3,9,-11,32] * Output:[32,21,10,9,3,1,-11] + * Time Complexity:O(N^2) */ const Stack = require('../index');