Design Order Management System Problem
Description
LeetCode Problem 3822.
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 thatorderIdis 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 theorderIds of all active orders that match the givenorderTypeandprice. 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();
orderManagementSystem.addOrder(1, "buy", 1); // A buy order with ID 1 is added at price 1.
orderManagementSystem.addOrder(2, "buy", 1); // A buy order with ID 2 is added at price 1.
orderManagementSystem.addOrder(3, "sell", 2); // A sell order with ID 3 is added at price 2.
orderManagementSystem.getOrdersAtPrice("buy", 1); // Both buy orders (IDs 1 and 2) are active at price 1, so the result is [2, 1].
orderManagementSystem.modifyOrder(1, 3); // Order 1 is updated: its price becomes 3.
orderManagementSystem.modifyOrder(2, 1); // Order 2 is updated, but its price remains 1.
orderManagementSystem.getOrdersAtPrice("buy", 1); // Only order 2 is still an active buy order at price 1, so the result is [2].
orderManagementSystem.cancelOrder(3); // The sell order with ID 3 is canceled and removed from active orders.
orderManagementSystem.cancelOrder(2); // The buy order with ID 2 is canceled and removed from active orders.
orderManagementSystem.getOrdersAtPrice("buy", 1); // There are no active buy orders left at price 1, so the result is [].
Constraints:
1 <= orderId <= 2000orderIdis unique across all orders.orderTypeis either"buy"or"sell".1 <= price <= 10^9- The total number of calls to
addOrder,modifyOrder,cancelOrder, andgetOrdersAtPricedoes not exceed 2000. - For
modifyOrderandcancelOrder, the specifiedorderIdis guaranteed to exist and be active.
Solution
Approach: Hash Table
Use two hash tables. orders maps an order id to its (orderType, price), and t maps each (orderType, price) pair to the list of order ids that are active there.
addOrder: store the order inordersand append its id to the list for its type and price.modifyOrder: read the order's type and old price, remove the id from the old list, update the price inorders, and append the id to the list for the new price.cancelOrder: read the type and price, remove the id from its list and delete the order fromorders.getOrdersAtPrice: return the list stored for the queried type and price (an empty list if there is none).
Appending and lookups are O(1). Removing an id from a list is linear in that list's length, which is fine because there are at most 2000 operations.
Complexity: O(1) for add and query, O(k) for modify and cancel where k is the size of one price list. Space is O(m) for m orders.
from collections import defaultdict
class OrderManagementSystem:
def __init__(self):
self.orders = {}
self.t = defaultdict(list)
def addOrder(self, orderId: int, orderType: str, price: int) -> None:
self.orders[orderId] = (orderType, price)
self.t[(orderType, price)].append(orderId)
def modifyOrder(self, orderId: int, newPrice: int) -> None:
orderType, price = self.orders[orderId]
self.orders[orderId] = (orderType, newPrice)
self.t[(orderType, price)].remove(orderId)
self.t[(orderType, newPrice)].append(orderId)
def cancelOrder(self, orderId: int) -> None:
orderType, price = self.orders[orderId]
del self.orders[orderId]
self.t[(orderType, price)].remove(orderId)
def getOrdersAtPrice(self, orderType: str, price: int) -> List[int]:
return self.t[(orderType, price)]
# Your OrderManagementSystem object will be instantiated and called as such:
# obj = OrderManagementSystem()
# obj.addOrder(orderId,orderType,price)
# obj.modifyOrder(orderId,newPrice)
# obj.cancelOrder(orderId)
# param_4 = obj.getOrdersAtPrice(orderType,price)