FastPrepDesign a Leaderboard

Design a Leaderboard

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process operations for a game leaderboard. Scores start empty.

  • ["ADD", playerId, score] adds score to that player's total, creating the player when necessary.
  • ["TOP", k] asks for the sum of the k highest current player totals.
  • ["RESET", playerId] removes that player's current score from the leaderboard.

Return one integer for every TOP operation, in encounter order.

Function

leaderboard(operations: String[][]) → int[]

Examples

Example 1

operations = [["ADD","1","73"],["ADD","2","56"],["ADD","3","39"],["ADD","4","51"],["ADD","5","4"],["TOP","1"],["RESET","1"],["RESET","2"],["ADD","2","51"],["TOP","3"]]return = [73,141]

The first query returns 73. After resets and player 2's new score, the top three are 51, 51, and 39.

Example 2

operations = [["ADD","1","10"],["ADD","2","20"],["ADD","1","15"],["TOP","2"]]return = [45]

Player 1 accumulates 25 and player 2 has 20.

Example 3

operations = [["ADD","7","8"],["TOP","1"],["RESET","7"],["ADD","7","3"],["TOP","1"]]return = [8,3]

RESET removes the old score, so the later ADD starts a new total.

Constraints

  • 1 <= operations.length <= 100000.
  • 1 <= playerId <= 100000.
  • 1 <= score <= 100000.
  • ADD totals and TOP sums fit in signed 32-bit integers.
  • RESET references a player currently on the leaderboard.
  • 1 <= k <= the current number of players for every TOP.
  • At least one TOP operation is present.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] leaderboard(String[][] operations) {
    // Write your code here.
}
operations[["ADD","1","73"],["ADD","2","56"],["ADD","3","39"],["ADD","4","51"],["ADD","5","4"],["TOP","1"],["RESET","1"],["RESET","2"],["ADD","2","51"],["TOP","3"]]
expected[73,141]
Checking account…