Need help with credit expiration algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A credit expiration system sounds simple until partial usage, different issue dates, and audit requirements enter the picture. The safest approach is to model credits as dated lots, consume them deterministically, and run expiration as a repeatable calculation rather than an ad hoc balance adjustment.
Model Credits as Individual Lots
Instead of storing only a single account balance, store each credit grant as its own record with:
- amount issued
- amount remaining
- issue date
- expiration date
- source or reason
That structure lets you answer the hard questions later: which credits were spent, which ones expired, and why the balance changed on a specific date.
Here is a small Python model:
With that representation, the algorithm becomes much easier to reason about.
Spend the Oldest Eligible Credits First
Most systems use a first-expiring, first-used rule. That prevents newer credits from being consumed while older credits quietly expire in the background.
This rule is deterministic. If the same lots and the same transaction date go in, the same answer comes out every time. That is exactly what you want for support investigations and accounting reconciliation.
Run Expiration as a Separate Step
Expiration should be its own calculation, usually at a daily cutoff or during balance reads. Do not mix it implicitly into unrelated write operations.
Now you can process an account in a clear order:
- expire old lots
- apply new grants
- apply spending events
- compute the visible balance
That ordering prevents hidden state changes and makes replay testing much easier.
Example End-to-End Flow
That example is simple, but it scales because every balance change is grounded in a specific lot and date.
Design Decisions You Should Make Early
Before writing production code, define the business rules precisely:
- Does a credit expire at the start of a day or the end of a day?
- Are time zones based on the user, the business, or UTC?
- Can expired credits be restored?
- Can some credit types expire while others do not?
- Does spending use earliest-expiring first, oldest-issued first, or something else?
If these rules are vague, no algorithm will stay correct for long.
Common Pitfalls
The biggest mistake is storing only a single integer balance. That loses the information needed to expire credits correctly and explain historic changes.
Another common problem is using the current clock directly inside business logic. Expiration code should accept an explicit as_of date or timestamp so tests are deterministic.
Teams also forget concurrency. If credits can be spent from multiple requests at once, the underlying database transaction must prevent the same remaining credit from being consumed twice.
Finally, be precise about date boundaries. A credit that expires on 2025-02-10 means nothing until you define whether it is valid throughout that day or only until midnight.
Summary
- Store credits as dated lots, not just a single balance.
- Spend credits in a deterministic order, usually earliest-expiring first.
- Run expiration as an explicit step with a clear
as_oftime. - Make policy rules around time zones and day boundaries explicit.
- Design for auditability and concurrency from the start.
Related reading
- Need to devise a number crunching algorithm
- Negative weights using Dijkstra's Algorithm
- Nesting maximum amount of shapes on a surface
- .NET library for text algorithms?
- Neural Network Back-Propagation Algorithm Gets Stuck on XOR Training PAttern
- Neural Network Back-Propagation Algorithm Gets Stuck on XOR Training PAttern
- Neural Network to predict nth square
- New cryptographic algorithms?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.