Top Two Users by Total Purchase Amount
Updated
Problem 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 Phone Screen questions
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
- Minimum StackPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Values Exclusive to the Current UserMeta · PHONE SCREENMeta interview questions
- Count Values Appearing Exactly OnceAgoda · PHONE SCREENAgoda interview questions
- Restaurant Vote LeadersZipRecruiter · PHONE SCREEN · ONSITE INTERVIEWZipRecruiter interview questions
- AI Role: Collection with Add and Remove OperationsApple · PHONE SCREENApple interview questions
- Employees With Shared InterestsPalantir · PHONE SCREENPalantir interview questions