FastPrepPer-Element Hit Counter
Problems/Per-Element Hit Counter

Per-Element Hit Counter

Uber logoUber● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source asked a Design Hit Counter variant with put(element), getCount(element), and getTotalCount(). The judged core task matches the visible source at about 90%; the 300-second window, command batch, and numeric bounds are specified for practice.

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

  • Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
  • Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Max Points on a LinePHONE SCREEN · Seen Oct 2026
All Uber interview questionsSee Uber hiring insights
Saved
class Solution {
    public int[] processHitCounterCommands(String[] commands) {
      // Write your code here.
    }
}
Checking account…