Skip to content

Commit 6531955

Browse files
Merge branch 'master' into master
2 parents b710c28 + 17eebb1 commit 6531955

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
1414
### Data Structures
1515

1616
- [Singly Linked List](src/_DataStructures_/LinkedList)
17+
1718
- [N Element From Last](src/_DataStructures_/LinkedList/element-from-last)
1819
- [Middle Node](src/_DataStructures_/LinkedList/middle-node)
1920
- [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list)
2021
- [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list)
22+
2123
- [Stack](src/_DataStructures_/Stack)
24+
2225
- [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack)
2326
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
2427
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
2528
- [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis)
2629
- [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation)
2730
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/ remove-consecutive-repeated-digits)
31+
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
2832

2933

3034
- [Queue](src/_DataStructures_/Queue)
@@ -43,6 +47,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
4347
- [Get Maze Path](src/_Problems_/get_subsequence)
4448
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
4549
- [Get Max Char](src/_Problems_/maxchar)
50+
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)
4651
- [Merge 2 Sorted Arrays](src/_Problems_/merge-two-sorted-arrays)
4752
- [Palindrome](src/_Problems_/palindrome)
4853
- [Product of Elements](src/_Problems_/product-of-elements)
@@ -68,9 +73,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
6873

6974
## CONTRIBUTION Guide
7075

71-
It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.
76+
It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.
7277

7378
- When adding a new **problem** with solution
79+
7480
- Take care of the filename convention (Very Important)
7581
- Problem statement should be there with examples
7682
- Make sure you add the Run Time complexity of your solution
@@ -79,7 +85,8 @@ It's great to know that you want to contribute to this repo. Thanks for taking i
7985
- Strictly follow ESLINT rules
8086

8187
- When adding a Unit Test
88+
8289
- Take care of the file name convention
8390
- Make sure CI (Travis) is passing
84-
85-
Keep an eye on this guide, it's subjected to change frequently.
91+
92+
Keep an eye on this guide, it's subjected to change frequently.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Revision to PR #35 where I implemented bullshit thinking of
3+
* new breakthrough :D
4+
*/
5+
6+
class TwoStacks {
7+
constructor(capacity) {
8+
this.data = [];
9+
this.top1 = -1;
10+
this.top2 = capacity;
11+
this.overflow = new Error('Overflow: Stack is full');
12+
13+
this.capacity = capacity;
14+
}
15+
16+
push1(value) {
17+
if (this.top1 < this.top2 - 1) {
18+
this.top1 += 1;
19+
this.data[this.top1] = value;
20+
} else {
21+
throw this.overflow;
22+
}
23+
}
24+
25+
push2(value) {
26+
if (this.top1 < this.top2 - 1) {
27+
this.top2 -= 1;
28+
this.data[this.top2] = value;
29+
} else {
30+
throw this.overflow;
31+
}
32+
}
33+
34+
pop1() {
35+
if (this.top1 >= 0) {
36+
const item = this.data[this.top1];
37+
delete this.data[this.top1];
38+
this.top1 -= 1;
39+
return item;
40+
}
41+
return -1;
42+
}
43+
44+
pop2() {
45+
if (this.top2 < this.capacity) {
46+
const item = this.data[this.top2];
47+
delete this.data[this.top2];
48+
this.top2 += 1;
49+
return item;
50+
}
51+
return -1;
52+
}
53+
}
54+
55+
module.exports = TwoStacks;
56+
57+
/** Test cases */
58+
59+
/*
60+
const s = new TwoStacks(4);
61+
62+
s.push1('a');
63+
console.log(s.data);
64+
65+
s.push2('a2');
66+
console.log(s.data);
67+
68+
s.push1('b');
69+
console.log(s.data);
70+
71+
s.push2('b2');
72+
console.log(s.data);
73+
74+
s.push2('d2');
75+
console.log(s.data);
76+
77+
s.push2('c23');
78+
console.log(s.data);
79+
80+
console.log(s.pop2());
81+
console.log(s.data);
82+
83+
console.log(s.pop1());
84+
console.log(s.data);
85+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Get the common smallest number between two integer arrays
2+
3+
const getSmallestCommonNumber = (a1, a2) => {
4+
let map = {};
5+
let i = 0;
6+
let min;
7+
8+
while (a1.length > i || a2.length > i) {
9+
if (i < a1.length) {
10+
map[`${a1[i]}a`] = true;
11+
if (map[`${a1[i]}b`] && (min > a1[i] || !min)) {
12+
min = a1[i];
13+
}
14+
}
15+
16+
if (i < a2.length) {
17+
map[`${a2[i]}b`] = true;
18+
if (map[`${a2[i]}a`] && (min > a2[i] || !min)) {
19+
min = a2[i];
20+
}
21+
}
22+
23+
i++;
24+
}
25+
26+
return min || -1;
27+
};
28+
29+
module.exports = { getSmallestCommonNumber };

0 commit comments

Comments
 (0)