The High Rollers
A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L4
- Asked in
- BeyondTrust
Problem
Given a list of wager dicts (each with a 'user' key and an 'amount' key) and a threshold (a percentile in the range 0..100), sum each user's total amount. Compute the threshold-th percentile of the per-user totals using linear interpolation between the two closest ranks (the numpy/'linear' convention): sort the totals, let rank = threshold/100 * (n - 1) over the n unique users, and interpolate between sorted_totals[floor(rank)] and sorted_totals[floor(rank)+1] by the fractional part. Return the alphabetically sorted list of usernames whose total is STRICTLY greater than that cutoff. With threshold 0.01 the cutoff sits just above the smallest total, so only users above the very bottom qualify.
Not every gambler bets the same - some wager far more than others.
Example
Input:
bets = [{"user":"alice","amount":500},{"user":"bob","amount":100},{"user":"alice","amount":200},{"user":"charlie","amount":50},{"user":"bob","amount":150}], threshold = 0.01Output:
["alice","bob"]
Worked solution and explanation
Why this problem exists in real interviews
This tests aggregation combined with percentile filtering, a common analytics pattern. It probes whether a candidate can compute per-user totals, determine a percentile threshold with linear interpolation, and filter accordingly.
Break down the requirements
Step 1: Aggregate total spend per user
Sum all wager amounts grouped by each wager's 'user' value.
Step 2: Compute the percentile threshold
Sort the per-user totals, then compute the threshold-th percentile by linear interpolation: rank = threshold/100 * (n - 1), and interpolate between the values at floor(rank) and floor(rank)+1 by the fractional part.
Step 3: Filter users above the threshold
Return usernames whose total is strictly greater than the interpolated cutoff, sorted alphabetically.
The solution
def top_bettors(bets: list[dict], threshold: float) -> list[str]:
totals = {}
for w in bets:
user = w['user']
totals[user] = totals.get(user, 0) + w['amount']
sorted_totals = sorted(totals.values())
n = len(sorted_totals)
if n == 0:
return []
rank = threshold / 100.0 * (n - 1)
lo = int(rank)
hi = min(lo + 1, n - 1)
frac = rank - lo
cutoff = sorted_totals[lo] + frac * (sorted_totals[hi] - sorted_totals[lo])
result = [user for user, total in totals.items() if total > cutoff]
return sorted(result)Common follow-up questions
- What percentile method does numpy use by default? (Tests knowledge of linear interpolation in `np.percentile`.)
- How would you do this in SQL? (Tests `PERCENT_RANK()` or `NTILE()` window functions.)
- What if the percentile changes frequently? (Tests precomputing sorted totals and using binary search for different thresholds.)