Best Two-Item Combo That Fits a Gift Card Balance

Last updated: November 29, 2026

Quick Overview

Given menu prices in cents and a gift card balance, return the indices of two different items whose combined price is as large as possible without exceeding the balance, or None if no pair fits.

DoorDash
Coding & Algorithms
Software Engineer
DoorDash
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given menu prices in cents and a gift card balance, return the indices of two different items whose combined price is as large as possible without exceeding the balance, or None if no pair fits.

A short two-pointer exercise in the style of DoorDash screens, set in the checkout flow where a customer wants to spend a gift card fully. The interviewer is looking for the sort plus two pointers pattern, careful index bookkeeping after sorting, and a fast early exit on an exact match.

What the Interviewer Expects
  • Explain why brute force O(n^2) is the baseline
  • Sort indices by price so original positions are preserved
  • Move the right pointer when over budget, the left when under
  • Exit early on an exact match
  • State the O(n log n) cost of sorting
Key Topics to Cover
two pointers
sorting
budget constraints
index mapping
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
  • What if the customer can buy up to three items?
  • What if each item also has a quantity limit of more than one?
  • How would you answer many balance queries against the same menu?
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: prices (list of positive ints, cents) and balance.
  • Pick two distinct items (distinct indices; equal prices are fine).
  • Maximize the sum subject to sum <= balance. Return original...
High-Level Design

Sort the indices by price. Put lo at the cheapest and hi at the most expensive. If the pair is over budget, the expensive item cannot pair with anything cheaper either, so drop it (hi -= 1). If ...


Submit Your Answer
Markdown supported