Rank the Top K Merchants by Net Card Spend

Last updated: November 29, 2026

Quick Overview

Given a list of card transactions as (merchant, amount in cents), where refunds are negative, return the k merchants with the highest net spend, highest first, breaking ties alphabetically.

Capital One
Coding & Algorithms
Data Scientist
Capital One
November 29, 2026
Data Scientist
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given a list of card transactions as (merchant, amount in cents), where refunds are negative, return the k merchants with the highest net spend, highest first, breaking ties alphabetically.

Spend summaries by merchant power the Capital One app's spending insights and year-end summaries. This short assessment item checks aggregation with a hash map, correct handling of refunds, and knowing when a heap beats a full sort for top-k.

What the Interviewer Expects
  • Aggregate net totals per merchant before ranking
  • Treat refunds as negative amounts in the same total
  • Use a heap of size k or heapq.nsmallest for O(m log k)
  • Apply a deterministic tie-break
  • Handle k larger than the number of merchants
Key Topics to Cover
Hash maps
Heaps
Top-k selection
Aggregation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you keep a live top-k as transactions stream in and totals change?
  • What if the data is spread across 100 files on different machines?
  • Should a merchant whose net spend is zero or negative ever appear in the list?
  • How would you normalize 'AMZN Mktp' and 'Amazon.com' as one merchant first?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: (merchant, amount_cents) pairs; negative values are refunds.
  • Output: up to k (merchant, net_total) tuples sorted by total descending, then name ascending.
  • If k exceeds the number of m...
High-Level Design

Two steps. First, sum amounts into a dictionary keyed by merchant; ranking raw transactions would count one big refund-reversed purchase as large spend. Second, select the top k. Sorting all m merchan...


Submit Your Answer
Markdown supported