Hashtables and Hashsets

Not the best solution for this problem, which also has a lot of corner cases, but the use of Hashtables and Hashsets help to achieve the goal. Whenever you have a choice, prefer Hashsets over Hashtables, and even the latter isn't super performing due to the number of casts needed. Also, in addition the slowdown here is exacerbated by the use of strings (and conversions) manipulations for the order keys. Lots of room for improvement for sure, but it is a good problem to exercise Hashtables and Hashsets in general. Code is down below, cheers, ACC.

Design Order Management System - LeetCode

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();
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 addOrdermodifyOrdercancelOrder, and getOrdersAtPrice does not exceed 2000.
  • For modifyOrder and cancelOrder, the specified orderId is guaranteed to exist and be active.
 

Seen this question in a real interview before?
1/5
Yes
No
Accepted
917/1.2K
Acceptance Rate
78.8%

public class OrderManagementSystem
{
    private Hashtable ordersByTypePrice = null;
    private HashSet ordersCancelled = null;
    private Hashtable latestPrice = null;
    private Hashtable orderIdToTorderType = null;
    public OrderManagementSystem()
    {
        ordersByTypePrice = new Hashtable();
        ordersCancelled = new HashSet();
        latestPrice = new Hashtable();
        orderIdToTorderType = new Hashtable();
    }

    public void AddOrder(int orderId, string orderType, int price)
    {
        string key = orderType + "@" + price.ToString();
        if (!ordersByTypePrice.ContainsKey(key)) ordersByTypePrice.Add(key, new List());
        List orders = (List) ordersByTypePrice[key];
        orders.Add(orderId);
        latestPrice.Add(orderId, price.ToString());
        orderIdToTorderType.Add(orderId, orderType);
    }

    public void ModifyOrder(int orderId, int newPrice)
    {
        if (newPrice.ToString() == latestPrice[orderId]) 
            return;
        string orderType = (string)orderIdToTorderType[orderId];
        string key = orderType + "@" + newPrice.ToString();
        if (!ordersByTypePrice.ContainsKey(key)) ordersByTypePrice.Add(key, new List());
        List orders = (List)ordersByTypePrice[key];
        orders.Add(orderId);
        latestPrice[orderId] = newPrice.ToString();
    }

    public void CancelOrder(int orderId)
    {
        if(!ordersCancelled.Contains(orderId))
            ordersCancelled.Add(orderId);
    }

    public int[] GetOrdersAtPrice(string orderType, int price)
    {
        string key = orderType + "@" + price.ToString();
        if (!ordersByTypePrice.ContainsKey(key)) return new int[0];
        List orders = (List)ordersByTypePrice[key];
        List retVal = new List();
        foreach (int orderId in orders)
        {
            if (!ordersCancelled.Contains(orderId) && price.ToString() == (string)latestPrice[orderId] && !retVal.Contains(orderId))
            {
                retVal.Add(orderId);
            }
        }

        return retVal.ToArray();
    }
}

Comments

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Classic Dynamic Programming IX

HashSet I