Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1789caf

Browse files
Merge branch 'master' into DoublyLinkList
2 parents 2db8441 + 25c7a15 commit 1789caf

File tree

9 files changed

+387
-7
lines changed

9 files changed

+387
-7
lines changed

‎README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,29 @@ 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)
29+
- [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation)
30+
- [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)
32+
2633

2734
- [Queue](src/_DataStructures_/Queue)
2835
- [Weave](src/_DataStructures_/Queue/weave)
2936

3037
- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)
38+
- [Suffix Tree](src/_DataStructures_/SuffixTree)
39+
3140
### Logical Problems
3241

3342
- [Anagrams](src/_Problems_/anagrams)
@@ -41,6 +50,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
4150
- [Get Maze Path](src/_Problems_/get_subsequence)
4251
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
4352
- [Get Max Char](src/_Problems_/maxchar)
53+
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)
4454
- [Merge 2 Sorted Arrays](src/_Problems_/merge-two-sorted-arrays)
4555
- [Palindrome](src/_Problems_/palindrome)
4656
- [Product of Elements](src/_Problems_/product-of-elements)
@@ -66,3 +76,25 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
6676

6777
- [Caeser Cipher](src/_Classics_/caeser_cipher)
6878
- [Fibonacci](src/_Classics_/fibonacci)
79+
80+
---
81+
82+
## CONTRIBUTION Guide
83+
84+
It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.
85+
86+
- When adding a new **problem** with solution
87+
88+
- Take care of the filename convention (Very Important)
89+
- Problem statement should be there with examples
90+
- Make sure you add the Run Time complexity of your solution
91+
- Please take care of the segregation of the Problems as per the given Folder Structure
92+
- It's great if you can add the **Unit Tests** to verify your solutions as well
93+
- Strictly follow ESLINT rules
94+
95+
- When adding a Unit Test
96+
97+
- Take care of the file name convention
98+
- Make sure CI (Travis) is passing
99+
100+
Keep an eye on this guide, it's subjected to change frequently.

‎src/_Classics_/fibonacci/index.js‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
//The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
12
// the algorithm has time complexity of O(n^2), very bad!
23
function fibonacci(position) {
34
// if position is 1 or 2, the number in fibonacci sequence will be 1
4-
if (position <3) {
5-
return 1;
5+
if (position <=1) {
6+
return position;
67
}
8+
79
// else the element in fibonacci sequence will be the sum of
810
// element at position(p) (p -1) and (p - 2)
911
return fibonacci(position - 2) + fibonacci(position - 1);
@@ -24,8 +26,8 @@ function fibonacciMemoized(index, cache) {
2426
if (cache[index]) {
2527
return cache[index];
2628
} else {
27-
if (index <3) {
28-
return 1;
29+
if (index <=1) {
30+
return index;
2931
} else {
3032
cache[index] =
3133
fibonacciMemoized(index - 1, cache) +
@@ -41,7 +43,9 @@ function fibonacciMemoized(index, cache) {
4143

4244
function fibonacciTabular(n) {
4345
const table = [0, 1];
44-
46+
if (n <= 1) {
47+
return n;
48+
}
4549
for (let i = 2; i <= n; i += 1) {
4650
table[i] = table[i - 1] + table[i - 2];
4751
}
@@ -54,4 +58,4 @@ function fibonacciTabular(n) {
5458
// console.log(`Fib normal - ${fibonacci(number)}`);
5559
// console.log('--');
5660
// console.log(`Fib memo - ${fibonacciMemoized(number)}`);
57-
// console.log(`Fib table - ${fibonacciTabular(number)}`);
61+
// console.log(`Fib table - ${fibonacciTabular(number)}`);

‎src/_DataStructures_/LinkedList/loop-in-list/index.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Floyd’s Cycle-Finding Algorithm
22

3-
function detechLoop(linkedList) {
3+
function detectLoop(linkedList) {
44
let slow = linkedList.getFirst();
55
let fast = linkedList.getFirst();
66

@@ -14,3 +14,7 @@ function detechLoop(linkedList) {
1414
}
1515
return false;
1616
}
17+
18+
module.exports = {
19+
detectLoop,
20+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { LinkedList } = require('../index');
2+
const { detectLoop } = require('.');
3+
4+
describe('Loop a LinkedList', () => {
5+
let loopList = null;
6+
let last = null;
7+
beforeEach(() => {
8+
loopList = new LinkedList();
9+
loopList.addAtBeginning('1');
10+
loopList.addAtEnd('2');
11+
loopList.addAtEnd('3');
12+
loopList.addAtEnd('4');
13+
loopList.addAtEnd('5');
14+
// Create loop in list
15+
last = loopList.getLast();
16+
last.next = loopList.getFirst();
17+
});
18+
19+
it('Should break for empty list', () => {
20+
loopList.delete();
21+
expect(() => detectLoop(loopList)).toThrow(TypeError);
22+
});
23+
24+
it('Should return `true` when looping list', () => {
25+
expect(detectLoop(loopList)).toEqual(true);
26+
});
27+
28+
it('Should return `false` for non loop list', () => {
29+
last.next = null; // remove loop in list
30+
expect(detectLoop(loopList)).toEqual(false);
31+
});
32+
33+
it('Should return `false` for non loop list', () => {
34+
const list = new LinkedList();
35+
list.addAtBeginning('1');
36+
list.addAtEnd('1');
37+
list.addAtEnd('1');
38+
expect(detectLoop(list)).toEqual(false);
39+
});
40+
});
Lines changed: 85 additions & 0 deletions
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+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Evaluation of Postfix Expression
3+
* Input:456*+
4+
* Output:34
5+
*/
6+
7+
const Stack = require('../index');
8+
9+
function evaluatePostfixExpression(expression) {
10+
let s = new Stack();
11+
for (let i = 0; i < expression.length; i++) {
12+
const char = expression[i];
13+
if (!isNaN(char)) {
14+
//if number push the char onto stack
15+
s.push(Number(char));
16+
} else {
17+
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
18+
//push the result to stack
19+
let val1 = s.pop();
20+
let val2 = s.pop()
21+
switch (char) {
22+
case '+':
23+
s.push(val2 + val1);
24+
break;
25+
case '-':
26+
s.push(val2 - val1);
27+
break;
28+
case '*':
29+
s.push(val2 * val1);
30+
break;
31+
case '/':
32+
s.push(val2 / val1);
33+
break;
34+
35+
}
36+
}
37+
}
38+
//pop the value of postfix expression
39+
return s.pop();
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Given an integer N, remove consecutive repeated digits from it.
3+
* Input:133445
4+
* Output:1345
5+
*/
6+
7+
const Stack = require('../index');
8+
9+
10+
function removeConsecutiveDigits(no) {
11+
let s = new Stack();
12+
let newNo = "";
13+
//initally push first digit into stack
14+
newNo += no[0];
15+
s.push(no[0]);
16+
for (let i = 1; i < no.length; i++) {
17+
const digit = no[i];
18+
//if stack top and incoming digit is same ignore it else append to newNo.
19+
if (s.peek() !== digit) {
20+
newNo += digit;
21+
s.push(digit);
22+
}
23+
}
24+
return newNo
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /