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
November 29, 20260
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
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
- 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 ProblemsSample Answer
Requirements
- Input:
prices(list of positive ints, cents) andbalance. - 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 ...