Skip to content

Commit 4b36bc2

Browse files
author
openset
committed
Add: Number of Recent Calls
1 parent 9a5c65d commit 4b36bc2

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
package number_of_recent_calls
2+
3+
import "container/list"
4+
5+
type RecentCounter struct {
6+
queue *list.List
7+
}
8+
9+
func Constructor() RecentCounter {
10+
return RecentCounter{list.New()}
11+
}
12+
13+
func (this *RecentCounter) Ping(t int) int {
14+
this.queue.PushBack(t)
15+
for this.queue.Front().Value.(int) < t-3000 {
16+
this.queue.Remove(this.queue.Front())
17+
}
18+
return this.queue.Len()
19+
}
20+
21+
/**
22+
* Your RecentCounter object will be instantiated and called as such:
23+
* obj := Constructor();
24+
* param_1 := obj.Ping(t);
25+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
package number_of_recent_calls
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input []int
10+
expected []int
11+
}
12+
13+
func TestConstructor(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: []int{1, 100, 3001, 3002},
17+
expected: []int{1, 2, 3, 3},
18+
},
19+
}
20+
for _, tc := range tests {
21+
obj, output := Constructor(), make([]int, 0)
22+
for _, t := range tc.input {
23+
output = append(output, obj.Ping(t))
24+
}
25+
if !reflect.DeepEqual(output, tc.expected) {
26+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)