Token Expiry with Lazy Deletion
Updated
Problem statement
A system hands out tokens that each stay valid for timeToLive time units. A token generated or renewed at time t expires at time t + timeToLive. A token is valid at time x only if its expiry time is greater than x, so a token that expires at time x is no longer valid at x.
Process operations in order. There are no tokens at the start. Each operation is one of:
[0, tokenId, currentTime]: generate. Create a new tokentokenIdatcurrentTime. Nothing is recorded.[1, tokenId, currentTime]: renew. If tokentokenIdexists and is valid atcurrentTime, its expiry becomescurrentTime + timeToLive. If the token was never generated or has already expired, nothing happens. Nothing is recorded.[2, currentTime]: count. Record the number of tokens that are valid atcurrentTime.
Return the recorded counts in operation order.
Expired tokens never become valid again, so you can delete them lazily, only when a count reaches them. Each operation should take amortized O(1) time.
Function
countValidTokens(timeToLive: int, operations: int[][]) → int[]Examples
Example 1
timeToLive = 10operations = [[0,7,1],[0,3,4],[2,5],[1,7,9],[2,12],[2,14],[1,3,16],[2,18],[2,19]]return = [2,2,1,1,0]Token 7 expires at 11 and token 3 at 14, so the count at time 5 is 2. Renewing token 7 at time 9 moves its expiry to 19, so both tokens are still valid at time 12. Token 3 expires at time 14, so the count at 14 is 1, and the renew of token 3 at time 16 does nothing. Token 7 is still valid at 18 and expires at 19.
Example 2
timeToLive = 3operations = [[1,5,1],[2,2],[0,5,3],[1,5,5],[1,5,7],[2,9],[2,10],[0,6,11],[2,12]]return = [0,1,0,1]The renew at time 1 does nothing because token 5 does not exist yet, so the count at time 2 is 0. Token 5 is generated at time 3 and renewed at 5 and 7, which moves its expiry to 8 and then 10. It is valid at time 9, so that count is 1. At time 10 token 5 has expired, so the count is 0. Token 6 is generated at time 11, so the count at 12 is 1.
Example 3
timeToLive = 100000000operations = [[0,0,1],[0,100000,2],[2,100000000]]return = [2]The tokens expire at 100000001 and 100000002, so both are still valid at time 100000000.
Constraints
1 <= timeToLive <= 10^81 <= operations.length <= 2 * 10^4- Each operation is
[0, tokenId, currentTime],[1, tokenId, currentTime]or[2, currentTime]. 0 <= tokenId <= 10^51 <= currentTime <= 10^8- The values of
currentTimestrictly increase from one operation to the next. - Each generate uses a
tokenIdthat was not generated before.
More Citadel Online Assessment questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Possible MEX Values After One Optional IncrementOA · Seen Oct 2026
- Busy IntersectionOA · Seen Sep 2026
- Shortest Path in an Unweighted GraphOA · Seen Sep 2026
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Image Processing CostOA · Seen Jul 2026
- Coprime Pick with Change CostOA · Seen Jul 2026
- Process SchedulingOA · Seen Mar 2026
- Employee With the Most OverlapsOA · Seen Feb 2026
Similar questions at other companies
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA
- Per-Domain Request LimiterExpedia · OAExpedia interview questions
- Truck Position SubscriberOptiver · OAOptiver interview questions
- Rate Limiter with a Sliding Window per ClientStackAdapt · PHONE SCREENStackAdapt interview questions
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Sliding Window Rate LimiterLotusFlare · ONSITE INTERVIEW
- Per-Client Sliding-Window Rate LimiterCVS Health · PHONE SCREEN
- Sliding-Window Rate LimiterAmazon · ONSITE INTERVIEW
- Sliding-Window Rate LimiterUber · PHONE SCREENUber interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences