Fewest Gift Card Values to Add So Every Total Is Payable
Last updated: November 29, 2026
Quick Overview
A sorted list of Play Store gift card values is given, and any subset can be combined. Return the minimum number of extra card values to add so every amount from 1 to n can be paid exactly as a subset sum.
0
0
0 solved
A sorted list of Play Store gift card values is given, and any subset can be combined. Return the minimum number of extra card values to add so every amount from 1 to n can be paid exactly as a subset sum.
This patching problem tests whether a Google onsite candidate can see an invariant about reachable sums and exploit it greedily. Interviewers like it because the solution is short but the proof is the real content.
What the Interviewer Expects
- Maintain the invariant that all sums in [1, miss) are payable
- Use the next card if it is at most miss
- Otherwise add a card of value miss
- Explain why adding miss is the best patch
- O(m + log n) time
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
- Return the patched values.
- Cards are not sorted.
- Each value can be used any number of times instead of once.
- n is up to 2^31; check for overflow in fixed-width languages.
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: sorted list of positive ints cards (each usable once) and target n. Output: the minimum number of values to add so every integer in [1, n] is a subset sum....
High-Level Design
Keep miss: the smallest amount we cannot yet pay, with every amount in [1, miss) payable. Start with miss = 1.
If the next card c <= miss, using it extends the range to [1, miss + c), since...