FastPrepMutable Stock Price Top K

Mutable Stock Price Top K

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Maintain the current integer price of each stock while processing a finite sequence of operations.

  • ["update", symbol, price] creates the stock if necessary and replaces its current price.
  • ["topK", k] returns up to k current stock symbols, ordered by descending price and then lexicographically by symbol when prices are equal.

If fewer than k stocks exist, return every current stock. Return one symbol list for each topK operation, in encounter order.

Function

trackTopStocks(operations: String[][]) → String[][]

Examples

Example 1

operations = [["update","AAPL","150"],["update","MSFT","310"],["update","GOOG","310"],["topK","2"],["update","AAPL","400"],["topK","3"]]return = [["GOOG","MSFT"],["AAPL","GOOG","MSFT"]]

The first query breaks the price tie between GOOG and MSFT lexicographically. After AAPL is updated to 400, it ranks first.

Example 2

operations = [["update","IBM","100"],["topK","3"],["update","IBM","90"],["topK","1"]]return = [["IBM"],["IBM"]]

The first query returns every current stock because only one exists. Updating IBM replaces its old price rather than adding a second stock entry.

Constraints

  • 1 <= operations.length <= 100000.
  • Every operation is either update or topK.
  • symbol contains between 1 and 10 uppercase English letters.
  • 1 <= price <= 1000000000.
  • 1 <= k <= 100000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[][] trackTopStocks(String[][] operations) {
    // Write your code here.
}
operations[["update","AAPL","150"],["update","MSFT","310"],["update","GOOG","310"],["topK","2"],["update","AAPL","400"],["topK","3"]]
expected[["GOOG", "MSFT"], ["AAPL", "GOOG", "MSFT"]]
Checking account…