Count Members With Continuous Subscription at Final Timestamp
Arrays & HashingAuto-graded · 22 test casesLast 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
xwhenx < expiry, whereexpiryis their current expiration timestamp. The boundary is strict. - The first record for a member at time
tsetsexpiry = t + durations[member_id]. - A subsequent record for the same member at time
t:- if
t < expiry(subscription is still running), updatesexpiry = 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.
- if
- Records are given in non-decreasing order of
tand 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 0001 <= durations[i] <= 10^91 <= len(records) <= 100 000- Each record is
[member_id, t]with0 <= member_id < len(durations)and0 <= t <= 10^9 - Records are sorted so that
records[j][1] <= records[j+1][1]for every validj - 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each member behaves as a small state machine with three states:
- Unseen — no records yet; never counted.
- Active — has a live expiry timestamp that can still be updated.
- Lapsed — a renewal arrived at or after their expiry; permanently disqualified.
Because records are already sorted by non-decreasing t, one left-to-right pass is enough. For each record [member_id, t]:
- If the member is already lapsed, skip immediately.
- If the member is unseen, record their first expiry:
expiry[member_id] = t + durations[member_id]. - If
t < expiry[member_id], the subscription is still running — renew it:expiry[member_id] = t + durations[member_id]. - Otherwise (
t >= expiry[member_id]), the renewal is too late — add the member tolapsed.
After the pass, count members where member_id not in lapsed and expiry[member_id] > t_final.
Why it is correct
- The strict inequality on renewal (
t < expiry) and the strict inequality on counting (expiry > t_final) match the problem rules exactly. Example 2 (expiry lands exactly att = 3, which is not< 3) and Example 3 (final expiry equalst_final = 2, which is not> 2) both verify these boundaries. - Renewal is a plain assignment, not a
max. Becausedurations[i] >= 1, a renewal never shortens access, so there is no need for a max. - The
lapsedset short-circuits every later record for a disqualified member. A too-late event cannot overwrite expiry and silently revive the member — a common bug if only expiry tracking is used without a separate lapsed flag. - Unseen members never enter
expiry, so they are never counted.
Two distinct reasons a member is not counted
| Reason | What happened |
|---|---|
| Lapsed | A renewal attempt arrived at or after expiry; member is disqualified. |
| Silently expired | No late renewal, but final expiry <= t_final. |
Keeping these separate in the data structures prevents one from masking the other.
Complexity
- Time: O(E + U), where E is the number of records and U is the number of distinct members seen. Each record costs O(1) for hash-map and set operations; the final count is one pass over
expiry. - Space: O(U) for the
expirydictionary and thelapsedset.
Python integers are arbitrary precision, so expiries up to 2 * 10^9 are handled safely. In Java or C++ you would use long / int64_t.
from typing import List
def count_active_subscriptions(durations: List[int], records: List[List[int]]) -> int:
expiry = {} # member_id -> current expiry timestamp
lapsed = set() # members permanently disqualified
t_final = records[-1][1] if records else 0
for member_id, t in records:
t_final = max(t_final, t) # defensive; records are already sorted
if member_id in lapsed:
continue # permanently disqualified; ignore
if member_id not in expiry:
expiry[member_id] = t + durations[member_id] # first record
elif t < expiry[member_id]:
expiry[member_id] = t + durations[member_id] # still active: renew
else:
lapsed.add(member_id) # arrived too late: lapse permanently
return sum(
1 for mid, e in expiry.items()
if mid not in lapsed and e > t_final
)