Print all odd numbers from an integer list
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency with array/list manipulation, parity checking, and handling edge cases such as negative values and integers outside 32-bit ranges.
Constraints
- 0 <= len(nums)
- Integers may be negative.
- Integers may exceed the 32-bit signed range; classification must remain correct.
- Solution must run in linear time over the length of nums.
Examples
Input: ([1, 2, 3, 4, 5],)
Expected Output: [1, 3, 5]
Explanation: Odd numbers 1, 3, 5 kept in their original order; evens 2 and 4 dropped.
Input: ([2, 4, 6, 8],)
Expected Output: []
Explanation: No odd numbers, so the result is empty.
Input: ([],)
Expected Output: []
Explanation: Empty input yields an empty result.
Input: ([-3, -2, -1, 0, 1],)
Expected Output: [-3, -1, 1]
Explanation: Negative odds -3 and -1 are included; 0 and -2 are even.
Input: ([7],)
Expected Output: [7]
Explanation: Single odd element is returned as-is.
Input: ([10000000000000000001, 10000000000000000002],)
Expected Output: [10000000000000000001]
Explanation: Value far beyond 32-bit range: the odd one is kept, demonstrating parity works for large integers.
Input: ([0, -0, 5, -5],)
Expected Output: [5, -5]
Explanation: 0 and -0 are even; the odd values 5 and -5 are kept in order.
Hints
- An integer x is odd exactly when x % 2 != 0. Avoid `x % 2 == 1`, which is false for negative odds in languages where `%` keeps the sign of the dividend.
- Iterate once and collect matching elements; do not sort or use nested loops.
- For languages with fixed-width ints (Java, C++), use a 64-bit type so values beyond the 32-bit range are still tested correctly. Parity (even/odd) does not depend on width, so the same `% 2` check works.