AlgoMaster Logo
AlgoMasterAssign Events to Session Windowsmedium

Assign Events to Session Windows

medium

A session window groups one user's activity until that user remains inactive for a configured gap. Different users maintain independent sessions even when their events are interleaved.

Design a SessionWindowAssigner class:

  • SessionWindowAssigner() creates a stateless assigner.
  • int[] assign(String[] users, int[] timestamps, int inactivityGap) returns the session number for every event.

Events are in nondecreasing timestamp order. Each user's first event belongs to session 1. A later event starts the next session for that user when its timestamp minus that user's immediately previous timestamp is greater than or equal to inactivityGap. Otherwise it remains in the current session. Return results in input order.

Example 1:
Example 2:

Constraints

  • 0 <= users.length <= 10^4
  • users.length == timestamps.length
  • 1 <= users[i].length <= 30
  • 0 <= timestamps[i] <= 10^9
  • Timestamps are nondecreasing.
  • 1 <= inactivityGap <= 10^9
  • At most 100 calls are made to assign.
Hints

Loading...
CallReturns
new SessionWindowAssigner()null
assign(["a","a","b","a","b"], [0,4,5,10,11], 6)[1,1,1,2,2]

Each user's six-second gap starts a new session. Events belonging to the other user do not affect that calculation.

Run checks these cases. Submit also runs a larger hidden set.