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>
Java 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
List<String> lowStock(List<Sku> parts) {
}
Worked examples
| Call | Result |
|---|---|
lowStock(Main.<Sku>ls(new Sku("B-2", 4, 10), new Sku("A-1", 20, 10), new Sku("C-3", 0, 30))) | Main.<String>ls("C-3", "B-2") |
lowStock(Main.<Sku>ls(new Sku("Z-9", 5, 10), new Sku("A-4", 1, 6))) | Main.<String>ls("A-4", "Z-9") |
lowStock(Main.<Sku>ls(new Sku("A-1", 10, 10))) | Main.<String>ls() |
lowStock(Main.<Sku>ls()) | Main.<String>ls() |
Hint
Filter, then sort with a comparator that falls back to the code when the shortfalls match.
Reference solution in Java
List<String> lowStock(List<Sku> parts) {
List<Sku> short_ = new ArrayList<>();
for (Sku p : parts) if (p.onHand < p.minLevel) short_.add(p);
short_.sort((a, b) -> {
int d = (b.minLevel - b.onHand) - (a.minLevel - a.onHand);
return d != 0 ? d : a.code.compareTo(b.code);
});
List<String> out = new ArrayList<>();
for (Sku p : short_) out.add(p.code);
return out;
}