FastPrepFastPrep
Problem Brief

Calculate Net Profit

OA

A quantitative trading firm aims to develop a tool to track the net profit/loss of the firm at any point in time. This tool processes a list of events, where each event falls into one of four categories:

  • BUY stock quantity: Indicates the purchase of quantity shares of stock at the market price.
  • SELL stock quantity: Indicates the sale of quantity shares of stock at the market price.
  • CHANGE stock price: Indicates a change in the market price of stock by price amount, which can be positive or negative.
  • QUERY: Requests the net profit/loss from the start of trading until the current time.
  • The tool should return a list of numbers corresponding to each QUERY event.

    Function Description

    Complete the function calculateNetProfit in the editor.

    calculateNetProfit has the following parameter:

    1. String[] events: an array of strings describing the events

    Returns

    long[]: the answers to the "QUERY" events

    My deepest, sincere, and boundless gratitude to an amazing friend for all their help. 🩵

    1Example 1

    Input
    events = ["BUY googl 20", "BUY aapl 50", "CHANGE googl 6", "QUERY", "SELL aapl 10", "CHANGE aapl 2", "QUERY"]
    Output
    [0, 0]
    Explanation
    Hallo ~ Output is just placeholder, but the example input is complete~ As alwasy, will update more once come across more sources on the internet...

    Constraints

    Limits and guarantees your solution can rely on.

    • 1 ≤ n ≤ 10^5
    • | events[] | ≤ 21
    • For query, SELL stock quantity, it is guaranteed that there are enough shares owned.
    • 1 ≤ quantity ≤ 10^3
    • The absolute value of a change in the price of any stock at any event will not exceed 10³.
    public long[] calculateNetProfit(String[] events) {
      // write your code here
    }
    
    Input

    events

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

    Output

    [0, 0]

    Sign in to submit your solution.