-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ16.js
More file actions
123 lines (105 loc) · 3.05 KB
/
Q16.js
File metadata and controls
123 lines (105 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
This problem was asked by Twitter.
You run an e-commerce website and want to record the last N order ids in a log. Implement a data structure to accomplish this, with the following API:
record(order_id): adds the order_id to the log
get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
You should be as efficient with time and space as possible.
*/
// Testing: `node ./Q16.js`
// Implementation with an array
class LimitedArray {
/* In JS, Sets are not as performant dominant over arrays as in Java. According to this article
(https://stackoverflow.com/a/39010462), arrays are better at insertion and Set's deletion is
much better than the slice method. */
arr;
n;
constructor (n) {
this.arr = [];
this.n = n;
}
record(order_id) {
this.arr.push(order_id);
if (this.arr.length > this.n) {
this.arr.shift();
}
}
get_last(i) {
i = Math.min(i, this.n);
// returns reversed array of the limited size
const limitedArr = [];
const lowestIndex = Math.max(0, this.arr.length - i);
for (let num = this.arr.length - 1; num >= lowestIndex; num--) {
limitedArr.push(this.arr[num]);
}
return limitedArr;
}
}
function test(DataStructure) {
const l = new DataStructure(10);
l.record(5);
l.record(6);
l.record(7);
l.record(8);
l.record(9);
l.record(10);
l.record(11);
l.record(12);
l.record(13);
l.record(14);
l.record(15);
l.record(16);
console.log('get_last(5)', l.get_last(5));
console.log('get_last(8)', l.get_last(8));
console.log('get_last(18)', l.get_last(18));
}
console.log('--- LimitedArray ---');
test(LimitedArray);
// Implementation with a double linked link structure
class Node {
val;
previousNode = null;
nextNode = null
constructor (val, prevNode, nextNode) {
this.val = val;
this.previousNode = prevNode;
this.nextNode = nextNode;
}
}
class LimitedDoubleLinkedList {
lastNode = null;
firstNode = null;
length = 0;
n;
constructor (n) {
this.n = n;
}
record (val) {
const newNode = new Node(val, this.firstNode);
if (this.firstNode) { // links previous first node with new one
this.firstNode.nextNode = newNode;
}
this.firstNode = newNode;
if (!this.lastNode) {
this.lastNode = newNode;
}
if (this.length === this.n) { // reasigns last node if at n
this.lastNode = this.lastNode.nextNode;
} else {
this.length++;
}
}
get_last(i) {
i = Math.min(i, this.n);
// traverses linked list and adds values to returning array
const arr = [];
let node = this.firstNode;
while (i && node) {
arr.push(node.val);
i--;
node = node.previousNode;
}
return arr;
}
}
console.log('--- LimitedDoubleLinkedList ---');
test(LimitedDoubleLinkedList);