Practice Free
HardBusiness Analytics High Frequency

Cross-Exchange Market Arbitrage Spread

Frequently Asked By:StripeCoinbaseBloomberg
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

Join `exchange_orderbooks` for `exchange_a` and `exchange_b` on `pair_symbol`. Calculate: 1. `a.pair_symbol` 2. `a.best_bid` AS `exchange_a_bid` 3. `b.best_ask` AS `exchange_b_ask` 4. `(a.best_bid - b.best_ask)` AS `arbitrage_spread` 5. `spread_pct`: `ROUND(100.0 * (a.best_bid - b.best_ask) / b.best_ask, 2)` Filter for executable opportunities where `a.best_bid > b.best_ask`. Order results by `spread_pct` descending.

Why Technical Interviewers Ask This

Quantitative market makers search for arbitrage discrepancies across crypto exchanges. An arbitrage opportunity exists when Exchange A's highest buy bid is strictly greater than Exchange B's lowest sell ask for the same currency pair.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass business analytics constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        a.pair_symbol,
        a.best_bid AS exchange_a_bid,
        b.best_ask AS exchange_b_ask,
        (a.best_bid - b.best_ask) AS arbitrage_spread,
        ROUND(100.0 * (a.best_bid - b.best_ask) / b.best_ask, 2) AS spread_pct
      FROM exchange_orderbooks a
      JOIN exchange_orderbooks b ON a.pair_symbol = b.pair_symbol
      WHERE a.exchange_name = 'Exchange_A'
        AND b.exchange_name = 'Exchange_B'
        AND a.best_bid > b.best_ask
      ORDER BY spread_pct DESC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Business Analytics. It meets the target criteria: Executable arbitrage pairs where Exchange A bid exceeds Exchange B ask.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Stripe and Coinbase interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Business Analytics queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions