Alternating Parity Permutations
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The example list for n = 10 starts 1 2 3 4 5 6 7 8 9 10, then jumps to 1 2 3 4 5 6 7 10 9 8. That's the whole problem in miniature. Goldman Sachs reported this OA in July 2026, and it's a backtracking question dressed up as a fuzzer for an e-commerce company. You build permutations of 1..n where neighbors never share parity, and output them in lexicographic order. If you freeze on the ordering or the pruning, StealthCoder is the safety net that runs invisibly during the live assessment and hands you the solution.
The problem
An e-commerce company is writing a fuzzer to test a piece of its software. They need a list of numbers with alternating parity, that is, one of them is odd and the other one is even. An alternating parity permutation is a permutation in which any two adjacent elements have different parity. For example, [1,2,3,4], [1], and [3,2,1] exhibit alternating parity, while [1,3,2] and [4,2,1,3] do not. For a given integer n, return all alternating parity permutations of the first n positive integers in lexicographically ascending order. Each permutation contains n elements. For example, the first four permutations where n = 10 are: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 10 9 8 1 2 3 4 5 6 9 8 7 10 1 2 3 4 5 6 9 10 7 8 Function alternatingParityPermutations(n: int) → int[][] Examples Example 1 n = 4 return = [ [1, 2, 3, 4], [1, 4, 3, 2], [2, 1, 4, 3], [2, 3, 4, 1], [3, 2, 1, 4], [3, 4, 1, 2], [4, 1, 2, 3], [4, 3, 2, 1] ] The values of the array are from 1 to n. The following are alternating parity permutations of the first 4 positive integers, sorted. Any other permutation will result in adjacent elements sharing the same parity.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to prune while you build, not filter after. Run DFS over positions 0..n-1. Try values 1 to n in ascending order, skip used ones, and skip any value whose parity matches the previous element. Trying values ascending gives lexicographic output for free, so no sort is needed. That's the pitfall people hit: generating all n! permutations and then checking parity, which dies fast. Another one is copying the path array wrongly when you store a result. Push a copy. The answer size is also huge. For even n it's roughly 2 * (n/2)!^2, so the output dominates the runtime and the backtracking cost is basically proportional to it. Use a boolean used array and a running path. If you blank on the recursion shape during the live OA, StealthCoder is the hedge that gives you a clean working version.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Alternating Parity Permutations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Alternating Parity Permutations FAQ
What's the trick to Alternating Parity Permutations?+
Backtrack with pruning. At each position, only try unused numbers whose parity differs from the previous element. Iterate candidates from 1 to n in order and the results come out already lexicographically sorted. Don't generate all permutations and filter them.
How hard is this one really?+
Medium. It's a standard permutation backtracking problem with one extra condition. If you've written the usual permutations solution with a used array, you add a single parity check. The hard part is staying calm and copying the path correctly.
Do I need to sort the output?+
No. If you loop candidates from 1 up to n at every depth, the DFS visits permutations in lexicographic order naturally. Sorting afterward is wasted work and can hide ordering bugs.
What's the time complexity?+
It's proportional to the number of valid permutations times n, since each result is copied out. For even n the count is about 2 * ((n/2)!)^2, and for odd n the count is smaller. Pruning keeps dead branches from exploding.
How do I prepare in 48 hours?+
Write plain permutations with a used array from memory, then add the parity check. Test n = 1, 2, 3 and 4 against the sample. For odd n, check that the sequence has to start and end with odd numbers. Then do one more backtracking problem to stay warm.