From c2a50029b4cac7243625cd8e4c5f45c9eba258d8 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 12:58:59 +0530 Subject: [PATCH 1/9] update: add new stack problem --- .../Stack/2-stacks-using1-array/index.js | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/_DataStructures_/Stack/2-stacks-using1-array/index.js diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js new file mode 100644 index 00000000..47219a05 --- /dev/null +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -0,0 +1,115 @@ +class TwoStacks { + constructor() { + this.data = []; + this.top1 = null; + this.top2 = null; + this.stack1Count = 0; + this.stack2Count = 0; + } + + push1(value) { + if (!this.data.length) { + this.data.push(value); + this.top1 = value; + this.stack1Count += 1; + } else { + const arr = []; + for (let i = 0; i < this.stack1Count; i += 1) { + arr.push(this.data[i]); + } + arr.push(value); + for (let i = this.stack1Count; i < this.data.length; i += 1) { + arr.push(this.data[i]); + } + this.data = arr; + this.top1 = value; + this.stack1Count += 1; + } + } + + push2(value) { + if (!this.data.length) { + this.data.push(value); + this.top2 = value; + this.stack2Count += 1; + } else { + const arr = []; + for (let i = 0; i < this.stack1Count; i += 1) { + arr.push(this.data[i]); + } + arr.push(value); + + for (let i = this.stack1Count; i < this.data.length; i += 1) { + arr.push(this.data[i]); + } + this.data = arr; + this.top2 = value; + this.stack2Count += 1; + } + } + + pop1() { + if (!this.top1) { + return null; + } + + const indexOfTop1 = this.data.indexOf(this.top1); + const arr = []; + + delete this.data[indexOfTop1]; + + this.data.forEach(el => arr.push(el)); + + const oldTop = this.top1; + this.top1 = this.data[indexOfTop1 - 1]; + this.data = arr; + return oldTop; + } + + pop2() { + return this.data.pop(); + } +} + +module.exports = TwoStacks; + +/** Test cases */ + +/* + +const s = new TwoStacks(); + +console.log(s.data); + +s.push1(5); +s.push1(4); +console.log(s.data); + + +s.push2(2) +s.push2(1); +console.log(s.data); + +s.push1(14); +console.log(s.data); +console.log(s.top1); + +console.log(s.pop1()) +console.log(s.data); + +console.log(s.pop1()) +console.log(s.data); + +console.log(s.pop2()) +console.log(s.data); + +console.log(s.pop2()) +console.log(s.data); + +console.log(s.pop2()) +console.log(s.data); + +console.log(s.pop1()) +console.log(s.data); + +*/ From 3eab705998e62965d50a7a737740814c6cc611f7 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 12:59:52 +0530 Subject: [PATCH 2/9] update: entry in readme --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bad182f9..013bb350 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,19 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct ### Data Structures - [Singly Linked List](src/_DataStructures_/LinkedList) + - [N Element From Last](src/_DataStructures_/LinkedList/element-from-last) - [Middle Node](src/_DataStructures_/LinkedList/middle-node) - [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list) - [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list) + - [Stack](src/_DataStructures_/Stack) + - [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack) - [Baseball Game](src/_DataStructures_/Stack/baseball-game) - [Minimum Stack](src/_DataStructures_/Stack/min-stack) - [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis) + - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) - [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) @@ -65,9 +69,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct ## CONTRIBUTION Guide -It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully. +It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully. - When adding a new **problem** with solution + - Take care of the filename convention (Very Important) - Problem statement should be there with examples - Make sure you add the Run Time complexity of your solution @@ -76,7 +81,8 @@ It's great to know that you want to contribute to this repo. Thanks for taking i - Strictly follow ESLINT rules - When adding a Unit Test + - Take care of the file name convention - Make sure CI (Travis) is passing - -Keep an eye on this guide, it's subjected to change frequently. + +Keep an eye on this guide, it's subjected to change frequently. From 3694b785981f100269010377760d7f53024ee407 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 13:01:17 +0530 Subject: [PATCH 3/9] update: infor added --- src/_DataStructures_/Stack/2-stacks-using1-array/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index 47219a05..1a764534 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -1,3 +1,10 @@ +/** + * So what special in this implementation? + * This deals with Dynamic array without a size hence the implemetation uses + * lot of space and I can sense that it can be further improved hence feel free + * to open PR + */ + class TwoStacks { constructor() { this.data = []; From 9e858fc5dc9264d697ce1832605357ee956e0b79 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 13:35:59 +0530 Subject: [PATCH 4/9] fix: maintaining rules of stack - O(1) insertion --- .../Stack/2-stacks-using1-array/index.js | 108 ++++-------------- 1 file changed, 22 insertions(+), 86 deletions(-) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index 1a764534..f948af44 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -6,117 +6,53 @@ */ class TwoStacks { - constructor() { + constructor(capacity) { this.data = []; - this.top1 = null; - this.top2 = null; - this.stack1Count = 0; - this.stack2Count = 0; + this.capacity = capacity; + this.top1 = -1; + this.top2 = capacity; } push1(value) { - if (!this.data.length) { - this.data.push(value); - this.top1 = value; - this.stack1Count += 1; - } else { - const arr = []; - for (let i = 0; i < this.stack1Count; i += 1) { - arr.push(this.data[i]); - } - arr.push(value); - for (let i = this.stack1Count; i < this.data.length; i += 1) { - arr.push(this.data[i]); - } - this.data = arr; - this.top1 = value; - this.stack1Count += 1; + if (this.top1 === -1 || this.top1 < this.top2 - 1) { + this.top1 += 1; + this.data[this.top1] = value; + return; } + throw new Error('Overflow'); } push2(value) { - if (!this.data.length) { - this.data.push(value); - this.top2 = value; - this.stack2Count += 1; - } else { - const arr = []; - for (let i = 0; i < this.stack1Count; i += 1) { - arr.push(this.data[i]); - } - arr.push(value); - - for (let i = this.stack1Count; i < this.data.length; i += 1) { - arr.push(this.data[i]); - } - this.data = arr; - this.top2 = value; - this.stack2Count += 1; + if (this.top2 <= this.capacity && this.top2 > this.top1) { + this.data[this.top2] = value; + this.top2 -= 1; + return; } + throw new Error('Overflow'); } - pop1() { - if (!this.top1) { - return null; - } - - const indexOfTop1 = this.data.indexOf(this.top1); - const arr = []; + pop1() {} - delete this.data[indexOfTop1]; - - this.data.forEach(el => arr.push(el)); - - const oldTop = this.top1; - this.top1 = this.data[indexOfTop1 - 1]; - this.data = arr; - return oldTop; - } - - pop2() { - return this.data.pop(); - } + pop2() {} } module.exports = TwoStacks; /** Test cases */ -/* - -const s = new TwoStacks(); +const s = new TwoStacks(3); +s.push1('a'); console.log(s.data); -s.push1(5); -s.push1(4); +s.push2('a2'); console.log(s.data); - -s.push2(2) -s.push2(1); +s.push1('b'); console.log(s.data); -s.push1(14); +s.push2('b2'); console.log(s.data); -console.log(s.top1); -console.log(s.pop1()) +s.push2('b3'); console.log(s.data); - -console.log(s.pop1()) -console.log(s.data); - -console.log(s.pop2()) -console.log(s.data); - -console.log(s.pop2()) -console.log(s.data); - -console.log(s.pop2()) -console.log(s.data); - -console.log(s.pop1()) -console.log(s.data); - -*/ From 77539778a6fc8f01d5c3f901b0a403c688ae8a7c Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 14:00:23 +0530 Subject: [PATCH 5/9] fix: correct implementation of all the methods, keeping rules inact --- .../Stack/2-stacks-using1-array/index.js | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index f948af44..e869bf0b 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -8,39 +8,60 @@ class TwoStacks { constructor(capacity) { this.data = []; - this.capacity = capacity; this.top1 = -1; this.top2 = capacity; + + this.capacity = capacity; + this.total = 0; } push1(value) { - if (this.top1 === -1 || this.top1 < this.top2 - 1) { + if (this.total >= this.capacity + 1) { + throw new Error('Overflow'); + } + if (this.top1 < this.top2 - 1) { this.top1 += 1; this.data[this.top1] = value; - return; + this.total += 1; } - throw new Error('Overflow'); } push2(value) { - if (this.top2 <= this.capacity && this.top2 > this.top1) { - this.data[this.top2] = value; + if (this.total >= this.capacity + 1) { + throw new Error('Overflow'); + } + if (this.top1 < this.top2 - 1) { this.top2 -= 1; - return; + this.data[this.top2] = value; + this.total += 1; } - throw new Error('Overflow'); } - pop1() {} + pop1() { + if (this.top1 >= 0) { + const item = this.data[this.top1]; + this.top1 -= 1; + return item; + } + return -1; + } - pop2() {} + pop2() { + if (this.top2 < this.capacity) { + const item = this.data[this.top2]; + this.top2 += 1; + return item; + } + return -1; + } } module.exports = TwoStacks; /** Test cases */ -const s = new TwoStacks(3); +/* +const s = new TwoStacks(4); s.push1('a'); console.log(s.data); @@ -56,3 +77,11 @@ console.log(s.data); s.push2('b3'); console.log(s.data); + +console.log(s.pop2()); +console.log(s.data); + +console.log(s.pop1()); +console.log(s.data); + +*/ From c743abf1cb9951849344f7583733014ce66f103f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 14:04:32 +0530 Subject: [PATCH 6/9] update: delete the elements --- src/_DataStructures_/Stack/2-stacks-using1-array/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index e869bf0b..2ee74cbb 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -40,6 +40,7 @@ class TwoStacks { pop1() { if (this.top1 >= 0) { const item = this.data[this.top1]; + delete this.data[this.top1]; this.top1 -= 1; return item; } @@ -49,6 +50,7 @@ class TwoStacks { pop2() { if (this.top2 < this.capacity) { const item = this.data[this.top2]; + delete this.data[this.top2]; this.top2 += 1; return item; } From 258d4a535ca8479b1533b83dbe601a06f56a8f62 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 14:06:11 +0530 Subject: [PATCH 7/9] update: revision to comments --- src/_DataStructures_/Stack/2-stacks-using1-array/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index 2ee74cbb..1ddcee51 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -1,8 +1,6 @@ /** - * So what special in this implementation? - * This deals with Dynamic array without a size hence the implemetation uses - * lot of space and I can sense that it can be further improved hence feel free - * to open PR + * Revision to PR #35 where I implemented bullshit thinking of + * new breakthrough :D */ class TwoStacks { From 981a98b0ac4913906230f835e03479fc5f1b4335 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 23:49:24 +0530 Subject: [PATCH 8/9] fix: removed extra if for throwing error --- .../Stack/2-stacks-using1-array/index.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index 1ddcee51..2fb0f7a7 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -10,28 +10,19 @@ class TwoStacks { this.top2 = capacity; this.capacity = capacity; - this.total = 0; } push1(value) { - if (this.total >= this.capacity + 1) { - throw new Error('Overflow'); - } if (this.top1 < this.top2 - 1) { this.top1 += 1; this.data[this.top1] = value; - this.total += 1; } } push2(value) { - if (this.total >= this.capacity + 1) { - throw new Error('Overflow'); - } if (this.top1 < this.top2 - 1) { this.top2 -= 1; this.data[this.top2] = value; - this.total += 1; } } @@ -75,7 +66,10 @@ console.log(s.data); s.push2('b2'); console.log(s.data); -s.push2('b3'); +s.push2('d2'); +console.log(s.data); + +s.push2('c23'); console.log(s.data); console.log(s.pop2()); @@ -83,5 +77,4 @@ console.log(s.data); console.log(s.pop1()); console.log(s.data); - */ From 5156f8dc8c74dbe24cc179b6d6e50594c880401a Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Mon, 7 Oct 2019 12:08:58 +0530 Subject: [PATCH 9/9] update: thow overflow --- src/_DataStructures_/Stack/2-stacks-using1-array/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index 2fb0f7a7..8ad7931c 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -8,6 +8,7 @@ class TwoStacks { this.data = []; this.top1 = -1; this.top2 = capacity; + this.overflow = new Error('Overflow: Stack is full'); this.capacity = capacity; } @@ -16,6 +17,8 @@ class TwoStacks { if (this.top1 < this.top2 - 1) { this.top1 += 1; this.data[this.top1] = value; + } else { + throw this.overflow; } } @@ -23,6 +26,8 @@ class TwoStacks { if (this.top1 < this.top2 - 1) { this.top2 -= 1; this.data[this.top2] = value; + } else { + throw this.overflow; } }