KatInterview

Sharded Order Book Simulator with Cancel and Open Lookup

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationstate-machine

Last updated

Simulate a sharded order book that routes users with FNV-1a, enforces a one-way status lifecycle, and answers per-user open-order queries in placement order.

Sharded Order Book Simulator

You are given a batch of requests for a small order book. Process them in order and return one result per request.

Users are spread over shard_count shards. A user's shard is fnv1a_32(user_id) % shard_count, where fnv1a_32 is the 32-bit FNV-1a hash of the UTF-8 bytes of user_id:

  1. Start with h = 2166136261.
  2. For each byte b of the UTF-8 encoding, do h = h XOR b, then h = (h * 16777619) mod 2^32.

Every order has one of three statuses: OPEN, FILLED, or CANCELLED. A new order starts as OPEN. An OPEN order may later become CANCELLED. Once an order leaves OPEN, its status never changes again.

Requests

Request i has the name commands[i] and the argument list params[i].

  • "place_order" with [user_id, order_id, symbol, amount]: create a new OPEN order owned by user_id, stored in that user's shard.
    • Order IDs are unique across the whole system and forever. If order_id was ever used before (including by an order that is now cancelled, and regardless of owner), do nothing and return False.
    • Otherwise, store the order and return True.
  • "cancel_order" with [user_id, order_id]: look the order up in the shard of user_id.
    • If the order does not exist there, belongs to a different user, or is not OPEN, change nothing and return False.
    • Otherwise, set its status to CANCELLED and return True.
  • "get_user_orders" with [user_id]: return a list of all orders of that user that are currently OPEN, in the order they were placed (oldest first). Each order is a dictionary with exactly these keys: "order_id", "user_id", "symbol", "amount", "status". The amount is returned exactly as it was supplied. The status of every returned order is "OPEN". If the user has no open orders, return []. The returned dictionaries must be copies, so later requests never alter results already produced.
  • Any other command name is invalid and must raise ValueError.

Task

Implement process_order_requests(shard_count, commands, params) so that it returns the list of results, one per request, in input order.

A good solution answers place_order and cancel_order in O(1) average time. get_user_orders should take time proportional to the number of that user's open orders, not to the total number of orders.

Constraints

  • 1 <= shard_count <= 1024
  • len(commands) == len(params), and the total number of requests is up to about 10^6.
  • user_id, order_id, and symbol are strings. user_id may contain non-ASCII characters.
  • amount is a number (int or float).
  • The result list contains booleans for place_order and cancel_order, and lists of dictionaries for get_user_orders.

Examples

Example 1

Input: process_order_requests(4, ['place_order', 'place_order', 'cancel_order', 'get_user_orders'], [['user1', 'ordA', 'BTC', 1], ['user1', 'ordB', 'ETH', 2], ['user1', 'ordA'], ['user1']])

Output: [True, True, True, [{'order_id': 'ordB', 'user_id': 'user1', 'symbol': 'ETH', 'amount': 2, 'status': 'OPEN'}]]

Example 2

Input: process_order_requests(1, ['place_order', 'get_user_orders'], [['alice', 'o1', 'AAPL', 3.5], ['alice']])

Output: [True, [{'order_id': 'o1', 'user_id': 'alice', 'symbol': 'AAPL', 'amount': 3.5, 'status': 'OPEN'}]]

Example 3

Input: process_order_requests(2, ['place_order', 'place_order', 'cancel_order', 'get_user_orders'], [['u1', 'x', 'BTC', 1], ['u2', 'y', 'ETH', 4], ['wrong-user', 'x'], ['u1']])

Output: [True, True, False, [{'order_id': 'x', 'user_id': 'u1', 'symbol': 'BTC', 'amount': 1, 'status': 'OPEN'}]]

Hints

Hint 1

Cancelled orders must still remember their IDs, otherwise an old order_id could be placed again. Keep a record of every ID ever used.

Hint 2

Keep a second index per user that holds only the OPEN orders, using a dictionary so insertion order (placement order) is preserved and deletion is O(1).

Hint 3

For FNV-1a, XOR the byte in first, then multiply by the prime and mask with 0xFFFFFFFF. Remove a user's entry from the open index when their last open order is cancelled, and return copies of the order dictionaries.