Count Cart Item Pairs Whose Prices Sum to Coupon Value
Last updated: November 29, 2026
Quick Overview
Given an array of cart item prices and a coupon threshold T, count the number of index pairs (i < j) whose prices add up to exactly T. Prices may repeat.
Flipkart
November 29, 20260
0
0 solved
Given an array of cart item prices and a coupon threshold T, count the number of index pairs (i < j) whose prices add up to exactly T. Prices may repeat.
A warm-up in Flipkart's online assessment for SDE-1 candidates, framed around a 'buy any two, get a coupon' offer on the cart page. The grader checks that you move from the O(n^2) double loop to a single hash map pass and handle duplicate prices correctly.
What the Interviewer Expects
- Clarify whether pairs are counted by index or by distinct price values
- Use a hash map of seen price counts in one pass
- Handle duplicates, such as T = 2x with several items priced x
- State O(n) time and O(n) space
- Test with an empty cart and a cart with no valid pairs
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 return distinct value pairs instead of index pairs?
- What if the array is already sorted and you must use O(1) extra space?
- How would you count pairs whose sum is at least T instead of exactly T?
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 of integer prices (paise or rupees), target
T. - Output: number of index pairs
(i, j)withi < jandprices[i] + prices[j] == T. - Duplicates count separately: three items at 50 ...
High-Level Design
Scan left to right. For each price p, the number of valid partners to its left is the number of earlier items priced T - p. Keep a frequency map of prices seen so far, add the complement count to ...