Questions / Quant development, Numerics & testing

Test an invariant, not just an example

Applied, about 6 minutes. Question D039.

A rolling aggregate supports add(trade) and remove(trade). Starting from an empty state, which property directly probes their consistency?

  1. ARemove from an empty window; verify that the operation rejects it.
  2. BAdd the trade twice; verify that its displayed average is unchanged.
  3. CAdd a trade and remove it; verify both totals return to zero.
  4. DAdd a trade and check that the count rose by one.

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.

  1. Adding a trade contributes price × quantity to notional and quantity to volume.
  2. Removing that same trade should subtract both contributions.
  3. 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.

More numerics & testing questions