KatInterview

Daily Spend Ranking for Frequent Payers

SQL & DataAuto-graded · 13 test cases
EasyAuto-gradedSQL & Datawindow-functionsgroupingranking

Last updated

Filter users by total event count, then rank each user's payments within each calendar day by amount using competition ranking (ties share a rank, then a gap).

Daily Spend Ranking for Frequent Payers

A payments log is given as a list of records, one per payment. Each record is a dictionary with these keys:

  • user_id: an integer identifying the user.
  • amount: a number giving the payment value.
  • transaction_time: an ISO-8601 timestamp string such as "2023-01-01T10:00:00".

Implement a function that finds the frequent payers and ranks their payments within each day.

Task

  1. Select frequent payers. A user qualifies only if they have strictly more than 5 payments in the entire input, counted across all days.
  2. Rank within each day. For each qualifying user and each calendar day (the date part of transaction_time), rank that user's payments for that day by amount from highest to lowest. The highest amount gets rank 1.
  3. Ties use competition ranking. Payments with equal amounts on the same user and day share the same rank, and the following rank is skipped. For example, ranks come out as 1, 2, 2, 4, not 1, 2, 2, 3. Each day starts again at rank 1.

Output

Return a list of dictionaries, one for each payment belonging to a qualifying user. Each dictionary has exactly these keys:

  • user_id
  • transaction_time (the original string, unchanged)
  • amount
  • day_rank

Sort the returned list by user_id ascending, then transaction_time ascending, then amount descending.

If no user qualifies, return an empty list. Duplicate records (same user, time and amount) are all kept and receive the same rank.

Constraints

  • The input list may be empty.
  • transaction_time values are valid ISO-8601 strings that can be parsed by datetime.fromisoformat; the day is taken directly from the timestamp, with no time-zone conversion.
  • The eligibility check uses the user's total payment count over all days, not the per-day count.
  • A user with exactly 5 payments does not qualify.
  • Only ranks within the same user and same day are compared; payments of different users or different days never affect each other's rank.

Examples

Example 1

Input: rank_daily_payments([{'user_id': 1, 'amount': 100, 'transaction_time': '2023-01-01T10:00:00'}, {'user_id': 1, 'amount': 200, 'transaction_time': '2023-01-01T11:00:00'}, {'user_id': 1, 'amount': 100, 'transaction_time': '2023-01-01T12:00:00'}, {'user_id': 1, 'amount': 50, 'transaction_time': '2023-01-02T10:00:00'}, {'user_id': 1, 'amount': 60, 'transaction_time': '2023-01-02T11:00:00'}, {'user_id': 1, 'amount': 70, 'transaction_time': '2023-01-02T12:00:00'}])

Output: [{'user_id': 1, 'transaction_time': '2023-01-01T10:00:00', 'amount': 100, 'day_rank': 2}, {'user_id': 1, 'transaction_time': '2023-01-01T11:00:00', 'amount': 200, 'day_rank': 1}, {'user_id': 1, 'transaction_time': '2023-01-01T12:00:00', 'amount': 100, 'day_rank': 2}, {'user_id': 1, 'transaction_time': '2023-01-02T10:00:00', 'amount': 50, 'day_rank': 3}, {'user_id': 1, 'transaction_time': '2023-01-02T11:00:00', 'amount': 60, 'day_rank': 2}, {'user_id': 1, 'transaction_time': '2023-01-02T12:00:00', 'amount': 70, 'day_rank': 1}]

Hints

Hint 1

Do this in two phases: first count payments per user over the entire input to find who qualifies, then handle the ranking only for those users.

Hint 2

Group the qualifying payments by the pair (user_id, calendar date). Parse the timestamp and take its date so that different times on the same day land in the same group.

Hint 3

Sort each group by amount descending. A row's rank is its 1-based position, unless its amount equals the previous row's amount, in which case it reuses the previous rank. Finally sort the output as specified.