Amazon OA SDE 2 discussion
Summary
I was asked the "Maximum Coins From K Consecutive Bags" problem during an online assessment.
Full Experience
Got asked this question but with a different language. 3413. Maximum Coins From K Consecutive Bags
Interview Questions (1)
Maximum Coins From K Consecutive Bags
You are given an array bags of length n where bags[i] represents the number of coins in the i‑th bag. You can select exactly k consecutive bags in one operation and collect all coins from those bags. After each operation, the selected bags become empty (their values become 0). You repeat the operation until all bags are empty. Return the maximum total number of coins you can collect.
Example
Input: bags = [1,2,3,4,5], k = 2
Output: 12
Explanation:
- First select bags[3] and bags[4] (values 4 and 5) -> collect 9, bags become [1,2,3,0,0]
- Then select bags[1] and bags[2] (values 2 and 3) -> collect 5, bags become [1,0,0,0,0]
- Finally select bags[0] and bags[1] (value 1 and 0) -> collect 1, total = 9+5+1 = 15.
Note: The above example is illustrative; actual optimal selections may differ.