Maintain a Running Median of Card Amounts to Flag Outliers
Last updated: November 29, 2026
Quick Overview
Process a card's transaction amounts as a stream, maintaining the median of all amounts seen so far in O(log n) per transaction, and flag any amount greater than 10 times the median of the earlier amounts once at least five have been seen.
Capital One
November 29, 20260
0
0 solved
Process a card's transaction amounts as a stream, maintaining the median of all amounts seen so far in O(log n) per transaction, and flag any amount greater than 10 times the median of the earlier amounts once at least five have been seen.
Capital One's real-time fraud features compare each authorization against a customer's typical spend, and the median resists outliers better than the mean. This onsite streaming question checks the two-heap pattern, its balance invariant, and judgment about using the statistic before updating it.
What the Interviewer Expects
- Use a max-heap for the lower half and a min-heap for the upper half
- Keep sizes balanced so the lower half has equal or one more element
- Compute the median from the heap tops in O(1)
- Score each amount against the median before inserting it
- Discuss memory over a long history and windowed alternatives
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you compute the median over only the last 90 days, with expirations?
- Per-customer heaps for 100 million customers do not fit in memory. What approximate structure would you use?
- Why is the median a better baseline than the mean here, and what does it miss?
- How would you handle a customer whose spend legitimately jumps after a move?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
add(x)inserts one amount;median()returns the current median or None if empty.- With an even count the median is the mean of the two middle values.
- Outlier rule: index i is flagged if i >= ...
High-Level Design
Split the stream into a lower half in a max-heap (negated values in heapq) and an upper half in a min-heap, keeping every lower element no larger than every upper element. Insert into the lower half u...