Skip to content

Commit 8682900

Browse files
Merge pull request #4 from rvguradiya/add-ecommerce_order_log_data_structure
Add solution for recording and retrieving last N order IDs using a ci…
2 parents b893db3 + 819e8e2 commit 8682900

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This problem was asked by **Twitter**.
2+
3+
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:
4+
5+
- record(order_id): adds the order_id to the log
6+
- get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
7+
8+
You should be as efficient with time and space as possible.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Approach {
2+
int[] log;
3+
int capacity;
4+
int front;
5+
int rare;
6+
7+
Approach(int capacity) {
8+
this.capacity = capacity;
9+
this.log = new int[this.capacity];
10+
this.front = 0;
11+
this.rare = -1;
12+
}
13+
14+
public void record(int order_id) {
15+
if (this.log.length < this.capacity) {
16+
this.log[rare] = order_id;
17+
this.rare++;
18+
} else {
19+
this.rare = (this.rare + 1) % this.capacity;
20+
this.front = (this.front + 1) % this.capacity;
21+
this.log[this.rare] = order_id;
22+
}
23+
}
24+
25+
public int get_last(int index) {
26+
return this.log[(this.front + index - 1) % this.capacity];
27+
}
28+
29+
public static void main(String[] args) {
30+
Approach r = new Approach(5);
31+
r.record(102);
32+
r.record(103);
33+
r.record(104);
34+
r.record(105);
35+
r.record(106);
36+
r.record(107);
37+
38+
System.out.println(r.get_last(3));
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Record {
2+
constructor(N) {
3+
this.capacity = N;
4+
this.log = [];
5+
this.front = 0;
6+
this.rare = 0;
7+
}
8+
9+
record(order_id) {
10+
if (this.log.length < this.capacity) {
11+
this.log.push(order_id);
12+
this.rare++;
13+
} else {
14+
this.rare = (this.rare + 1) % this.capacity;
15+
this.front = (this.front + 1) % this.capacity;
16+
this.log[this.rare] = order_id;
17+
}
18+
}
19+
20+
get_last(index) {
21+
return this.log[(this.front + index - 1) % this.capacity];
22+
}
23+
}
24+
25+
r = new Record(5);
26+
r.record(102);
27+
r.record(103);
28+
r.record(104);
29+
r.record(105);
30+
r.record(106);
31+
r.record(107);
32+
33+
console.log(r.get_last(3));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Record:
2+
def __init__(self,N) -> None:
3+
self.log = []
4+
self.capacity = N
5+
6+
def record(self,order_id):
7+
if len(self.log) < self.capacity:
8+
self.log.append(order_id)
9+
else:
10+
self.log.pop(0)
11+
self.log.append(order_id)
12+
13+
def get_last(self,index):
14+
return self.log[-index]
15+
16+
r = Record(5)
17+
r.record(102)
18+
r.record(103)
19+
r.record(104)
20+
r.record(105)
21+
r.record(106)
22+
r.record(107)
23+
24+
print(r.get_last(3))

0 commit comments

Comments
 (0)