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.
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 <= 2000
orderId is unique across all orders.
orderType is either "buy" or "sell".
1 <= price <= 109
The total number of calls to addOrder, modifyOrder, cancelOrder, and getOrdersAtPrice does not exceed 2000.
For modifyOrder and cancelOrder, the specified orderId is guaranteed to exist and be active.
Solutions
Solution 1: Hash Table
We use a hash table \(\textit{orders}\) to store the type and price information of each order, where the key is the order ID and the value is a tuple \((\textit{orderType}, \textit{price})\). Additionally, we use another hash table \(\textit{t}\) to store the list of order IDs corresponding to each \((\textit{orderType}, \textit{price})\), where the key is a tuple \((\textit{orderType}, \textit{price})\) and the value is the list of order IDs.
When calling \(\texttt{addOrder}\), we add the order information to \(\textit{orders}\) and append the order ID to the corresponding list in \(\textit{t}\).
When calling \(\texttt{modifyOrder}\), we first retrieve the order type and old price from \(\textit{orders}\), then update the order's price information. Next, we remove the order ID from the corresponding list in \(\textit{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 \(\textit{orders}\), then remove the order ID from the corresponding list in \(\textit{t}\) and delete the order from \(\textit{orders}\).
When calling \(\texttt{getOrdersAtPrice}\), we directly return the list of order IDs corresponding to the query in \(\textit{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.
classOrderManagementSystem:def__init__(self):self.orders={}self.t=defaultdict(list)defaddOrder(self,orderId:int,orderType:str,price:int)->None:self.orders[orderId]=(orderType,price)self.t[(orderType,price)].append(orderId)defmodifyOrder(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)defcancelOrder(self,orderId:int)->None:orderType,price=self.orders[orderId]delself.orders[orderId]self.t[(orderType,price)].remove(orderId)defgetOrdersAtPrice(self,orderType:str,price:int)->List[int]:returnself.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)
classOrderManagementSystem{privaterecordKey(StringorderType,intprice){}privatefinalMap<Integer,String>orderTypeMap;privatefinalMap<Integer,Integer>priceMap;privatefinalMap<Key,List<Integer>>t;publicOrderManagementSystem(){orderTypeMap=newHashMap<>();priceMap=newHashMap<>();t=newHashMap<>();}publicvoidaddOrder(intorderId,StringorderType,intprice){orderTypeMap.put(orderId,orderType);priceMap.put(orderId,price);varkey=newKey(orderType,price);t.computeIfAbsent(key,_->newArrayList<>()).add(orderId);}publicvoidmodifyOrder(intorderId,intnewPrice){varorderType=orderTypeMap.get(orderId);varoldPrice=priceMap.get(orderId);priceMap.put(orderId,newPrice);t.get(newKey(orderType,oldPrice)).remove((Integer)orderId);t.computeIfAbsent(newKey(orderType,newPrice),_->newArrayList<>()).add(orderId);}publicvoidcancelOrder(intorderId){varorderType=orderTypeMap.remove(orderId);varprice=priceMap.remove(orderId);t.get(newKey(orderType,price)).remove((Integer)orderId);}publicint[]getOrdersAtPrice(StringorderType,intprice){varlist=t.getOrDefault(newKey(orderType,price),List.of());returnlist.stream().mapToInt(Integer::intValue).toArray();}}/** * Your OrderManagementSystem object will be instantiated and called as such: * OrderManagementSystem obj = new OrderManagementSystem(); * obj.addOrder(orderId,orderType,price); * obj.modifyOrder(orderId,newPrice); * obj.cancelOrder(orderId); * int[] param_4 = obj.getOrdersAtPrice(orderType,price); */
classOrderManagementSystem{usingKey=pair<string,int>;structKeyHash{size_toperator()(constKey&k)const{returnhash<string>()(k.first)^(hash<int>()(k.second)<<1);}};unordered_map<int,string>orderTypeMap;unordered_map<int,int>priceMap;unordered_map<Key,vector<int>,KeyHash>t;public:OrderManagementSystem(){}voidaddOrder(intorderId,stringorderType,intprice){orderTypeMap[orderId]=orderType;priceMap[orderId]=price;t[{orderType,price}].push_back(orderId);}voidmodifyOrder(intorderId,intnewPrice){stringorderType=orderTypeMap[orderId];intoldPrice=priceMap[orderId];priceMap[orderId]=newPrice;auto&oldList=t[{orderType,oldPrice}];oldList.erase(find(oldList.begin(),oldList.end(),orderId));t[{orderType,newPrice}].push_back(orderId);}voidcancelOrder(intorderId){stringorderType=orderTypeMap[orderId];intprice=priceMap[orderId];orderTypeMap.erase(orderId);priceMap.erase(orderId);auto&list=t[{orderType,price}];list.erase(find(list.begin(),list.end(),orderId));}vector<int>getOrdersAtPrice(stringorderType,intprice){autoit=t.find({orderType,price});if(it==t.end())return{};returnit->second;}};/** * Your OrderManagementSystem object will be instantiated and called as such: * OrderManagementSystem* obj = new OrderManagementSystem(); * obj->addOrder(orderId,orderType,price); * obj->modifyOrder(orderId,newPrice); * obj->cancelOrder(orderId); * vector<int> param_4 = obj->getOrdersAtPrice(orderType,price); */
typeKeystruct{orderTypestringpriceint}typeOrderManagementSystemstruct{orderTypeMapmap[int]stringpriceMapmap[int]inttmap[Key][]int}funcConstructor()OrderManagementSystem{returnOrderManagementSystem{orderTypeMap:make(map[int]string),priceMap:make(map[int]int),t:make(map[Key][]int),}}func(this*OrderManagementSystem)AddOrder(orderIdint,orderTypestring,priceint){this.orderTypeMap[orderId]=orderTypethis.priceMap[orderId]=pricekey:=Key{orderType,price}this.t[key]=append(this.t[key],orderId)}func(this*OrderManagementSystem)ModifyOrder(orderIdint,newPriceint){orderType:=this.orderTypeMap[orderId]oldPrice:=this.priceMap[orderId]this.priceMap[orderId]=newPriceoldKey:=Key{orderType,oldPrice}oldList:=this.t[oldKey]fori,v:=rangeoldList{ifv==orderId{this.t[oldKey]=append(oldList[:i],oldList[i+1:]...)break}}newKey:=Key{orderType,newPrice}this.t[newKey]=append(this.t[newKey],orderId)}func(this*OrderManagementSystem)CancelOrder(orderIdint){orderType:=this.orderTypeMap[orderId]price:=this.priceMap[orderId]delete(this.orderTypeMap,orderId)delete(this.priceMap,orderId)key:=Key{orderType,price}list:=this.t[key]fori,v:=rangelist{ifv==orderId{this.t[key]=append(list[:i],list[i+1:]...)break}}}func(this*OrderManagementSystem)GetOrdersAtPrice(orderTypestring,priceint)[]int{key:=Key{orderType,price}returnthis.t[key]}/** * Your OrderManagementSystem object will be instantiated and called as such: * obj := Constructor(); * obj.AddOrder(orderId,orderType,price); * obj.ModifyOrder(orderId,newPrice); * obj.CancelOrder(orderId); * param_4 := obj.GetOrdersAtPrice(orderType,price); */
classOrderManagementSystem{privateorderTypeMap:Map<number,string>;privatepriceMap:Map<number,number>;privatet:Map<string,number[]>;constructor(){this.orderTypeMap=newMap();this.priceMap=newMap();this.t=newMap();}privatekey(orderType:string,price:number):string{return`${orderType}#${price}`;}addOrder(orderId:number,orderType:string,price:number):void{this.orderTypeMap.set(orderId,orderType);this.priceMap.set(orderId,price);constk=this.key(orderType,price);if(!this.t.has(k)){this.t.set(k,[]);}this.t.get(k)!.push(orderId);}modifyOrder(orderId:number,newPrice:number):void{constorderType=this.orderTypeMap.get(orderId)!;constoldPrice=this.priceMap.get(orderId)!;this.priceMap.set(orderId,newPrice);constoldKey=this.key(orderType,oldPrice);constoldList=this.t.get(oldKey)!;constidx=oldList.indexOf(orderId);if(idx!==-1){oldList.splice(idx,1);}constnewKey=this.key(orderType,newPrice);if(!this.t.has(newKey)){this.t.set(newKey,[]);}this.t.get(newKey)!.push(orderId);}cancelOrder(orderId:number):void{constorderType=this.orderTypeMap.get(orderId)!;constprice=this.priceMap.get(orderId)!;this.orderTypeMap.delete(orderId);this.priceMap.delete(orderId);constk=this.key(orderType,price);constlist=this.t.get(k)!;constidx=list.indexOf(orderId);if(idx!==-1){list.splice(idx,1);}}getOrdersAtPrice(orderType:string,price:number):number[]{returnthis.t.get(this.key(orderType,price))??[];}}/** * Your OrderManagementSystem object will be instantiated and called as such: * var obj = new OrderManagementSystem() * obj.addOrder(orderId,orderType,price) * obj.modifyOrder(orderId,newPrice) * obj.cancelOrder(orderId) * var param_4 = obj.getOrdersAtPrice(orderType,price) */
usestd::collections::HashMap;structOrderManagementSystem{orders:HashMap<i32,(String,i32)>,t:HashMap<(String,i32),Vec<i32>>,}implOrderManagementSystem{fnnew()->Self{Self{orders:HashMap::new(),t:HashMap::new(),}}fnadd_order(&mutself,order_id:i32,order_type:String,price:i32){self.orders.insert(order_id,(order_type.clone(),price));self.t.entry((order_type,price)).or_insert_with(Vec::new).push(order_id);}fnmodify_order(&mutself,order_id:i32,new_price:i32){ifletSome((order_type,old_price))=self.orders.get(&order_id).cloned(){self.orders.insert(order_id,(order_type.clone(),new_price));ifletSome(v)=self.t.get_mut(&(order_type.clone(),old_price)){ifletSome(pos)=v.iter().position(|&x|x==order_id){v.remove(pos);}}self.t.entry((order_type,new_price)).or_insert_with(Vec::new).push(order_id);}}fncancel_order(&mutself,order_id:i32){ifletSome((order_type,price))=self.orders.remove(&order_id){ifletSome(v)=self.t.get_mut(&(order_type,price)){ifletSome(pos)=v.iter().position(|&x|x==order_id){v.remove(pos);}}}}fnget_orders_at_price(&self,order_type:String,price:i32)->Vec<i32>{self.t.get(&(order_type,price)).cloned().unwrap_or_default()}}/** * Your OrderManagementSystem object will be instantiated and called as such: * let obj = OrderManagementSystem::new(); * obj.add_order(orderId, orderType, price); * obj.modify_order(orderId, newPrice); * obj.cancel_order(orderId); * let ret_4: Vec<i32> = obj.get_orders_at_price(orderType, price); */