Max Even Sum Subsequence
Reported by candidates from Mathwork's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a Mathworks OA in your inbox and one of the problems is about finding a max even sum subsequence. This is a dynamic programming problem hiding in plain sight. The trick isn't complex, but it's easy to miss under time pressure. You need to track two states as you iterate: the maximum even sum you can build, and the maximum odd sum you can build. Every number you see either stays in your current sums or gets added to flip parity. StealthCoder can feed you the state machine if you blank on the approach during the real assessment.
Pattern and pitfall
The pattern here is dynamic programming with state tracking. At each element, you maintain two values: max_even (best even sum so far) and max_odd (best odd sum so far). For each new number, you either skip it or add it to one of your existing sums. If you add an even number to an even sum, you stay even. If you add an even number to an odd sum, you stay odd. If you add an odd number to an even sum, you flip to odd. If you add an odd number to an odd sum, you flip to even. The final answer is max_even. Common mistake: initializing wrong or not handling the case where no elements are selected (sum of zero is even). When you're live in the OA, this state transition is your safety net to keep moving.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Max Even Sum Subsequence 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Mathwork's OA.
Mathwork reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Max Even Sum Subsequence FAQ
Is this really just DP or am I missing a math trick?+
It's DP with parity tracking. The math insight is that addition preserves parity rules (even + even = even, odd + odd = even, etc.). Once you map those rules to state transitions, the algorithm is straightforward iteration and comparison. No fancy number theory needed.
What if all numbers are odd or all are even?+
If all are odd, you pick an even count of them to get an even sum. If all are even, sum any subset and you're fine. Your DP handles both: max_even will be the sum of zero (0) or the best even combination you can make from whatever array you get.
Do I have to use a subsequence or can it be any subset?+
Subsequence and subset mean the same thing in this context (order preserved, but not all elements required). Your DP doesn't care about order, only whether you pick an element or not. The state update works the same way.
What's the time complexity I should aim for?+
Linear time, one pass through the array. Space is O(1) because you only track two variables (max_even and max_odd). Avoid building a table unless you really need to debug. Just update as you go.
How do I know if my answer is correct on the spot?+
Sanity check: if the array is empty or all negative, the answer should be 0 (sum of zero elements). If you have at least one even number or an even count of odd numbers, you can build a positive even sum. Test on a small example (2-3 numbers) in your head before submitting.