Calculate Spend Percentage of High-Volume Spenders
Arrays & HashingAuto-graded · 21 test casesLast updated
Compute the share of 2023 total expenditure contributed by users who met a high-tier spending threshold in 2022.
You are analyzing ad expenditure data across different calendar years. You are given a list of transactions, where each entry is represented as [user_id, spend, transaction_date].
user_idis an integer identifying the user.spendis a numeric value (integer or float) indicating the transaction amount.transaction_dateis a string formatted as"YYYY-MM-DD"(or a standard date representation).
A user is designated as a "Whale" if their cumulative expenditure across all transactions in the calendar year 2022 is $10,000 or more.
Your goal is to determine what percentage of the total expenditure in 2023 (across all users) was generated by these 2022 Whales.
Specifically, calculate: $$\text{Percentage} = \left( \frac{\text{Total 2023 spend by 2022 Whales}}{\text{Total 2023 spend by all users}} \right) \times 100$$
Specifications & Edge Cases
- The threshold filter of $10,000 applies to the sum of all transactions by a user in 2022, not to individual purchases.
- The denominator includes all spend in 2023 from all users, whether they were 2022 Whales, non-Whales, or new users in 2023.
- If total expenditure in 2023 across all users is
0(or if there are no 2023 transactions), return0.0. - Transactions occurring in years other than 2022 and 2023 should be ignored.
- Return an exact
floatrepresenting the percentage on a scale of0.0to100.0.
Constraints
0 <= len(rows) <= 10^5- For each row:
user_idis a positive integer,spendis a non-negative float/int, andtransaction_dateis a valid date string or date object.
Examples
Example 1
Input: whale_spend_share([[1, 12000, '2022-05-10'], [2, 500, '2022-06-15'], [1, 2000, '2023-01-20'], [3, 5000, '2023-02-10']])
Output: 28.57142857142857
Example 2
Input: whale_spend_share([[1, 6000, '2022-01-01'], [1, 4000, '2022-12-31'], [1, 2500, '2023-03-01'], [2, 7500, '2023-04-01']])
Output: 25
Hints
Hint 1
Aggregate the total spend per user specifically for transactions occurring in the year 2022.
Hint 2
Identify all user_ids whose 2022 cumulative spend reaches or exceeds 10,000.
Hint 3
Sum the 2023 spend for these high-value users, then divide by the 2023 spend of all users combined, multiplied by 100.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires a two-step aggregation across two distinct calendar years:
- Cohort Identification (2022): Group transactions occurring in 2022 by
user_idand compute each user's total spend. Any user whose aggregated 2022 spend is $\ge 10,000$ belongs to the "Whale" cohort. - Measurement & Ratio Calculation (2023):
- Sum the 2023 spend for every user to get the overall 2023 spend (
total_2023). - Sum the 2023 spend specifically for users who were identified as 2022 Whales (
whale_2023_spend). - Compute
(whale_2023_spend / total_2023) * 100.0. Iftotal_2023is zero, return0.0to avoid division by zero.
- Sum the 2023 spend for every user to get the overall 2023 spend (
Implementation Details
We can achieve this in a single pass through the input records:
- Maintain hash maps (
defaultdict) forspend_2022andspend_2023. - Parse each date record (handling both string formats like
"YYYY-MM-DD"and Pythondateobjects) to determine the year. - Record amounts into the appropriate year map and track the running total for 2023.
- Filter 2023 users whose 2022 spend $\ge 10,000$ and compute the resulting percentage.
Complexity Analysis
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the number of transaction rows. We iterate through the rows once and then iterate over distinct 2023 users (which is at most $N$).
- Space Complexity: $\mathcal{O}(U)$, where $U$ is the number of distinct users appearing in 2022 and 2023 transactions stored in hash maps.
Python Reference Solution
from collections import defaultdict
from datetime import date
from typing import List
def whale_spend_share(rows: List[List]) -> float:
spend_2022 = defaultdict(float)
spend_2023 = defaultdict(float)
total_2023 = 0.0
for user_id, spend, transaction_date in rows:
if isinstance(transaction_date, str):
transaction_date = date.fromisoformat(transaction_date)
year = transaction_date.year
if year == 2022:
spend_2022[user_id] += spend
elif year == 2023:
spend_2023[user_id] += spend
total_2023 += spend
if total_2023 == 0.0:
return 0.0
whale_2023_spend = sum(
amount for user_id, amount in spend_2023.items()
if spend_2022[user_id] >= 10000.0
)
return (whale_2023_spend / total_2023) * 100.0