FastPrepPer-Element Hit Counter
Problem · Hash Table

Per-Element Hit Counter

Learn this problem
MediumUber logoUberFULLTIMEONSITE INTERVIEW
See Uber hiring insights

Problem statement

Process a chronological batch of hit-counter commands. Each command is one of:

  • HIT el timestamp records one hit of element el at timestamp.
  • GET el timestamp returns how many hits of el fall in the half-open window (timestamp - 300, timestamp].
  • TOTAL timestamp returns how many hits of any element fall in that same window.

Timestamps are positive and non-decreasing. A hit at time t is counted by a query at time q if and only if q - 300 < t <= q.

Return the results of the GET and TOTAL commands in the order they appear.

Function

processHitCounterCommands(commands: String[]) → int[]

Examples

Example 1

commands = ["HIT a 1","HIT b 2","HIT a 2","GET a 2","GET b 2","TOTAL 2"]return = [2,1,3]

At time 2, element a has hits at 1 and 2, element b has one hit, and the three hits together make the total 3.

Example 2

commands = ["HIT a 1","HIT a 2","HIT b 300","GET a 301","GET b 301","TOTAL 301"]return = [1,1,2]

The query window at time 301 is (1, 301], so the hit of a at time 1 has expired. The remaining hits are a at 2 and b at 300.

Constraints

  • 1 <= commands.length <= 10000.
  • Each command is exactly HIT el timestamp, GET el timestamp, or TOTAL timestamp.
  • Every el is a nonempty string of at most 16 lowercase English letters.
  • Timestamps are integers in [1, 10^9] and are non-decreasing across the batch.
  • At least one command is GET or TOTAL.

More Uber problems

drafts saved locally
public int[] processHitCounterCommands(String[] commands) {
  // Write your code here.
}
commands["HIT a 1","HIT b 2","HIT a 2","GET a 2","GET b 2","TOTAL 2"]
expected[2,1,3]
checking account