Wiggle Sort
A medium-tier problem at 68% community acceptance, tagged with Array, Greedy, Sorting. Reported in interviews at Myntra and 1 others.
Wiggle Sort is the kind of array problem that sounds simple until you realize the naive approach tanks your runtime. You're asked to rearrange an array so that it alternates between peaks and valleys: nums[0] < nums[1] > nums[2] < nums[3] and so on. Google and Myntra both ask this. The trap is thinking you need to sort or search exhaustively. The real solution is a single greedy pass that swaps elements in place. With a 68% acceptance rate, most people who see this problem solve it, but plenty spend too long hunting for an elegant algorithm that's already there.
Companies that ask "Wiggle Sort"
Wiggle Sort is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe key insight: you don't need to sort. You just need to ensure each element satisfies the wiggle condition relative to its neighbors. Iterate left to right. At each even index, check if the current element is smaller than the next. At each odd index, check if it's larger than the next. Whenever the condition fails, swap. One pass does it. The greedy property holds because swapping a pair to satisfy a local wiggle condition never breaks previously satisfied pairs. The obvious pitfall is overcomplicating it with sorting or dynamic programming, which eats time unnecessarily. If you blank on this during the live OA, StealthCoder surfaces the greedy swap pattern in seconds and runs it invisible to the proctor.
Pattern tags
You know the problem.
Make sure you actually pass it.
Wiggle Sort recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Wiggle Sort interview FAQ
Do I really not need to sort?+
Correct. Sorting is O(n log n) overkill. The greedy swap approach is O(n) and guarantees a valid wiggle arrangement on the first pass. Sorting creates a sorted array, but that's not the goal here. The goal is any valid wiggle, and greedy achieves it in linear time.
What happens if I swap at an even index? Won't that break the previous odd index?+
No. When you swap nums[i] and nums[i+1] at an even index i, you're fixing the peak-valley relationship at that boundary. The element that was at nums[i+1] now sits at nums[i], and the old nums[i] moves right. The condition at i-1 (if it exists) remains satisfied because you haven't touched nums[i-1].
Is Wiggle Sort still asked at Google and Myntra?+
Yes. Both companies report asking it. It's a classic medium-difficulty array problem used to test whether you can spot a greedy solution under time pressure. It's not trendy, but it's reliable in hiring loops.
What's the difference between Wiggle Sort and Wiggle Subsequence?+
Wiggle Sort rearranges the entire array in place. Wiggle Subsequence finds the longest subsequence that wiggles, which involves dynamic programming or greedy counting. Different problems, different patterns.
Can I solve this with a different condition at each index?+
Yes. Some solutions separate even and odd indices into two passes. The idea is identical: ensure evens are valleys and odds are peaks (or vice versa). Single-pass swap is more elegant and avoids extra loops, but both work.
Want the actual problem statement? View "Wiggle Sort" on LeetCode →