Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Create two lists; append each element to the even or odd list based on `x % 2 == 0`. Order is preserved by appending as you scan.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you produce a stable partition and understand the space/order trade-offs.
Where candidates slip
Reversing a group, or the negative-parity bug.
How to stand out
Contrast stable two-list output with the unstable in-place two-pointer swap.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Route into two lists
1Hint 1Explore hintHide hint
Two output buckets — what decides which one an element goes to?
2Hint 2Explore hintHide hint
Appending as you scan preserves each group's order automatically.
3Hint 3Explore hintHide hint
Use `% 2 == 0` for the even bucket.
A · Core idea
Route each element into an even or odd output list based on its parity. Appending in scan order makes the partition stable — each group keeps its original relative order — for free.
Why appending preserves order. You read left to right and append to the back of the matching list, so within each bucket the elements appear in the same sequence they had in the input. No sorting or extra bookkeeping is needed.
Two output lists vs in place. Producing two separate lists is O(n) extra space but simplest. An in-place stable partition is harder; an unstable in-place partition (two-pointer swap) is O(1) space but reorders within groups — mention the trade-off.
Parity robustness. Test % 2 == 0 so negative evens are bucketed correctly.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4]1
odd
even=[], odd=[1][[2, 4], [1, 3]]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(n) space — one parity test and one append per element; two lists hold n elements total, so O(n) space.
E · Edge cases and pitfalls5nums = [2, 4] — All even → [[2, 4], []].Explore edge casesHide edge cases
Edge cases handled
nums = [2, 4]All even → [[2, 4], []].nums = [1]All odd → [[], [1]].nums = []→ [[], []].
Pitfalls
- Inserting at the front and reversing a group's order.
- Parity bug on negative numbers.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic java.util.List<java.util.List<Integer>> separate(int[] nums) { java.util.List<Integer> even = new java.util.ArrayList<>(); java.util.List<Integer> odd = new java.util.ArrayList<>(); for (int x : nums) { if (x % 2 == 0) even.add(x);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Reversing one group by inserting at the front.
- Parity bug on negative numbers.
Revise
Carry the pattern into the interview
Pattern to remember
Stable partition into buckets
Code pattern to memorize
even=[x for even]; odd=[x for odd] (stable by append)Rules to remember
- 1Append in scan order.
- 2Parity picks the bucket.
- 3Order preserved per group.
Use this pattern when
- Splitting into groups while preserving order
Interview takeaway
Appending while you scan preserves relative order automatically.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Stable partition into buckets. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Sort Array By ParityComing soonEvens before odds in one array (order can be relaxed).
- Move Zeros to EndA related stable partition by a predicate.