Skip to main content
Back to problems
#3822
Medium Algorithms

Design order management system

Hash Table Design
79.3% acceptance
Apr 3, 2026
8
1
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 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.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};

#[derive(Clone, Copy)]
struct Order {
  is_buy: bool,
  price: i32,
}

struct State {
  orders: HashMap<i32, Order>,
  price_levels: HashMap<(bool, i32), HashSet<i32>>,
}

impl State {
  fn add_to_level(&mut self, order_id: i32, is_buy: bool, price: i32) {
    self.price_levels
      .entry((is_buy, price))
      .or_default()
      .insert(order_id);
  }

  fn remove_from_level(&mut self, order_id: i32, is_buy: bool, price: i32) {
    let key = (is_buy, price);
    let should_remove = if let Some(ids) = self.price_levels.get_mut(&key) {
      ids.remove(&order_id);
      ids.is_empty()
    } else {
      false
    };
    if should_remove {
      self.price_levels.remove(&key);
    }
  }
}

struct OrderManagementSystem {
  state: RefCell<State>,
}


/** 
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl OrderManagementSystem {

  fn new() -> Self {
    Self {
      state: RefCell::new(State {
        orders: HashMap::new(),
        price_levels: HashMap::new(),
      }),
    }
  }
  
  fn add_order(&self, order_id: i32, order_type: String, price: i32) {
    let is_buy = order_type == "buy";
    let mut state = self.state.borrow_mut();
    state.orders.insert(order_id, Order { is_buy, price });
    state.add_to_level(order_id, is_buy, price);
  }
  
  fn modify_order(&self, order_id: i32, new_price: i32) {
    let mut state = self.state.borrow_mut();
    let order = state.orders.get(&order_id).copied().unwrap();
    if order.price == new_price {
      return;
    }

    state.remove_from_level(order_id, order.is_buy, order.price);
    state.add_to_level(order_id, order.is_buy, new_price);
    state.orders.get_mut(&order_id).unwrap().price = new_price;
  }
  
  fn cancel_order(&self, order_id: i32) {
    let mut state = self.state.borrow_mut();
    let order = state.orders.remove(&order_id).unwrap();
    state.remove_from_level(order_id, order.is_buy, order.price);
  }
  
  fn get_orders_at_price(&self, order_type: String, price: i32) -> Vec<i32> {
    let is_buy = order_type == "buy";
    let state = self.state.borrow();
    state
      .price_levels
      .get(&(is_buy, price))
      .map(|ids| ids.iter().copied().collect())
      .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);
 */