Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions problems/number-of-recent-calls/number_of_recent_calls.go
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
package number_of_recent_calls

import "container/list"

type RecentCounter struct {
queue *list.List
}

func Constructor() RecentCounter {
return RecentCounter{list.New()}
}

func (this *RecentCounter) Ping(t int) int {
this.queue.PushBack(t)
for this.queue.Front().Value.(int) < t-3000 {
this.queue.Remove(this.queue.Front())
}
return this.queue.Len()
}

/**
* Your RecentCounter object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Ping(t);
*/
28 changes: 28 additions & 0 deletions problems/number-of-recent-calls/number_of_recent_calls_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
package number_of_recent_calls

import (
"reflect"
"testing"
)

type caseType struct {
input []int
expected []int
}

func TestConstructor(t *testing.T) {
tests := [...]caseType{
{
input: []int{1, 100, 3001, 3002},
expected: []int{1, 2, 3, 3},
},
}
for _, tc := range tests {
obj, output := Constructor(), make([]int, 0)
for _, t := range tc.input {
output = append(output, obj.Ping(t))
}
if !reflect.DeepEqual(output, tc.expected) {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}