注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
第一题解法是keep track of the maximum buy price and minimum sell price, 每次需要return worst trade的时候就算现在价格和maximum buy and minimum sell 的差。第二题我没时间做,好像不难。求加米,看面经!
Question 1 Worst trade reporter
You are tasked with building a tool to analyze the profitability of trades (also known as "profit and loss" or PnL). For the purpose of this problem, every trade has the following attributes:
• TradeID - unique identifier for the trade,
• InstrumentID - identifier of the instrument that has been traded,
• Buy/Sell - flag to indicate whether you bought or sold the instrument,
• Price - the price at which the instrument has been traded,
• Volume - the quantity of the instrument that has been traded.
Consider a trade with the following attributes:
• TradeID = 5,
• InstrumentID = Google,
• Buy/Sell = BUY,
• Price = 500,
• Volume = 20.
It means that you bought 20 lots of Google stock for 500 dollars and the identifier for the trade is 5. Note that trades have a TradeID because there can be multiple trades with the same InstrumentID, Buy/Sell, Price and Volume attributes.
Problem Statement
In this task, you receive a stream of N instructions. Each instruction can be an update or a query. An update can be of 2 kinds:
• Trade - Indicates that a trade has happened. Attributes: TradeID, InstrumentID, Buy/Sell, Price, and Volume.
• Price - Indicates that the true value of an Instrument has been updated. Attributes: instrumentID and Price.
There is only 1 kind of query:
• WorstTrade - output the TradeID of the worst trade for an instrument. Attributes: instrumentID.
The WorstTrade is the trade with the highest loss made per lot of the trade. To calculate PnL of a trade the latest price update for an instrument is used as the true value of the instrument.
• If you sell 2 Google stocks for 500 each, and had the latest price update saying Google is worth 400, the PnL for the trade is (500 - 400) *
3. R <ClientId> means that the Client with ID ClientId requests position updates.
The ouput format is defined as follows:
1. Clients print the answer they get as a subscription request like this
• S <ClientId> <TruckId> <x_pos> <y_pos>
2. Clients print all updates they get as
• U <ClientID> <TruckId> <delta_x> <delta_y>
Example
Input
1
2 3
U 0 1.5 2.5
S 0 0
U 0 1 2
U 0 -0.5 -0.5
S 1 0
R 0
U 0 1 1
R 1
Output
S 0 0 3.5 5.5
S 1 0 4 7
U 0 1 2
U 0 -0.5 -0.5
U 1 1 1
Explanation:
• The first line of output is produced when client with ID=0 subscribes to truck with ID=0. At that point truck with ID=0 is at (3.5, 5.5).
• The second line of output is produced when client with ID=1 subscribes to truck with ID=0. At that point the truck is at (4, 7).
• The third and fourth line of output are produced when client with ID=0 requests position updates for truck with ID=0. Since there were 2 updates after the subscription, there's one line for each update
• The fifth line of output is produced when client with ID=1 requests position updates for truck with ID=0. Since there was 1 update after the subscription, there's one line of output.
Starting Code- from dataclasses import dataclass, replace
- @dataclass
- class TruckPosition:
- x: float
- y: float
- @dataclass
- class TruckPositionDelta:
- truck_id: int
- delta_x: float
- delta_y: float
- class Subscriber:
- def __init__(self, server):
- pass
- def process_update(self, update):
- pass
- def subscribe_to_truck(self, truck_id, client_id):
- return TruckPosition(0, 0)
- def get_updates(self, client_id):
- return [TruckPositionDelta(0, 0, 0)]
-
- class Server:
- def __init__(self):
- self.registered_trucks = set()
- self.current_pos = {}
-
- def subscribe_to_truck(self, truck_id):
- self.registered_trucks.add(truck_id)
- return replace(self.current_pos([truck_id])
-
- def add_position(self, truck_id, pos):
- self.current_pos[truck_id] = pos
复制代码 |