As reported: Goldman Sachs Associate Superday, DSA round 1 (Transaction Banking, 2025), second question: given an array and k, take values only from the front or the back, k times, and output the maximum sum (solved fully).
In one step you take one card from the beginning or the end of the row cardPoints. You must take exactly k cards. Return the maximum total points.
This is LeetCode 1423 — Maximum Points You Can Obtain from Cards.
Example 1
cardPoints = [1,2,3,4,5,6,1], k = 3
-> 12
Example 2
cardPoints = [2,2,2], k = 2
-> 4
Example 3
cardPoints = [9,7,7,9,7,7,9], k = 7
-> 55
Constraints: 1 ≤ cardPoints.length ≤ 10^5; 1 ≤ cardPoints[i] ≤ 10^4; 1 ≤ k ≤ cardPoints.length.
Code runner format: one JSON value per line: cardPoints, then k (LeetCode's test-case format). Implement maxScore(cardPoints, k); the harness prints the result as JSON.