Maximum Points You Can Obtain from Cards explained
Maximum Points You Can Obtain from Cards looks like a game of choices, but any set of k picks from the two ends is just some number i of cards from the left plus k - i from the right. Sweep i from 0 to k while swapping one left card for one right card, or equivalently find the cheapest block of n - k cards to leave behind. Either way it is O(k) or O(n) time and O(1) space.
By Evin JohnPublished 7 min read
The problem in plain words
A row of cards each shows a positive number of points. You must take exactly k cards. On each turn you may take the card at the left end or the card at the right end of what remains. Your score is the sum of the cards you took. Return the best possible score.
| Cards | k | Best | Best split |
|---|---|---|---|
[4, 1, 1, 9, 2, 6] | 3 | 17 | Three from the right: 9 + 2 + 6 |
[1, 1, 10, 1, 1] | 2 | 2 | The 10 is out of reach with only two picks |
[3, 8, 2] | 3 | 13 | k equals the row length, so take everything |
[5, 5, 5] | 2 | 10 | Every split ties |
Why interviewers like it
The prompt invites a recursive search over left-or-right decisions, and some candidates write a full dynamic program. The insight the interviewer is waiting for is that the sequence of decisions is irrelevant: only the final split counts. Spotting that reduces a branching search to a short loop, and the second view (minimize what you leave behind) shows you can flip a problem around.
Brute force: try the decisions
Recursively take the left card or the right card until k are taken. Without memoization that is 2^k paths. With memoization on (left index, right index, picks left) it drops to about k² states, which is fine for small k but still more than needed.
from functools import lru_cache
def best_take_slow(cards, k):
# every order of left/right picks, memoized: about k² states
@lru_cache(maxsize=None)
def go(lo, hi, left):
if left == 0:
return 0
return max(cards[lo] + go(lo + 1, hi, left - 1),
cards[hi] + go(lo, hi - 1, left - 1))
return go(0, len(cards) - 1, k)The key fact: only the split matters
However you interleave the picks, you always end with some prefix of the row and some suffix, and their lengths add to k. Taking left, right, left gives the same cards as left, left, right. So list the k + 1 splits: all k from the left, k - 1 from the left and 1 from the right, and so on, down to all k from the right.
Method A: slide the split
Start with all k cards from the left. To move to the next split, give back the innermost left card (index k - i) and take the next card from the right end (index n - i). Each move is one subtraction and one addition, so the sweep costs O(k) after the first sum.
def best_take_split(cards, k):
# take i from the left and k - i from the right
take = sum(cards[:k]) # all k from the left
best = take
for i in range(1, k + 1):
take += cards[-i] - cards[k - i] # swap one left card for one right card
best = max(best, take)
return bestfunction bestTake(cards, k) {
let take = 0;
for (let i = 0; i < k; i++) take += cards[i]; // all k from the left
let best = take;
for (let i = 1; i <= k; i++) {
take += cards[cards.length - i] - cards[k - i]; // trade a left card for a right one
best = Math.max(best, take);
}
return best;
}Method B: minimize what stays behind
The cards you do not take always form one unbroken block of n - k cards somewhere in the middle. Maximizing what you take is the same as minimizing that block's sum. Slide a window of length n - k across the row, track the smallest sum, and subtract it from the total.
def best_take_window(cards, k):
# the cards left behind form one block of n - k; make that block as small as possible
n = len(cards)
keep = n - k
total = sum(cards)
if keep == 0:
return total
window = sum(cards[:keep])
smallest = window
for right in range(keep, n):
window += cards[right] - cards[right - keep]
smallest = min(smallest, window)
return total - smallestMethod A touches only 2k cards, so it wins when k is small compared with n. Method B always walks the whole row but some people find it easier to prove correct. Both are fine answers; mention the other as a follow-up.
Trace on the first example
Cards [4, 1, 1, 9, 2, 6], k = 3. All left: 4 + 1 + 1 = 6. Swap the 1 at index 2 for the 6 at the right end: 11. Swap the 1 at index 1 for the 2: 12. Swap the 4 for the 9: 17. The best is 17. Method B agrees: the cheapest block of three cards to leave is 4 + 1 + 1 = 6, and the total is 23, so 23 - 6 = 17.
Complexity
| Approach | Time | Extra space |
|---|---|---|
| Every left/right sequence | O(2^k) | O(k) recursion |
| Memoized search | O(k²) | O(k²) cache |
| Slide the split | O(k) | O(1) |
| Smallest middle block | O(n) | O(1) |
Edge cases
- k equals n: take every card. Method B must handle a window of length 0, which is why it returns the total early.
- k is 1: compare the two end cards.
- Equal values: many splits tie; any of them is fine since only the score is returned.
- Large values: in languages with fixed-size integers, check that k times the biggest card fits.
Common mistakes
- Greedily taking the bigger end card each turn. On
[5, 1, 1, 1, 9, 2]with k = 2, greedy takes the 5 and then a 2, for 7, while taking both right cards gives 9 + 2 = 11. A big card hidden one step behind a small one is exactly what greedy misses. - Off-by-one in the swap: the left card leaving is at index k - i, the right card joining is at n - i, for i from 1 to k.
- Sliding a window of length k instead of n - k in Method B.
- Writing a full dynamic program and not noticing the split insight when the interviewer hints at it.
How to explain it out loud
Fictional example
- Candidate
- The order of picks doesn't matter. I always end with some prefix and some suffix whose lengths add to k, so there are only k + 1 outcomes.
- Candidate
- I'll start with all k from the left and, k times, swap the innermost left card for the next card on the right, tracking the best sum.
- Candidate
- That's O(k) time and constant space. Another way to see it: the cards left behind are one block of n minus k, so I could minimize that block with a window.
Related problems
Grumpy Bookstore Owner is another fixed-length window where you pick the best place to spend a limited resource. Binary Subarrays With Sum counts windows instead of maximizing one, and Permutation in String slides a fixed window over letters. All are on the coding problems hub.
Where Natively helps in practice
When you rehearse this on a Mac or Windows PC, Natively can take a screenshot of the prompt and explain why the pick order is irrelevant, or compare your split sweep against the middle-window version. You can then scroll back through the transcript of your mock to see whether you stated the k + 1 outcomes before coding. More on the AI interview assistant page.
Questions people ask
How do you solve Maximum Points You Can Obtain from Cards?
Any k picks from the ends equal i cards from the left plus k - i from the right. Start with all k from the left, then k times swap the innermost left card for the next right card, keeping the best total.
Why is Maximum Points You Can Obtain from Cards a sliding window problem?
The cards you leave behind are one contiguous block of n - k. Minimizing that block's sum with a fixed-size window gives the maximum you can take.
Does a greedy approach work for Maximum Points from Cards?
No. Taking the larger end card each turn can miss a big card hidden behind a small one, for example [5, 1, 1, 1, 9, 2] with k = 2.
What is the time complexity of Maximum Points You Can Obtain from Cards?
The split sweep is O(k) time and O(1) space. The middle-window version is O(n) time and O(1) space.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
