Top Two Users by Total Purchase Amount
Learn this problemProblem statement
You are given a finite batch of purchase records in purchases. Each row is [user, amount], where user is a non-empty case-sensitive printable ASCII identifier and amount is a non-negative decimal integer stored as a string.
Sum all purchase amounts for each distinct user. Rank users by total purchase amount in descending order. If two users have the same total, rank the lexicographically smaller user identifier first.
Return a String[] containing only the first two ranked user identifiers. If fewer than two distinct users appear, return every distinct user in ranked order. If purchases is empty, return an empty array.
Function
topTwoUsers(purchases: String[][]) → String[]Examples
Example 1
purchases = [["maya","80"],["li","120"],["maya","70"],["zoe","130"],["li","20"]]return = ["maya","li"]maya totals 150, li totals 140, and zoe totals 130. Therefore the first two ranked users are maya and li.
Example 2
purchases = [["zoe","40"],["amy","10"],["bob","40"],["amy","30"]]return = ["amy","bob"]All three users total 40. Lexicographic tie-breaking orders them as amy, bob, then zoe, so the first two are returned.
Example 3
purchases = [["solo","0"],["solo","25"]]return = ["solo"]There is only one distinct user, whose total is 25, so the result contains that user only.
Example 4
purchases = []return = []No purchase record contributes a user, so the result is empty.
Constraints
0 <= purchases.length <= 2 * 10^5- Every row in
purchasescontains exactly two strings:[user, amount]. 1 <= user.length <= 40, and every user identifier contains only printable ASCII characters.0 <= amount <= 10^9, written as a base-10 integer without surrounding whitespace.- Every per-user total fits in a signed 64-bit integer.
More Snowflake problems
- Closest Target CharacterPHONE SCREEN · Seen Jul 2026
- Horizontal Pod AutoscalerSeen Jul 2026
- Minimum HeightOA · Seen Jul 2026
- Vowel SubstringSeen Jun 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Jun 2026
- Efficient DeploymentsOA · Seen Jun 2026
- Character Frequencies Across Nested String ListsPHONE SCREEN · Seen Jun 2026
- Character Frequencies Across StringsPHONE SCREEN · Seen Jun 2026