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
Coding & Algorithms
Software Engineer
Flipkart
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

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
hash map
complement counting
duplicates
two sum variant
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 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 Problems
Sample Answer
Requirements
  • Input: list of integer prices (paise or rupees), target T.
  • Output: number of index pairs (i, j) with i < j and prices[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 ...


Submit Your Answer
Markdown supported