Uber | Onsite | metric collector

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2023-03-24Added By Anonymous
Source: Link

Company: Uber

Interview Stage: Onsite


Original Post Content:


Design a metric collector, which has procide two function:

add(string metricName) -> add 1 to the metric

getMetricCount(string metricName) -> get the count of the metric within 100s


e.g.

Time

1 add(Test)

99 add(Test)

100 getMetricCount(Test) = 2

101 getMetricCount(Test) = 1


My solution:

unordered_map<string, deque<clock_t>> metrics, which store the timestamp of a log

when call getMetricCount, remove the logs that expires, then count the length in deque.


I think my solution isn't effecient, because it takes linear time when call getMetricCount, but I don't have other idea.

Comments