Process exchange prints, volume checks, and client orders
Broker Program for Exchange Interaction
You are tasked with developing a broker program that processes a stream of commands from stdin and writes required outputs to stdout. The program must process commands strictly in order.
Timestamps are integers, representing seconds since market open, and are guaranteed to be non-decreasing across all input lines. Security names and client names are unquoted strings without spaces. All numeric values (quantity, price, goal shares, participation rate) are guaranteed to fit within 32-bit signed integers.
Command Types
1. Trade Print (Market Trade)
Input format:
print <timestamp> <security> <quantity> <price>
quantityandpriceare positive integers.- This command updates the most recent trade price for the specified security.
2. Volume Check (Query)
Input format:
volume-check <timestamp> <security>
For each volume-check command, the program must output one line containing the trailing-minute market volume for that security at the given timestamp.
- Trailing-minute market volume is defined as the sum of
quantityfrom all market trade prints (print ...lines) for that security with timestamps in the last 60 seconds, ending at the query time. The window is inclusive on the upper bound and exclusive on the lower bound:t-60 < trade_timestamp <= t.
**Output format (for volume-check):
<volume>
Part 2: Trading for a Client
In addition to the above, the input stream may also contain client orders:
order <timestamp> <security> <client> <goal_shares> <participation_rate>
goal_sharesis a positive integer, representing the total shares the client wishes to trade.participation_rateis an integer percentage (e.g.,20means 20%).
Execution Rules for Client Orders
When processing the input stream, your program may generate its own trades to fill active client orders, subject to the following rules:
- Trade Price: Every trade executed by your program for a security must use the previous trade price for that security. This is defined as the most recent price seen for that security from an exchange
printline. If no price has ever been printed for a security, your program cannot trade it. - Participation Constraint (per security): At any given timestamp
t, the client’s cumulative executed shares for that security in the trailing minute (t-60 < trade_timestamp <= t) must not exceedfloor( market_volume(t) * participation_rate / 100 ).market_volume(t)here refers only to the trailing-minute market volume derived from exchangeprintlines, explicitly excluding any trades generated by your program. - Trade ASAP: After reading each input line, and before moving to the next line, your program must execute and output all trades that are currently feasible for any active orders. This is subject to the remaining
goal_sharesand the participation constraints. - Order Completion: An order remains active until its
goal_sharesare fully executed.
**Output format (for generated trades):
For each trade your program generates, output a line:
print <timestamp> <security> <num_shares> <price>
- The
timestampfor generated trades must be the timestamp of the currently processed input line that enabled the trade.
Notes and Edge Cases
- The system must handle multiple distinct securities and multiple outstanding client orders concurrently.
- Timestamps can repeat, meaning multiple commands may occur at the same second.
- The "trade ASAP" semantics must be strictly respected: all feasible executions at time
tmust be processed before reading the next input line. - You must choose and document a deterministic policy for allocating trading capacity if multiple active orders compete for the same security at the same timestamp (e.g., FIFO by order arrival time).
AI interview prep
Core dilemma
Managing complex, time-sensitive state across multiple entities (securities, clients, orders) with strict ordering, sliding window calculations, and dynamic execution constraints, all within a single-threaded, event-driven processing model.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Process exchange prints, volume checks, and client orders
Broker Program for Exchange Interaction
You are tasked with developing a broker program that processes a stream of commands from stdin and writes required outputs to stdout. The program must process commands strictly in order.
Timestamps are integers, representing seconds since market open, and are guaranteed to be non-decreasing across all input lines. Security names and client names are unquoted strings without spaces. All numeric values (quantity, price, goal shares, participation rate) are guaranteed to fit within 32-bit signed integers.
Command Types
1. Trade Print (Market Trade)
Input format:
print <timestamp> <security> <quantity> <price>
quantityandpriceare positive integers.- This command updates the most recent trade price for the specified security.
2. Volume Check (Query)
Input format:
volume-check <timestamp> <security>
For each volume-check command, the program must output one line containing the trailing-minute market volume for that security at the given timestamp.
- Trailing-minute market volume is defined as the sum of
quantityfrom all market trade prints (print ...lines) for that security with timestamps in the last 60 seconds, ending at the query time. The window is inclusive on the upper bound and exclusive on the lower bound:t-60 < trade_timestamp <= t.
**Output format (for volume-check):
<volume>
Part 2: Trading for a Client
In addition to the above, the input stream may also contain client orders:
order <timestamp> <security> <client> <goal_shares> <participation_rate>
goal_sharesis a positive integer, representing the total shares the client wishes to trade.participation_rateis an integer percentage (e.g.,20means 20%).
Execution Rules for Client Orders
When processing the input stream, your program may generate its own trades to fill active client orders, subject to the following rules:
- Trade Price: Every trade executed by your program for a security must use the previous trade price for that security. This is defined as the most recent price seen for that security from an exchange
printline. If no price has ever been printed for a security, your program cannot trade it. - Participation Constraint (per security): At any given timestamp
t, the client’s cumulative executed shares for that security in the trailing minute (t-60 < trade_timestamp <= t) must not exceedfloor( market_volume(t) * participation_rate / 100 ).market_volume(t)here refers only to the trailing-minute market volume derived from exchangeprintlines, explicitly excluding any trades generated by your program. - Trade ASAP: After reading each input line, and before moving to the next line, your program must execute and output all trades that are currently feasible for any active orders. This is subject to the remaining
goal_sharesand the participation constraints. - Order Completion: An order remains active until its
goal_sharesare fully executed.
**Output format (for generated trades):
For each trade your program generates, output a line:
print <timestamp> <security> <num_shares> <price>
- The
timestampfor generated trades must be the timestamp of the currently processed input line that enabled the trade.
Notes and Edge Cases
- The system must handle multiple distinct securities and multiple outstanding client orders concurrently.
- Timestamps can repeat, meaning multiple commands may occur at the same second.
- The "trade ASAP" semantics must be strictly respected: all feasible executions at time
tmust be processed before reading the next input line. - You must choose and document a deterministic policy for allocating trading capacity if multiple active orders compete for the same security at the same timestamp (e.g., FIFO by order arrival time).