-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcp-016-website-data-structure.linq
64 lines (49 loc) · 1.1 KB
/
dcp-016-website-data-structure.linq
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
<Query Kind="Program" />
// 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.
void Main()
{
var buffer = new Buffer<int>(3);
buffer.Add(1);
buffer.Add(2);
buffer.Add(3);
buffer.Add(4);
buffer.GetLast(1).Dump();
buffer.GetLast(2).Dump();
buffer.GetLast(3).Dump();
buffer.Add(5);
"***".Dump();
buffer.GetLast(1).Dump();
buffer.GetLast(2).Dump();
buffer.GetLast(3).Dump();
}
public class CircularBuffer<T>
{
private T[] _data;
private int _idx;
private int _n;
public CircularBuffer(int capacity)
{
_n = capacity;
_data = new T[_n];
}
public void Add(T item)
{
_data[_idx] = item;
_idx += 1;
if (_idx >= _n)
_idx = 0;
}
public T GetLast(int i)
{
int resultIdx = _idx - i;
while (resultIdx < 0)
resultIdx += _n;
return _data[resultIdx];
}
}