Pick Three Cart Items Priced Closest to a Gift Card Balance

Last updated: November 29, 2026

Quick Overview

Given a list of item prices and a gift card balance, choose three distinct items whose combined price is as close as possible to the balance and return that combined price.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of item prices and a gift card balance, choose three distinct items whose combined price is as close as possible to the balance and return that combined price.

An Amazon technical screen question themed on suggesting a bundle that uses up a gift card. The interviewer wants sort plus two pointers in O(n^2), an early exit on an exact match, and a clear argument for why moving the pointers never skips the best pair.

What the Interviewer Expects
  • Sort prices first and fix the smallest item
  • Run two pointers on the remainder
  • Move the pointer that brings the sum toward the target
  • Return immediately on an exact match
  • Explain why O(n^2) beats the O(n^3) brute force
Key Topics to Cover
Two Pointers
Sorting
3Sum Variant
Arrays
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
  • Return the indices of the three items in the original list.
  • What if the total must not exceed the balance?
  • Generalize to k items; what is the complexity?
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 prices with at least three integers and an integer balance. Output: the sum of three distinct-index items minimizing abs(sum - balance). Ties can return either....

High-Level Design

Sort the prices. For each index i as the first item, place lo = i + 1 and hi = n - 1. Compute the triple sum and keep it if it is closer than the best so far. If the sum is below the balance, incr...


Submit Your Answer
Markdown supported