Dynamic Fixed-K Score Leaderboard
Updated
Problem statement
Build a leaderboard that always looks at one fixed rank k, where k is positive. You get three arrays of the same length: operations, userIds, and scores. Handle each index in order:
"add"puts the user on the board with the given score. If the user is already there, the old score is replaced."remove"takes the user off the board. If the user is not on the board, nothing happens."query"asks for thek-th highest score right now. Every user counts as a separate entry, even when two users have the same score.
For a query, ignore the user ID and score at that index. Return one string for each query, in order. The string is the k-th highest score written in decimal (plain digits, with a minus sign if it is negative), or "null" when fewer than k users are on the board.
Function
processLeaderboard(k: int, operations: String[], userIds: String[], scores: int[]) → String[]Examples
Example 1
k = 2operations = ["add","query","add","query","add","query","remove","query"]userIds = ["u1","","u2","","u1","","u2",""]scores = [10,0,5,0,3,0,0,0]return = ["null","5","3","null"]Here the rank is the second-highest score. At the first query only one user is on the board, so the answer is "null". After adding u2, there are two users, and the second-highest score is 5. Then u1 is updated to 3, which puts it below u2, so the second-highest score becomes 3. Finally, removing u2 leaves only one user, so the last answer is "null".
Example 2
k = 2operations = ["add","add","query","remove","query"]userIds = ["a","b","","a",""]scores = [7,7,0,0,0]return = ["7","null"]Users a and b have the same score. They still count as two separate entries, so the second-highest score is 7. After a is removed, only one user is left, so the next answer is "null".
Example 3
k = 1operations = ["remove","query","add","query"]userIds = ["ghost","","n",""]scores = [0,0,-4,0]return = ["null","-4"]Removing ghost does nothing, because that user is not on the board. The board is still empty, so the first query returns "null". Then n is added with a negative score. It is the only user, so it holds the highest score, and the second query returns that negative score. Negative scores are allowed on the board.
Constraints
1 <= k <= 100000.operations.length == userIds.length == scores.length.0 <= operations.length <= 100000.- Every operation is exactly
"add","remove", or"query". - An add or remove user ID is non-empty and contains at most 50 Unicode characters.
-1000000000 <= scores[i] <= 1000000000for add operations.
Similar questions at other companies
- Restaurant WaitlistGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Dynamic Fixed-K Score LeaderboardCoinbase · ONSITE INTERVIEWCoinbase interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spreadsheet Formula DependenciesCitadel · PHONE SCREENCitadel interview questions
- Randomized String MultisetLinkedIn · PHONE SCREENLinkedIn interview questions
- Least Frequently Used CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
- Sparse Vector and Matrix OperationsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Capacity-Limited Timed CacheNetflix · PHONE SCREENNetflix interview questions