623. Design Order Management System

You are required to develop a basic order management system for a trading platform.

Each order is linked 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:

calls = ["OrderManagementSystem", "addOrder", "addOrder", "addOrder", "getOrdersAtPrice", "modifyOrder", "modifyOrder", "getOrdersAtPrice", "cancelOrder", "cancelOrder", "getOrdersAtPrice"]

params = [[], [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 [].

Example 2:

Input:

calls = ["OrderManagementSystem", "addOrder", "getOrdersAtPrice", "cancelOrder", "getOrdersAtPrice"] [[], params = [10, "sell", 100], ["sell", 100], [10], ["sell", 100]]

Output: [null, null, [10], null, []]

Explanation:

  • OrderManagementSystem orderManagementSystem = new OrderManagementSystem(); orderManagementSystem.addOrder(10, "sell", 100); // Sell order ID 10 is added at 100. orderManagementSystem.getOrdersAtPrice("sell", 100); // Returns [10]. orderManagementSystem.cancelOrder(10); // Order 10 is canceled. orderManagementSystem.getOrdersAtPrice("sell", 100); // Order 10 is no longer active, returns [].

Now Your Turn!

Pick the correct output for the given input

Input:

calls = ["OrderManagementSystem", "addOrder", "getOrdersAtPrice", "cancelOrder", "getOrdersAtPrice"] [[], params = [10, "sell", 100], ["sell", 100], [10], ["sell", 100]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

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.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class OrderManagementSystem {
public:
OrderManagementSystem() {
}
void addOrder(int orderId, string orderType, int price) {
}
void modifyOrder(int orderId, int newPrice) {
}
void cancelOrder(int orderId) {
}
vector<int> getOrdersAtPrice(string orderType, int price) {
}
};
 
/**
* 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);
*/
Test Case

Input:

Calls
Params