This is a verified interview question from Lseg---london-stock-exchange. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Corporate VWAP (Bugfix) - LSEG London Stock Exchange Online Assessment RNS Institute of Technology" covers key patterns like Arrays.
"Task 3 ## What the stub computes `solution(trades)` should compute the volume-weighted average price (VWAP) per instrument, using **only** trades with **positive** quantity: ``` VWAP = sum(price_i * quantity_i) / sum(quantity_i) ``` - Input: list of trades `(instrument, price, quantity)`. `price` is never negative. `quantity` may be positive, negative, zero, or fractional. - Output: a map from instrument to VWAP. - **An instrument with no qualifying trades must be omitted entirely** — no zero, `null`, or placeholder entry. --- ## The buggy code (as given in the stub) ```cpp #include <vector> #include <string> #include <tuple> #include <map> using namespace std; // Trade = {instrument, price, quantity} using Trade = tuple<string, double, double>; // Result: instrument -> volume-weighted average price, using only trades // with positive quantity (see README) using Result = map<string, double>; Result solution(vector<Trade>& trades) { map<string, double> totals; map<string, double> quantities; for (auto& [instrument, price, quantity] : trades) { if (quantity < 0) continue; // <-- BUG totals[instrument] += price * quantity; quantities[instrument] += quantity; } Result result; for (auto& [instrument, total] : totals) { result[instrument] = total / quantities[instrument]; } return result; } ``` ## The bug The filter is `if (quantity < 0) continue;`, which only skips **negative** quantities. The spec says to use only trades with **positive** quantity — i.e. strictly `> 0`. Since `quantity` can also be exactly `0`, the buggy filter lets zero-quantity trades slip through. A zero-quantity trade contributes `price * 0 = 0` to `totals[instrument]` and `0` to `quantities[instrument]`. That alone looks harmless — but if an instrument has **no** trades with positive quantity (only zero and/or negative ones), the buggy code still creates an entry for it in `totals` (via the `+=` on a zero-quantity trade), which means: - The instrument now shows up in the final `result` loop (which iterates over `totals`), **violating "must be omitted entirely."** - Its VWAP is computed as `0 / 0` → `NaN`, which gets returned to the caller as the instrument's price. ### Reproduction ```cpp vector<Trade> trades = { {"AAPL", 100.0, 0.0}, // only a zero-quantity trade {"MSFT", 50.0, 10.0}, {"MSFT", 60.0, -5.0} // negative, correctly excluded either way }; ``` **Buggy output:** ``` Result size: 2 AAPL -> -nan MSFT -> 50 ``` `AAPL` should not appear at all — it has no trade with strictly positive quantity. ---"
Join thousands of developers practicing for Lseg---london-stock-exchange.