00:00

Problem Statement

Input/Output Examples

Visual Explanation

Each number is tested once with n % 2 == 0. Because elements are appended in the order they are read, both groups keep the relative order of the input.

Approaches

Both approaches keep the original order inside each group. The loop needs one pass; the stream version uses two filters.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of elements. Every element is tested once (twice for the stream version), and the two groups together hold all n elements.

Interview Tips and Recommended Approach

  • Test even, not odd. In Java, -3 % 2 is -1, so n % 2 == 1 misses negative odd numbers; use n % 2 != 0 or n % 2 == 0.
  • Mention the bitwise test. (n & 1) == 0 is also correct for negatives and avoids the division.
  • Ask whether order matters. If it does not, a two-pointer partition can move evens to the front of the same array in O(1) extra space.
  • Know the collectors option: Collectors.partitioningBy() splits a List<Integer> into a Map<Boolean, List<Integer>> in one call.

Recommended Approach: Single Loop With Two Lists

Why: it needs only one pass, keeps the original order of both groups and handles negative numbers by testing for even. Each step is plain and easy to explain.