cswork / OAOpen practice workspace

JPMorgan ChaseOA #5

Calculate Net Profit

Estimated MediumJudge-ready

Topics (AI-labelled)SimulationHashing

BUY and SELL trade at the current market price, CHANGE is a change in price, and QUERY asks for the net profit and loss from the start until now, including the unrealized profit and loss on holdings not yet sold; SELL is guaranteed to have enough holdings. The original example's output is a placeholder; the correct query results are 120 and 200.

Input

The input is a JSON array of strings, where each item is an event: BUY stock quantity, SELL stock quantity, CHANGE stock delta, or QUERY. Original bounds: 1≤number of events≤100000, and each event has ≤21 characters (this site counts Unicode code points); quantities are 1..1000, and |delta|≤1000. A stock name is a single non-empty token without whitespace, preserving Unicode and the non-whitespace control characters represented by JSON escapes; event fields are separated by a single space.

Output

The first line is the number of queries, followed by one net profit and loss per line; if there is no QUERY, output only 0.

Example 1

Input:

["BUY googl 20", "BUY aapl 50", "CHANGE googl 6", "QUERY", "SELL aapl 10", "CHANGE aapl 2", "QUERY"]

Output:

2
120
200

Example 2

Input:

["QUERY", "BUY A 2", "CHANGE A -3", "QUERY"]

Output:

2
0
-6

Example 3

Input:

["BUY A 1", "SELL A 1"]

Output:

0

Example notes

The outputs of the three public examples are, in order: 2 / 120 / 200; 2 / 0 / -6; 0. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…