FastPrepTop Two Users by Total Purchase Amount
Problems/Top Two Users by Total Purchase Amount

Top Two Users by Total Purchase Amount

Snowflake logoSnowflake● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source states the purchase-record input and top-two-by-total objective, but the complete sample, expected result, return shape, tie rule, amount model, bounds, and fewer-than-two-user behavior are cropped or unstated. The judged core task matches the visible source at about 90%.

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 purchases contains 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

  • Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
  • Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
  • Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
All Snowflake interview questionsSee Snowflake hiring insights
Saved
public String[] topTwoUsers(String[][] purchases) {
  // write your code here
}
Checking account…