-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
38 lines (31 loc) · 891 Bytes
/
script.js
File metadata and controls
38 lines (31 loc) · 891 Bytes
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
/* Problem 1
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.
You should be as efficient with time and space as possible.
*/
/* Defining data structure with methods */
class Logger {
/* @param { number } size
*/
constructor(size){
this.size = size
this.log = [];
this.currI = 0;
}
/*
* @param {number} order_id
*/
record(order_id){
this.log[currI] = order_id
this.currI = (this.currI + 1) % this.size
}
/*
* @param {number} i
* @return {number}
*/
get_last(i){
return this.log[(this.currI - i + this.size) % this.size]
}
}