← 返回 coinbase 的题目列表Crypto Trading Order Management System
类型:online_judge
Problem: Crypto Trading Order Management System
Design and implement a cryptocurrency order management system that supports placing, pausing, resuming, canceling, completing, and displaying live orders.
Each order contains:
id: globally unique order ID
currency: cryptocurrency symbol, e.g. BTC, ETH
amount: positive integer amount
timestamp: order placement time
type: either buy or sell
state: one of live, paused, completed, canceled
Implement CryptoTradingSystem:
CryptoTradingSystem()
String placeOrder(String id, String currency, int amount, int timestamp, String type)
String pauseOrder(String id)
String resumeOrder(String id)
String cancelOrder(String id)
String completeOrder(String id)
List displayLiveOrders()
Rules:
placeOrder: creates a new live order. Return an empty string if the ID already exists or parameters are invalid; otherwise return the order ID.
pauseOrder: only a live order can be paused.
resumeOrder: only a paused order can be resumed.
cancelOrder: only a live or paused order can be canceled.
completeOrder: only a live order can be completed.
displayLiveOrders: returns all live orders sorted by ascending timestamp; if timestamps tie, sort by ascending id.
Follow-up 1: Multiple users
Add:
userId: the user who owns the order
The API becomes:
String placeOrder(String id, String currency, int amount, int timestamp, String type, String userId)
int cancelAllOrders(String userId)
cancelAllOrders(userId) cancels all live or paused orders associated with the given user and returns the number of successfully canceled orders.
Follow-up 2: n data streams
Initialize the system with n streams:
CryptoTradingSystem(int n)
Requirements:
All orders from the same user must stay in the same stream.
You may use hash(userId) % n to select the stream.
Preserve all previous functionality.
Constraints
1 <= n <= 10^4
Number of operations Q <= 2 * 10^5
id and userId length at most 64
1 <= amount <= 10^9
1 <= timestamp <= 10^18
Input/output format for testing
First line:
n q
Next q lines contain operations:
PLACE id currency amount timestamp type userId
PAUSE id
RESUME id
CANCEL id
COMPLETE id
CANCEL_ALL userId
DISPLAY
Output rules:
For string-returning operations, print the returned value; print EMPTY for an empty string.
For CANCEL_ALL, print the number of canceled orders.
For DISPLAY, print live order IDs separated by commas; print EMPTY if there are no live orders.
Example
Input
1 6
PLACE o1 BTC 10 100 buy u1
PLACE o2 ETH 5 90 sell u2
DISPLAY
PAUSE o2
DISPLAY
RESUME o2
Output
o1
o2
o2,o1
o2
o1
o2