What the shelf is worth
Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.
- Quantity and cost are both in whole units; the answer is in minor currency units.
- Negative quantities are counting errors — skip those rows entirely.
- An empty shelf is worth nothing.
stockValue(rows: list<Row>) → 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
int stockValue(std::vector<Row> rows) {
}
Worked examples
| Call | Result |
|---|---|
stockValue(std::vector<Row>{Row{3, 250}, Row{2, 100}}) | 950 |
stockValue(std::vector<Row>{Row{-4, 500}, Row{1, 500}}) | 500 |
stockValue(std::vector<Row>{Row{0, 900}}) | 0 |
stockValue(std::vector<Row>{}) | 0 |
Hint
One pass, one accumulator, one guard.
Reference solution in C++
int stockValue(std::vector<Row> rows) {
int total = 0;
for (const auto& r : rows) if (r.qty > 0) total += r.qty * r.unitCost;
return total;
}