FastPrepActive Users in a Five-Minute Window

Active Users in a Five-Minute Window

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You receive a finite sequence of operations over timestamped user activity.

  • ["add", timestamp, userId] records one request from userId at the given integer timestamp.
  • ["getActiveUsers", currentTimestamp] asks for the number of distinct users with at least one recorded request in the inclusive interval [currentTimestamp - 300, currentTimestamp].

Event timestamps may arrive out of order. Query timestamps are nondecreasing, and every added event timestamp is at most the timestamp of the next query. Repeated requests from the same user count that user once while any of the user's requests remains in the active window.

Return one integer for each getActiveUsers operation, in encounter order.

Function

countActiveUsers(operations: String[][]) → int[]

Examples

Example 1

operations = [["add","100","alice"],["add","250","bob"],["getActiveUsers","400"],["add","100","alice"],["getActiveUsers","401"]]return = [2,1]

At time 400, both users have a request in the inclusive interval [100, 400]. At time 401, both requests at time 100 are expired, so only bob remains active.

Example 2

operations = [["add","0","u"],["add","300","v"],["getActiveUsers","300"],["getActiveUsers","301"]]return = [2,1]

The request at time 0 is included when the query time is 300, then expires when the query time becomes 301.

Constraints

  • 1 <= operations.length <= 100000.
  • Every operation is either add or getActiveUsers.
  • 0 <= timestamp, currentTimestamp <= 1000000000.
  • Query timestamps are nondecreasing.
  • Each added event timestamp is at most the timestamp of the next query.
  • userId contains between 1 and 40 visible ASCII characters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] countActiveUsers(String[][] operations) {
    // Write your code here.
}
operations[["add","100","alice"],["add","250","bob"],["getActiveUsers","400"],["add","100","alice"],["getActiveUsers","401"]]
expected[2,1]
Checking account…