FastPrepRealized Gains from a Real-Estate Transaction Stream

Realized Gains from a Real-Estate Transaction Stream

Kikoff logoKikoff● MediumFULLTIMEPHONE SCREEN

Problem statement

You are given a stream of real-estate transactions in time order. Transaction i says that persons[i] did actions[i] on properties[i] at prices[i].

  • "buy": the person buys the property. They now hold it, and the price is their cost.
  • "sell": the person sells the property. The realized gain is the sale price minus the seller's cost, which is negative for a loss. If the property has no earlier record in the stream, its cost is 0. After a sale, nobody holds the property.

Right after each transaction, record that transaction's person's total realized gain or loss so far: the sum over all of their sales up to now. A buy does not change the total.

Return the recorded totals, one per transaction.

Function

realizedGainsAfterEachTransaction(persons: String[], properties: String[], actions: String[], prices: int[]) → long[]

Examples

Example 1

persons = ["alice","bob","alice","carl","carl","alice"]properties = ["elm1","oak7","elm1","elm1","elm1","oak7"]actions = ["buy","sell","sell","buy","sell","buy"]prices = [100,50,130,120,90,70]return = [0,50,30,0,-30,30]

Alice buys elm1 for 100; she has realized nothing, so 0. Bob sells oak7, which has no earlier record, so its cost is 0 and his gain is 50. Alice sells elm1 for 130, a gain of 30. Carl buys elm1 for 120 (0 for Carl), then sells it for 90, a loss of 30, so Carl is at -30. Alice buys oak7; buying does not change her total of 30.

Example 2

persons = ["dan","dan","dan","dan"]properties = ["a1","a1","a1","a1"]actions = ["buy","sell","buy","sell"]prices = [500,400,450,700]return = [0,-100,-100,150]

Dan loses 100 on the first sale (400 - 500), so his total is -100. He buys a1 again for 450 and sells it for 700, a gain of 250. The second sale uses the second purchase price, so his total becomes -100 + 250 = 150.

Constraints

  • 1 <= persons.length <= 500
  • properties.length == actions.length == prices.length == persons.length
  • Person names are 1 to 10 lowercase English letters. Property names are 1 to 10 lowercase English letters or digits.
  • Each actions[i] is "buy" or "sell".
  • 1 <= prices[i] <= 10^9
  • A property is only bought when nobody holds it. A property is only sold by the person who holds it, or by anyone when it has no earlier record.

More Kikoff problems

See Kikoff hiring insights
Saved
public long[] realizedGainsAfterEachTransaction(String[] persons, String[] properties, String[] actions, int[] prices) {
    // Write your code here.
}
Checking account…