← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE coding round, one algorithmic problem about subsequences. Pretty straightforward premise but the even-sum constraint had me second-guessing my approach more than I expected.

Questions Asked (1)

Q1

Given a positive integer array, find the longest subsequence where the sum of all elements is even. Implement a solution and provide relevant test cases.

Algorithms & Data Structures
Author's notes

My first instinct was to just take the whole array if its sum is already even, which works.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: a subsequence preserves order but can skip elements, and we want the longest one with even sum. The key insight is that if the total sum of the array is even, the entire array is the answer; if odd, we must remove exactly one odd element (the smallest odd) to make the sum even, so the answer is the array minus that element. Implement by computing total sum and finding the smallest odd element if needed.

Pro tip: Always discuss edge cases like all elements even (answer is whole array) or no odd elements when sum is odd (impossible, so return empty). Also, mention that the solution is O(n) time and O(1) space, which is optimal.

1. Clarify the problem

Confirm that a subsequence maintains relative order but can skip elements, and we need the longest such subsequence with even sum. Ask if the array can be empty or if there are constraints on values.

2. Identify the key observation

If the total sum of all elements is even, the entire array is the longest subsequence. If the sum is odd, removing exactly one odd element (the smallest odd) yields an even sum, and this is optimal because removing any even element would keep the sum odd.

3. Design the algorithm

Compute the total sum and track the smallest odd element. If sum is even, return the whole array. If sum is odd and an odd element exists, return the array without that smallest odd element. If no odd element exists (impossible for odd sum), return empty.

4. Implement and test

Write code that iterates once to compute sum and find min odd. Then construct the result by skipping that element if needed. Test with cases: all even, all odd, mixed, single element, empty array.

5. Analyze complexity

State that the solution runs in O(n) time and O(1) extra space (excluding output), which is optimal since we must examine each element at least once.

Key Points to Mention

  • Subsequence vs substring: subsequence preserves order but can skip elements.
  • Parity of sum: even sum requires an even number of odd elements.
  • If total sum is even, the whole array is the answer.
  • If total sum is odd, remove the smallest odd element to maximize length.
  • Edge cases: all even elements, all odd elements, single element, empty array.
  • Time and space complexity: O(n) time, O(1) extra space.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.