Count Item Price Pairs That Fill Whole Gift Card Units
Last updated: November 29, 2026
Quick Overview
Given a list of item prices in cents and a gift card unit size m (for example 500 cents), count the pairs (i, j) with i < j whose combined price is an exact multiple of m.
Amazon
November 29, 20260
0
0 solved
Given a list of item prices in cents and a gift card unit size m (for example 500 cents), count the pairs (i, j) with i < j whose combined price is an exact multiple of m.
A typical easy question in the Amazon online assessment, phrased around bundling items to fully spend gift card balances. The grader checks for the remainder-counting hash approach rather than an O(n^2) double loop, plus correct handling of the zero remainder and the m/2 remainder.
What the Interviewer Expects
- Reduce each price modulo m
- Count complement remainders seen so far in one pass
- Explain the special cases of remainder 0 and m/2
- Avoid double counting by only pairing with earlier items
- State O(n) time and O(m) space
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
- Return the pairs themselves rather than a count; what is the output size risk?
- What if you need triples that sum to a multiple of m?
- How would you answer this for a stream of prices with a running count?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list prices of non-negative integers, integer m >= 1. Output: number of index pairs i < j with (prices[i] + prices[j]) % m == 0....
High-Level Design
Two prices pair up exactly when their remainders sum to 0 or m, that is, when r2 == (m - r1) % m. Walk the list once; for each price, add the number of earlier items with the complementary remainder...