FunctionRacerProblemsRace

Share Lots Sold Oldest First

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class Portfolio that tracks shares of one stock bought in lots and always sells from the oldest lot first. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

Portfolio()
buy(10, 5) returns 10
buy(10, 8) returns 20
sell(12, 9) returns 42
oldest_lot() returns [8,8]
get_cost_basis() returns 64
sell(3, 4) returns -12
get_total_profit() returns 30

The sale of 12 shares uses all 10 bought at 5 and 2 of those bought at 8, so they cost 66 and bring in 108. The second sale loses money: 12 comes in for shares that cost 24.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems