Which parts need attention first
The morning report lists every part sitting below its minimum, worst first, so the buyer knows where to start.
- A part is short when what is on hand is strictly below its minimum.
- Order by shortfall, largest first.
- Parts equally short are listed by code, A to Z.
lowStock(parts: list<Sku>) → list<string>
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
std::vector<std::string> lowStock(std::vector<Sku> parts) {
}
Worked examples
| Call | Result |
|---|---|
lowStock(std::vector<Sku>{Sku{std::string("B-2"), 4, 10}, Sku{std::string("A-1"), 20, 10}, Sku{std::string("C-3"), 0, 30}}) | std::vector<std::string>{std::string("C-3"), std::string("B-2")} |
lowStock(std::vector<Sku>{Sku{std::string("Z-9"), 5, 10}, Sku{std::string("A-4"), 1, 6}}) | std::vector<std::string>{std::string("A-4"), std::string("Z-9")} |
lowStock(std::vector<Sku>{Sku{std::string("A-1"), 10, 10}}) | std::vector<std::string>{} |
lowStock(std::vector<Sku>{}) | std::vector<std::string>{} |
Hint
Filter, then sort with a comparator that falls back to the code when the shortfalls match.
Reference solution in C++
std::vector<std::string> lowStock(std::vector<Sku> parts) {
std::vector<Sku> short_;
for (const auto& p : parts) if (p.onHand < p.minLevel) short_.push_back(p);
std::sort(short_.begin(), short_.end(), [](const Sku& a, const Sku& b) {
int da = a.minLevel - a.onHand, db = b.minLevel - b.onHand;
if (da != db) return da > db;
return a.code < b.code;
});
std::vector<string> out;
for (const auto& p : short_) out.push_back(p.code);
return out;
}