Drill

ProblemsC# › inventory

Group stock by how soon it expires

mediuminventoryHash mapsArraysC#

A food depot dashboard puts every batch into one of four buckets so staff can see what to move first.

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.

Solve it in Python →

Where you start

public Dictionary<string, int> ExpiryBuckets(List<int> daysLeft) {
    
}

Worked examples

CallResult
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;
}

The same problem in another language

More inventory problems in C#