Reverse even numbers in a list
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Given an integer array `nums`, remove all odd numbers and return the remaining even numbers in reverse order (preserving no other ordering constraints).
Example:
- Input: `nums = [2, 3, 4]`
- Output: `[4, 2]`
Clarifications:
- `0` is even.
- If no even numbers exist, return an empty array.
Quick Answer: This question evaluates competency in basic array manipulation, specifically filtering elements by parity and reversing their order while preserving no other ordering constraints.
Given an integer array `nums`, remove all odd numbers and return the remaining even numbers in **reverse order** of their appearance in the original array.
In other words, scan the array from right to left and collect every even value you encounter.
**Example:**
- Input: `nums = [2, 3, 4]`
- Output: `[4, 2]`
**Clarifications:**
- `0` is even.
- Negative even numbers (e.g. `-2`) count as even.
- If no even numbers exist, return an empty array.
- Duplicate even values are all kept.
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.