FastPrepP&L Leaderboard Operations

P&L Leaderboard Operations

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Process a finite sequence of operations on a trader score leaderboard. Each operation is a string array:

  • ["ADD", traderId, scoreDelta] creates the trader with score 0 if needed, then adds the positive delta.
  • ["TOP", k] emits the sum of the k highest current trader scores.
  • ["RESET", traderId] sets that registered trader's score to 0.

Process operations in order and return one long result for every TOP operation, in encounter order.

Function

leaderboardTopSums(operations: String[][]) → long[]

Examples

Example 1

operations = [["ADD","1","73"],["ADD","2","56"],["ADD","3","39"],["TOP","2"],["RESET","1"],["ADD","2","45"],["TOP","2"]]return = [129,140]

The first top-two sum is 73 + 56. After resetting trader 1 and increasing trader 2 to 101, the second sum is 101 + 39 = 140.

Example 2

operations = [["ADD","10","5"],["ADD","10","7"],["TOP","1"],["RESET","10"],["TOP","1"]]return = [12,0]

Repeated ADD operations accumulate. RESET keeps the trader registered with score zero.

Example 3

operations = [["ADD","4","8"],["ADD","2","8"],["ADD","9","3"],["TOP","3"]]return = [19]

The only query sums every registered score: 8 + 8 + 3 = 19.

Constraints

  • 1 <= operations.length <= 100000.
  • traderId is a positive 32-bit integer written in decimal.
  • 1 <= scoreDelta <= 1000000.
  • Every RESET refers to a registered trader.
  • Every TOP uses 1 <= k <= the number of registered traders.
  • At least one TOP operation is present.
  • Every emitted sum fits in a signed 64-bit integer.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public long[] leaderboardTopSums(String[][] operations) {
    // Write your code here.
}
operations[["ADD","1","73"],["ADD","2","56"],["ADD","3","39"],["TOP","2"],["RESET","1"],["ADD","2","45"],["TOP","2"]]
expected[129,140]
Checking account…