Group stock by how soon it expires
A food depot dashboard puts every batch into one of four buckets so staff can see what to move first.
- Below zero days is "expired"; 0 to 7 is "week"; 8 to 30 is "month"; anything more is "later".
- All four keys appear in the result, even when the count is zero.
ExpiryBuckets(daysLeft: list<int>) → map<string, int>
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public Dictionary<string, int> ExpiryBuckets(List<int> daysLeft) {
}
Worked examples
| Call | Result |
|---|---|
ExpiryBuckets(new List<int> { -1, 0, 7, 8, 30, 31, 400 }) | new Dictionary<string, int> { { "expired", 1 }, { "week", 2 }, { "month", 2 }, { "later", 2 } } |
ExpiryBuckets(new List<int> { }) | new Dictionary<string, int> { { "expired", 0 }, { "week", 0 }, { "month", 0 }, { "later", 0 } } |
ExpiryBuckets(new List<int> { -5, -5, -5 }) | new Dictionary<string, int> { { "expired", 3 }, { "week", 0 }, { "month", 0 }, { "later", 0 } } |
ExpiryBuckets(new List<int> { 0 }) | new Dictionary<string, int> { { "expired", 0 }, { "week", 1 }, { "month", 0 }, { "later", 0 } } |
Hint
Seed the map with all four keys at zero first, then walk the list once.
Reference solution in C#
public Dictionary<string, int> ExpiryBuckets(List<int> daysLeft) {
var result = new Dictionary<string, int> { { "expired", 0 }, { "week", 0 }, { "month", 0 }, { "later", 0 } };
foreach (var d in daysLeft) {
string k = d < 0 ? "expired" : d <= 7 ? "week" : d <= 30 ? "month" : "later";
result[k]++;
}
return result;
}