Order Processing System
Modern e-commerce platforms handle a continuous stream of customer orders, each with varying urgency. Efficiently managing these orders, especially when processing resources are limited, is crucial for customer satisfaction and operational efficiency. This problem challenges you to design a core component of such a system, focusing on prioritizing critical orders while maximizing throughput within resource constraints.
You are tasked with implementing the OrderProcessingSystem class. This class should manage a pool of incoming orders, prioritize premium orders, and ensure that a fixed maximum number of orders are processed concurrently.
Your implementation must include the following methods:
constructor(capacity: number): Initializes the order processing system with a specified maximum number of orders that can be processed simultaneously.addOrder(orderId: number, isPremium: boolean): void: Adds a new order to the system. isPremium indicates whether the order should be given higher priority.startProcessing(): number | null: Attempts to start processing an order.
null.completeOrder(orderId: number): void: Marks an order as completed. This frees up the processing slot occupied by orderId. You can assume orderId was previously returned by startProcessing() and is currently in an 'in-progress' state.Class: OrderProcessingSystem
Constructor:
capacity: number: The maximum number of orders that can be processed concurrently.Method: addOrder
orderId: number: A unique identifier for the order.isPremium: boolean: true if the order is premium, false otherwise.Method: startProcessing
number | null: The orderId of the order that just started processing, or null if no order could be started.Method: completeOrder
orderId: number: The ID of the order to mark as complete.orderId values passed to addOrder will be unique.completeOrder will only be called with orderIds that were previously returned by startProcessing() and are currently in an 'in-progress' state. You do not need to handle cases where an invalid orderId is passed to completeOrder.Examples
Example 1
Input:
operations = ["OrderProcessingSystem","addOrder","addOrder","startProcessing","startProcessing","addOrder","startProcessing","completeOrder","startProcessing","completeOrder","startProcessing"], params = [[1],[101,false],[102,true],[],[],[103,false],[],[102],[],[101],[]]
Output:
[null,null,null,102,null,null,null,null,101,null,103]
Note:
Example 2
Input:
operations = ["OrderProcessingSystem","addOrder","addOrder","addOrder","startProcessing","startProcessing","startProcessing","completeOrder","startProcessing","completeOrder","startProcessing"], params = [[2],[201,false],[202,true],[203,false],[],[],[],[202],[],[201],[]]
Output:
[null,null,null,null,202,201,null,null,203,null,null]
Note:
Example 3
Input:
operations = ["OrderProcessingSystem","addOrder","startProcessing","completeOrder","startProcessing"], params = [[1],[301,true],[],[301],[]]
Output:
[null,null,301,null,null]
Note:
Constraints
1 <= capacity <= 10001 <= orderId <= 10^9addOrder calls will not exceed 10^5.Array.prototype.shift for JS, Queue<T> for TS) are allowed.Loading editor...