KatInterview

Simulate Trading Order Fill Rate Limit

Greedy & IntervalsAuto-graded · 17 test cases
HardAuto-gradedGreedy & Intervalssliding-windowqueuegreedy

Last updated

Simulate order executions under a time-windowed volume participation constraint using a sliding window.

You are tasked with evaluating an algorithmic trading execution algorithm. Given a historical stream of public trade records, you need to simulate how much of a client's buy order would be executed and calculate the total cost paid.

Inputs

  1. market_prints: A chronological list of tuples (timestamp, price, volume) representing historical market transactions. timestamp is an integer millisecond timestamp, price is a float, and volume is a float.
  2. order: A tuple (target_volume, max_participation_rate) representing the client's order parameters. target_volume is the total volume to fill (float), and max_participation_rate is a float in [0.0, 1.0].
  3. window_size: An integer duration in milliseconds defining the historical look-back window.

Simulation Rules

  • Execution Timing & Batching: Process market records sequentially by timestamp. If multiple prints share the same timestamp T, process them as a single decision event at time T.
  • Execution Price: The trade price applied to any fill at timestamp T is the price of the last print occurring at timestamp T.
  • Participation Limit: At timestamp T, the maximum volume your algorithm is allowed to execute is bounded by max_participation_rate * V, where V is the aggregate market volume of prints that occurred strictly within the half-open time interval [T - window_size, T).
  • Window Membership: Volume from prints occurring at timestamp T does not count towards V at timestamp T. Instead, it becomes visible for future timestamps after the decision at T is finalized.
  • Order Limit: The total accumulated fill volume across all events cannot exceed target_volume. The fill amount at timestamp T is min(remaining_volume, max_participation_rate * V).
  • Early Exit: Once the order is completely filled (remaining_volume == 0), processing terminates immediately.

Return a tuple (total_executed_volume, total_cost) as floats.

Constraints

  • market_prints is sorted in non-decreasing order by timestamp.
  • Timestamps are non-negative integers.
  • window_size is a non-negative integer.
  • target_volume >= 0.0 and 0.0 <= max_participation_rate <= 1.0.
  • Prices and volumes are non-negative numbers.

Examples

Example 1

Input: process_trades([[100, 10, 50], [150, 10.1, 100], [250, 10.2, 50]], [100, 0.1], 200)

Output: [20, 203.5]

Hints

Hint 1

Group all prints with identical timestamps together so that you use the final print's price for execution at that timestamp.

Hint 2

Maintain a sliding window of historical prints using a queue to efficiently track total volume in the interval [T - window_size, T).

Hint 3

Be careful with timing: pop expired prints from the queue before calculating execution volume, and append the current timestamp's prints to the queue only after executing.