Drill

ProblemsC# › inventory

Replay a day of stock movements

mediuminventoryArraysSimulationC#

The warehouse log holds a day of movements for one part: positive for receipts, negative for picks. Replay them to get the closing figure.

StockAfterMoves(opening: int, moves: list<int>) → 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 int StockAfterMoves(int opening, List<int> moves) {
    
}

Worked examples

CallResult
StockAfterMoves(10, new List<int> { -3, 5, -4 })8
StockAfterMoves(10, new List<int> { -3, 5, -20 })0
StockAfterMoves(5, new List<int> { -10, 3 })3
StockAfterMoves(0, new List<int> { })0

Hint

Apply one movement at a time and clamp at zero after each.

Reference solution in C#
public int StockAfterMoves(int opening, List<int> moves) {
    int level = Math.Max(0, opening);
    foreach (var m in moves) level = Math.Max(0, level + m);
    return level;
}

The same problem in another language

More inventory problems in C#