Maximum Points You Can Obtain from Cards
A medium Sliding Window interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Sliding Window
- Sheets
- 1
- Core for
- 5 roles
The problem
Given an array of card values and a number k, pick exactly k cards from either end of the array to maximize the total score.
Example 1
- Input
- cardPoints = [1,2,3,4,5,6,1], k = 3
- Output
- 12
- Why
- Pick the three rightmost cards [5,6,1] for a total of 12.
Example 2
- Input
- cardPoints = [2,2,2], k = 2
- Output
- 4
- Why
- Pick any two cards to get 2+2=4.
Constraints
- 1 <= cardPoints.length <= 10^5
- 1 <= cardPoints[i] <= 10^4
- 1 <= k <= cardPoints.length
How to think about it
Updated 2026-09-09Taking k cards from the outer ends leaves a contiguous block of exactly n - k cards untouched in the middle. Maximizing the score of the outer cards is identical to minimizing the sum of this unpicked middle window, turning a split-boundary search into a standard fixed-size sliding window.
Approaches, worst first
Recursive exploration
time O(2^k) · space O(k)
At each step, branch on whether to take a card from the left or right end. Explores exponentially many paths with vast overlapping subproblems.
Prefix and suffix sum combinations
time O(k) · space O(n)
Compute prefix sums and suffix sums, then evaluate taking i cards from the prefix and k - i cards from the suffix for all i from 0 to k.
Minimize remaining window sumWrite this one
time O(n) · space O(1)
Slide a window of size n - k across the array to find the minimum possible subarray sum. Subtract that minimum from the total array sum in one pass with constant auxiliary memory.
Where people lose marks · 3
- When k equals the array length, the unpicked window size is 0; attempting to slide a size-zero window causes index errors unless guarded.
- Greedily picking the larger available end at each step fails because taking a smaller card can expose a much larger card behind it.
- Recomputing the window sum from scratch on each shift instead of adding the incoming card and subtracting the outgoing card degrades runtime.
The theory behind it
Sliding Window — the ground this problem stands on. All Sliding Window problems
What Sliding Window is
A sliding window is an adjustable magnifying lens placed over a continuous segment of a sequence. Rather than recalculating metrics for every potential subsection from scratch, the window expands rightward by absorbing fresh elements and contracts leftward to expel stale entries. Only data currently framed within the window borders contributes to the active calculation.
When to reach for it
Reach for a sliding window when a question asks for the longest, shortest, or optimal contiguous subarray or substring matching a constraint. Key signals include fixed window sizes like maximum sum across k consecutive values, or dynamic criteria like finding the shortest substring holding all target characters. If the target subset must form an unbroken continuous run, window mechanics replace repetitive segment rescanning.
How the pattern works
Maintain two boundary indices, left and right, defining the active interval alongside a running state accumulator. In each step, expand the right boundary to incorporate the incoming element into state totals. When current state violates the designated problem constraints, increment the left boundary while deducting departing values until validity is restored. Update your tracking metric, whether minimum window length or maximum score, only during valid intervals.
More Sliding Window problems
Target Roles
Core requirement for 5 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .