Get Max Alternating Music
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's Get Max Alternating Music hit the October assessments, and it's testing whether you can spot a greedy pattern under pressure. You're given a sequence, probably with costs or values, and you need to pick items that alternate in some property while maximizing a score. The trick isn't dynamic programming. It's recognizing that at each step, you have a small set of valid choices, and picking the locally optimal one compounds to a global win. StealthCoder will catch you if you freeze on the state transition logic.
Pattern and pitfall
The pattern here is greedy with a state machine. You're likely tracking the last selection (or its parity) and at each position deciding whether to take the current item based on whether it alternates with what came before. The pitfall is overcomplicating it with DP when a simple state variable (last item taken, or last type/parity) and a forward pass works. Most candidates try to memoize or recurse when they should just iterate, decide, and accumulate. If you blank mid-OA on the recurrence, StealthCoder gives you the state transitions instantly so you can code without the mental loop.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Get Max Alternating Music cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Max Alternating Music FAQ
What does 'alternating' mean in this problem?+
It means consecutive selections must differ in some property, likely index parity (odd-even), value parity, or category. The problem forces you to pick items such that adjacent picks don't share that property. It's a constraint that eliminates most brute-force paths.
Is this a DP problem or greedy?+
Greedy with state tracking. You maintain one or two variables (e.g., max score if last item was odd, max score if last item was even) and iterate once. DP is overkill here and signals you're overthinking.
What's the common mistake candidates make?+
Trying to explore all subsequences instead of using a greedy pass with state. They recurse with memoization when a single forward loop with two accumulators solves it in O(n) time. Recognizing that 'max so far' with alternation constraint needs only constant state is the key.
How do I code this in under 15 minutes?+
Parse the input, identify the alternation rule, set up two variables (e.g., max_ending_odd and max_ending_even), loop once through the array, and update them based on whether the current item alternates. Return the max of your two accumulators.
Will this problem come back on Amazon OAs?+
Yes. Greedy with alternation constraints is a steady pattern in Amazon assessments. The specific framing changes, but the core trick stays the same. Knowing that state machines + greedy beats DP here gives you a big edge.