You are asked to design a simple order management system for a trading platform.
Each order is associated with an orderId, an orderType ("buy" or "sell"), and a price.
An order is considered active unless it is canceled.
Implement the OrderManagementSystem class:
OrderManagementSystem(): Initializes the order management system.void addOrder(int orderId, string orderType, int price): Adds a new active order with the given attributes. It is guaranteed that orderId is unique.void modifyOrder(int orderId, int newPrice): Modifies the price of an existing order. It is guaranteed that the order exists and is active.void cancelOrder(int orderId): Cancels an existing order. It is guaranteed that the order exists and is active.vector<int> getOrdersAtPrice(string orderType, int price): Returns the orderIds of all active orders that match the given orderType and price. If no such orders exist, return an empty list.Note: The order of returned orderIds does not matter.
Example 1:
Input:
["OrderManagementSystem", "addOrder", "addOrder", "addOrder", "getOrdersAtPrice", "modifyOrder", "modifyOrder", "getOrdersAtPrice", "cancelOrder", "cancelOrder", "getOrdersAtPrice"]
[[], [1, "buy", 1], [2, "buy", 1], [3, "sell", 2], ["buy", 1], [1, 3], [2, 1], ["buy", 1], [3], [2], ["buy", 1]]
Output:
[null, null, null, null, [2, 1], null, null, [2], null, null, []]
Explanation
OrderManagementSystem orderManagementSystem = new OrderManagementSystem();[2, 1].[2].[].
Constraints:
1 <= orderId <= 2000orderId is unique across all orders.orderType is either "buy" or "sell".1 <= price <= 109addOrder, modifyOrder, cancelOrder, and getOrdersAtPrice does not exceed 2000.modifyOrder and cancelOrder, the specified orderId is guaranteed to exist and be active.We use a hash table orders to store the type and price information of each order, where the key is the order ID and the value is a tuple (orderType, price). Additionally, we use another hash table t to store the list of order IDs corresponding to each (orderType, price), where the key is a tuple (orderType, price) and the value is the list of order IDs.
When calling \texttt{addOrder}, we add the order information to orders and append the order ID to the corresponding list in t.
When calling \texttt{modifyOrder}, we first retrieve the order type and old price from orders, then update the order's price information. Next, we remove the order ID from the corresponding list in t and add it to the list corresponding to the new price.
When calling \texttt{cancelOrder}, we retrieve the order type and price information from orders, then remove the order ID from the corresponding list in t and delete the order from orders.
When calling \texttt{getOrdersAtPrice}, we directly return the list of order IDs corresponding to the query in t.
In the above operations, the time complexity for adding and retrieving the order ID list is O(1), while the time complexity for removing an order ID from the list is O(n), where n is the length of the corresponding list. Since the total number of orders in the problem does not exceed 2000, this method is efficient enough in practice. The space complexity is O(m), where m is the total number of orders.
Java
C++
Go
TypeScript
Rust
Practice Design Order Management System with our built-in code editor and test cases.
Practice on FleetCode