Reported June 2022
SambaNova Systemssorting

Deterministic Zigzag Sort

Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live SambaNova Systems OA. Under 2s to a working solution.
Founder's read

SambaNova Systems reported this one in June 2022, and the input size tells you what they want. With up to 100000 elements, you can't try permutations until one zigzags. The problem is Deterministic Zigzag Sort, and the trick is sitting in the statement: sort ascending, then swap pairs at indices (1,2), (3,4), and so on. It's a sorting problem with a tiny post-processing step. If you've got the OA in a day or two, this is a ten-minute solve once you see it. StealthCoder is the safety net if your mind goes blank mid-assessment.

The problem

Return a deterministic rearrangement of nums that satisfies a[0] <= a[1] >= a[2] <= a[3]....
For a unique answer, sort ascending and swap each adjacent pair at indices (1,2), (3,4), and so on. Preserve every input occurrence.

Function
wiggleSort(nums: int[]) → int[]

Examples
Example 1
nums = [1,2,3,4,5]
return = [1,3,2,5,4]
Swap the two alternating adjacent pairs.
Example 2
nums = [3,1,2]
return = [1,3,2]
Sorting then one swap forms a peak at index one.
Example 3
nums = [2,2,1,1]
return = [1,2,1,2]
Duplicates still satisfy non-strict inequalities.

Constraints
1 <= nums.length <= 100000.
-10^9 <= nums[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The algorithm is two steps. Sort nums ascending in O(n log n). Then loop i from 1 while i+1 < n, swap nums[i] and nums[i+1], and step i by 2. That gives a[0] <= a[1] >= a[2] <= a[3] and so on. Check Example 1: [1,2,3,4,5] becomes [1,3,2,5,4]. Good. The pitfall is reaching for the O(n) greedy wiggle sort, which gives a valid zigzag but not the unique answer the grader expects. Another trap is the loop bound on odd and even lengths, so test n=1, n=2, and n=3. Duplicates are fine since the inequalities are non-strict. Example 3, [2,2,1,1], sorts to [1,1,2,2], and swapping indices 1 and 2 gives [1,2,1,2]. Return a new or modified array, whichever the signature wants. If you freeze on the swap indices during the live OA, StealthCoder can supply the loop as a hedge.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Deterministic Zigzag Sort 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Deterministic Zigzag Sort FAQ

How hard is Deterministic Zigzag Sort really?+

Easy once you read the statement closely. It tells you the algorithm: sort ascending, then swap adjacent pairs starting at index 1. The only real work is writing the loop bounds correctly and handling tiny arrays. Most of the difficulty is trusting that it's this simple.

What's the trick to getting the unique answer?+

Don't use the O(n) greedy wiggle approach. It produces a valid zigzag but a different array than the grader expects. Sort first, then swap pairs (1,2), (3,4), and so on. The sort makes the output deterministic, which is the whole point of the problem.

What time complexity should I aim for?+

O(n log n) from the sort, with O(1) extra work for the swaps. With n up to 100000, that's comfortably fast. Brute force over permutations is out of the question, and nothing here needs more than a built-in sort plus one linear pass.

What edge cases should I test?+

Test length 1, length 2, and length 3, since the swap loop bound matters most there. Also test all-equal arrays and duplicates like [2,2,1,1], which should give [1,2,1,2]. Negative values up to -10^9 are fine because you're only comparing and swapping.

How do I prepare for this in 48 hours?+

Write it from scratch twice and run the three given examples. Then try odd and even lengths by hand. Also glance at the standard wiggle sort variants so you recognize the difference between a valid zigzag and this fixed, sorted-then-swapped output.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with SambaNova Systems.

OA at SambaNova Systems?
Invisible during screen share
Get it