# Quant SWE native experiment

Download `latency_lab.cpp` and build with a C++20 compiler:

```sh
c++ -std=c++20 -O3 -pthread -Wall -Wextra latency_lab.cpp -o latency_lab
./latency_lab
```

Before running, predict why scanning contiguous nodes and following a shuffled chain have the same O(n) complexity but different costs. The dataset contains 1,048,576 nodes; both paths verify the same checksum. Setup is outside the timed region, both paths are warmed, and their order alternates across 21 samples. Output quantiles describe **whole batches**, not individual accesses or request p99. Do not extrapolate these results to exchange latency.

Record compiler/version, flags, CPU, power mode, background load and output. Re-run on your target environment, change the dataset size, inspect generated assembly and use hardware counters where available. Explain whether vectorization, dependency chains, caches, memory bandwidth or scheduling could account for the difference. Twenty-one batches cannot characterize rare tails.

The bounded SPSC queue has exactly one producer and one consumer. An acquire load observes the other thread's release publication, both for reading a new slot and safely reusing a consumed slot. The two indices wrap modulo a power-of-two capacity; one slot is reserved. Padding assumes a 64-byte cache line for the exercise. The stress check validates one million FIFO values through repeated wrap-around. It is evidence from one execution, not a proof of race freedom or lock freedom on every platform.

## Interview follow-ups

1. Replace the release/acquire operations with relaxed operations **on paper**. Identify the missing happens-before edge and the resulting data race; a test that happens to pass does not make the change valid.
2. Explain why adding a second producer invalidates this design. Outline reservation and publication separately before attempting MPSC.
3. Add explicit shutdown and a full-queue policy. Compare spinning, yielding and blocking under load. Keep correctness tests separate from timing runs.
4. Add per-message timestamps and an open-loop arrival schedule. Account for queueing, dropped messages, clock overhead, warm-up and coordinated omission before reporting percentiles.

Background: [ISO C++ working draft, atomics and memory model](https://isocpp.org/files/papers/n4713.pdf). This is an independent teaching exercise, not a production trading component.
