KatInterview

Count Members With Continuous Subscription at Final Timestamp

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashingsimulationhash-mapstate-machine

Last updated

Tests per-entity state simulation where a subscription can only be renewed while still active, and any lapsed subscription is permanently void.

A streaming platform manages time-limited subscriptions for its members. Member i has a fixed subscription window of durations[i] time units.

The platform processes a sequence of renewal records. Each record [member_id, t] means that at time t, the platform attempts to set a subscription for member_id, which would cover the window [t, t + durations[member_id]) — that is, active through but not including time t + durations[member_id].

Subscriptions must be continuous. A renewal attempt can only take effect while the member's subscription is still running. The moment a member's subscription runs out, it expires permanently, and every later record for that member is discarded with no effect.

After processing all records, return the number of members who hold an active subscription at t_final, defined as the largest t appearing in records.

Rules

  • A member's subscription is active at time x when x < expiry, where expiry is their current expiration timestamp. The boundary is strict.
  • The first record for a member at time t sets expiry = t + durations[member_id].
  • A subsequent record for the same member at time t:
    • if t < expiry (subscription is still running), updates expiry = t + durations[member_id];
    • if t >= expiry (subscription has already lapsed), the member is permanently disqualified — this record and every later one for them are ignored.
  • Records are given in non-decreasing order of t and must be processed in that order.
  • A member counts toward the answer only if they have at least one record, have never lapsed, and their final expiry > t_final.
  • Members with no records at all are never counted.

Constraints

  • 1 <= len(durations) <= 100 000
  • 1 <= durations[i] <= 10^9
  • 1 <= len(records) <= 100 000
  • Each record is [member_id, t] with 0 <= member_id < len(durations) and 0 <= t <= 10^9
  • Records are sorted so that records[j][1] <= records[j+1][1] for every valid j
  • A member's expiry can reach up to 2 * 10^9, which exceeds the 32-bit signed integer limit — use 64-bit integers in typed languages

Examples

Example 1

Input: count_active_subscriptions([5, 3, 10], [[0, 1], [1, 2], [0, 4], [1, 6], [2, 7], [0, 8]])

Output: 2

Example 2

Input: count_active_subscriptions([3], [[0, 0], [0, 3], [0, 4]])

Output: 0

Example 3

Input: count_active_subscriptions([2, 5], [[0, 0], [1, 0], [1, 2]])

Output: 1

Hints

Hint 1

Track two things per member: their current expiry timestamp and whether they have already lapsed. Once a member lapses, no future record can change that — a simple set works well for this.

Hint 2

The renewal check is a strict inequality: a record arriving exactly when the subscription ends counts as too late. Trace through an example where t equals the current expiry to make sure your condition handles the boundary correctly.

Hint 3

At the end, a member can be uncounted for two independent reasons: they lapsed (a renewal came too late), or their subscription simply ran out before t_final with no late renewal. Keep these cases separate — do not let a late record overwrite expiry and accidentally revive a lapsed member.