-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathP16.js
34 lines (27 loc) · 928 Bytes
/
P16.js
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
/**
* 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.
*/
function AppLog(N) {
this.data = [];
this.current = 0;
this.size = N;
}
AppLog.prototype.record = function(order_id) {
this.data[this.current] = order_id;
this.current = (this.current + 1) % this.size;
}
AppLog.prototype.get_last = function(i) {
return this.data[(this.current - i + this.size) % this.size];
}
const log = new AppLog(5);
for(let i = 0; i < 9; i++) {
log.record(`312${i}`);
console.log(log);
}
for(let i=1; i<=5; i++) {
console.log(log.get_last(i));
}