-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProblem16.java
55 lines (45 loc) · 1.34 KB
/
Problem16.java
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
/***
* 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.
*/
import java.util.Random;
public class Problem16 {
public static void main(String[] args) {
Logs logs = new LogArray(10);
addRandomIds(logs);
System.out.println(logs.getLast(3));
}
private static void addRandomIds(Logs logs){
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int randomId = rand.nextInt();
logs.record(randomId);
}
}
}
interface Logs {
void record(int orderId);
int getLast(int index);
}
class LogArray implements Logs {
private int[] records;
private int index;
public LogArray(int numOfRecords){
this.records = new int[numOfRecords];
this.index = 0;
}
@Override
public void record(int orderId) {
records[index++] = orderId;
if (index >= records.length) index = 0;
}
@Override
public int getLast(int i) {
return records[index - i];
}
}