Skip to content

Commit 7ca7bd9

Browse files
authored
Merge pull request knaxus#81 from knaxus/problems
Problems involving Queues
2 parents 5bec3a5 + 75b69be commit 7ca7bd9

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
3030
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits)
3131
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
3232

33-
3433
- [Queue](src/_DataStructures_/Queue)
3534

3635
- [Weave](src/_DataStructures_/Queue/weave)
36+
- [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k)
37+
- [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number)
38+
- [Queue using Stack](src/_DataStructures_/Queue/queue-using-stack)
3739

3840
- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)
3941

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Queue = require('../index');
2+
3+
function generateBinaryNumber(n) {
4+
const result = [];
5+
const q = new Queue();
6+
7+
// add `1` to the queue
8+
q.enqueue('1');
9+
10+
// iterate till the given number
11+
for (let i = 0; i < n; i += 1) {
12+
// push the item in the queue to the array
13+
result.push(q.dequeue());
14+
15+
// append `0` & `1` respectively
16+
const s1 = `${result[i]}0`;
17+
const s2 = `${result[i]}1`;
18+
19+
// push the combinations in the queue
20+
q.enqueue(s1);
21+
q.enqueue(s2);
22+
}
23+
// return the result containing all the binary numbers
24+
return result;
25+
}
26+
27+
// console.log(generateBinaryNumber(5));
28+
29+
module.exports = generateBinaryNumber;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Stack = require('../../Stack');
2+
3+
class Queue {
4+
constructor() {
5+
this.queue = new Stack();
6+
this.temp = new Stack();
7+
}
8+
9+
enqueue(data) {
10+
this.queue.push(data);
11+
}
12+
13+
dequeue() {
14+
if (!this.queue.peek()) {
15+
return null;
16+
}
17+
18+
// pop all the element to the temp stack
19+
while (this.queue.peek()) this.temp.push(this.queue.pop());
20+
const el = this.temp.pop();
21+
22+
// push all the temp items to the queue again
23+
while (this.temp.peek()) this.queue.push(this.temp.pop());
24+
return el;
25+
}
26+
}
27+
28+
module.exports = Queue;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const Queue = require('../index');
3+
const Stack = require('../../Stack');
4+
5+
function reverseFirstKElelments(q, k) {
6+
const s = new Stack();
7+
8+
// push all the k elements ot the stack
9+
for (let i = 0; i < k; i += 1) {
10+
s.push(q.dequeue());
11+
}
12+
13+
// push the stack items to the queue
14+
for (let i = 0; i < k; i += 1) {
15+
q.enqueue(s.pop());
16+
}
17+
18+
// empty the queue and push the same queue
19+
const remaining = q.length() - k;
20+
for (let i = 0; i < remaining; i += 1) {
21+
q.enqueue(q.dequeue());
22+
}
23+
24+
// return the queue
25+
return q;
26+
}
27+
28+
module.exports = reverseFirstKElelments;
29+
30+
// let q = new Queue();
31+
32+
// q.enqueue(1);
33+
// q.enqueue(2);
34+
// q.enqueue(3);
35+
// q.enqueue(4);
36+
// q.enqueue(5);
37+
// q.enqueue(6);
38+
// q.enqueue(7);
39+
// q.enqueue(8);
40+
// q.enqueue(9);
41+
42+
// q = reverseFirstKElelments(q, 4);
43+
44+
// const arr = [];
45+
// while (q.length()) {
46+
// arr.push(q.dequeue());
47+
// }
48+
// console.log(arr);

src/_DataStructures_/Stack/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ class Stack {
1515
peek() {
1616
return this.data[this.data.length - 1];
1717
}
18-
isEmpty(){ //check if stack is empty
19-
return this.data.length==0;
18+
19+
isEmpty() {
20+
// check if stack is empty
21+
return this.data.length === 0;
2022
}
2123
}
2224

0 commit comments

Comments
 (0)