Swap Adjacent Inversions in One Pass
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this ZipRecruiter OA, reported October 2023, is reading the original array instead of the updated one. The task is one bubble sort pass, nothing more. You walk left to right, compare values[i] with values[i+1], swap if the left one is bigger, and keep going on the mutated array. That's the whole problem. It looks trivial, which is exactly why people rush it and lose points on edge cases. If you blank on the details during the live assessment, StealthCoder is the invisible safety net that reads the problem and hands you a clean solution.
The problem
Make exactly one left-to-right pass over a copy of values. At index i, if the current value is greater than the current value at i + 1, swap that adjacent pair. Then continue to index i + 1 using the updated array. Return the array after the pass. Function swapAdjacentInversions(values: int[]) → int[] Examples Example 1 values = [4,3,2,1] return = [3,2,1,4] The large first value bubbles right through the entire pass. Example 2 values = [1,2,3] return = [1,2,3] No adjacent inversion changes the array. Constraints 0 <= values.length <= 100000 -1000000000 <= values[i] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there is no trick. It's a simulation. Copy the array, loop i from 0 to n-2, and if arr[i] > arr[i+1], swap. Because you continue with the updated array, the largest value seen so far gets carried right, which is why [4,3,2,1] becomes [3,2,1,4]. The pitfall is comparing against the original input, or swapping and then skipping an index. Don't do either. Also copy the input so you don't mutate the caller's array. Handle length 0 and 1 by letting the loop simply not run. Time is O(n), space is O(n) for the copy, and n up to 100000 is no problem. Values go up to a billion in magnitude, but you only compare them, so overflow isn't a concern. If you freeze on the semantics during the real OA, StealthCoder is the hedge that gives you the loop instantly.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Swap Adjacent Inversions in One Pass 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Swap Adjacent Inversions in One Pass FAQ
How hard is Swap Adjacent Inversions in One Pass really?+
Easy. It's a single loop with a conditional swap. The difficulty is purely in reading carefully: you compare against the updated array, not the original. If you code exactly what the statement says, it passes. Most lost points come from rushing, not from algorithm complexity.
What's the trick to getting it right?+
Mutate a copy in place as you go. At each index i, compare arr[i] with arr[i+1] using the current state, swap if needed, then move to i+1. The swapped larger value carries forward, which is why 4 travels all the way to the end in example 1.
Is this just bubble sort?+
It's exactly one pass of bubble sort. You don't repeat until sorted. One pass moves the biggest running value toward the right, but the array usually isn't fully sorted afterward. Don't add an outer loop, or you'll return the wrong answer.
What edge cases should I test before submitting?+
Test an empty array, a single element, an already sorted array like [1,2,3], a strictly descending one like [4,3,2,1], and duplicates. Equal neighbors shouldn't swap since the condition is strictly greater than. Negative values and large magnitudes behave the same.
How do I prepare for this in 48 hours?+
Write it from scratch twice in your language of choice, then trace [4,3,2,1] by hand. Spend the rest of your time on other array simulation problems where state changes mid-loop. This question rewards careful reading more than memorized patterns.