Highest Score from Exactly k End Cards

Quick Overview

Highest Score from Exactly k End Cards. Cards lie in a row. Each step removes one card from either end and adds its points to your score.

Highest Score from Exactly k End Cards

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Cards lie in a row. Each step removes one card from either end and adds its points to your score. Take exactly k cards and return the largest obtainable score. ```cpp class Solution { public: int maxScore(vector<int>& cardPoints, int k); }; ``` ### Examples **Example 1:** ```text Input: cardPoints = [5,1,2,8], k = 2 Output: 13 Explanation: Taking the5 from the left and8 from the right beats the two-left score6 and two-right score10. ``` **Example 2:** ```text Input: cardPoints = [3,6], k = 2 Output: 9 Explanation: Every card must be taken. ``` ### Constraints - cardPoints has length 1 to 100000. - `1 <= cardPoints[i] <= 10000`. - `1 <= k <= cardPoints.length`. ### Contract considerations Cards cannot be taken from the middle before reaching them from an end. ```hint Read the boundary condition Use exactly the requested number of end removals. Include choices from both ends and the case in which every card is taken. ``` ### Boundary reminders Cards cannot be taken from the middle before reaching them from an end.

Overview: Highest Score from Exactly k End Cards. Cards lie in a row. Each step removes one card from either end and adds its points to your score.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Cards lie in a row. Each step removes one card from either end and adds its points to your score. Take exactly k cards and return the largest obtainable score.

class Solution {
public:
    int maxScore(vector<int>& cardPoints, int k);
};

Examples

Example 1:

Input: cardPoints = [5,1,2,8], k = 2
Output: 13
Explanation: Taking the5 from the left and8 from the right beats the two-left score6 and two-right score10.

Example 2:

Input: cardPoints = [3,6], k = 2
Output: 9
Explanation: Every card must be taken.

Constraints

  • cardPoints has length 1 to 100000.
  • 1 <= cardPoints[i] <= 10000 .
  • 1 <= k <= cardPoints.length .

Contract considerations

Cards cannot be taken from the middle before reaching them from an end.

Boundary reminders

Cards cannot be taken from the middle before reaching them from an end.