Reverse even numbers in a list
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates competency in basic array manipulation, specifically filtering elements by parity and reversing their order while preserving no other ordering constraints.
Read the full Upstart Software Engineer interview experience this question came from
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Examples
Input: ([2, 3, 4],)
Expected Output: [4, 2]
Explanation: Scanning right-to-left: 4 (even, keep), 3 (odd, skip), 2 (even, keep) -> [4, 2].
Input: ([],)
Expected Output: []
Explanation: Empty input yields an empty result.
Hints
- Iterate over the array from the last element to the first.
- Keep only the values that are divisible by 2 (use the modulo operator: x % 2 == 0). Remember that 0 and negative numbers can be even too.
- Equivalently, filter the evens first then reverse the result — both give the same order.