Credit Ledger That Spends Earliest Expiring Grants First
Last updated: November 29, 2026
Quick Overview
Design a class that tracks a user's song credits as grants with amounts and expiry times (monthly plan credits, top-up packs, promos), supporting grant, balance and an all-or-nothing spend that always draws from the grant expiring soonest.
Suno
November 29, 20260
0
0 solved
Design a class that tracks a user's song credits as grants with amounts and expiry times (monthly plan credits, top-up packs, promos), supporting grant, balance and an all-or-nothing spend that always draws from the grant expiring soonest.
Suno sells generation through credits on Free, Pro and Premier plans plus add-on packs, each with different expiry rules. The interviewer looks at data structure choice, atomic spend semantics, and whether you think about the money-like correctness concerns of a ledger.
What the Interviewer Expects
- Use a min-heap keyed on expiry
- Lazily drop expired grants at each operation
- Make spend all-or-nothing (check balance first)
- Partially drain grants and keep the remainder
- Deterministic tie-break between grants with equal expiry
- Discuss persistence and concurrency beyond the in-memory version
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you refund credits for a failed generation back to the right grant?
- How do you make spend safe with two concurrent requests in production?
- What if balance must be O(1)?
- How would you model this as an append-only ledger in Postgres?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
grant(amount, expires_at, now), balance(now), spend(amount, now) -> bool. Expired credit is unusable. Spending draws from the soonest-expiring grant so users lose as little as possible. A spend ...
High-Level Design
Store grants in a min-heap of [expires_at, seq, remaining]. seq breaks ties in insertion order and keeps comparisons off the mutable amount. Every public call first pops grants with `expires_at <=...