Questions / Quant development, Numerics & testing
Test an invariant, not just an example
A rolling aggregate supports add(trade) and remove(trade). Starting from an empty state, which property directly probes their consistency?
Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.
Show the answer and worked solution
Answer: C. Add a trade and remove it; verify both totals return to zero.
- Adding a trade contributes price × quantity to notional and quantity to volume.
- Removing that same trade should subtract both contributions.
- Starting empty makes the expected final state unambiguous: both totals return to zero, with the documented empty-window result.
Interview shortcut. Add then remove should restore the empty state.
Takeaway. Derive round-trip tests from the state equations.
Why the other choices fail
- Remove from an empty window; verify that the operation rejects it. Empty-window rejection is a useful boundary check, but it does not test that add and remove cancel for a valid trade.
- Add the trade twice; verify that its displayed average is unchanged. The unchanged average after duplicating a trade can hold even if removal updates the numerator or denominator incorrectly.
- Add a trade and check that the count rose by one. A one-sided operation does not test their consistency.
In the app this has a follow-up where one thing about the setup changes. Try the follow-up.