Bird
Raised Fist0
Interview Prepsort-and-searchmediumGoogleAmazon

Wiggle Sort II

Choose your preparation mode4 modes available
▶
Steps
setup

Sort the array

The input array is sorted in ascending order to prepare for median finding and rearrangement.

💡 Sorting organizes elements so the median can be easily found and the wiggle pattern can be constructed systematically.
Line:nums.sort()
💡 Sorting is the foundation for the median-based partitioning that follows.
📊
Wiggle Sort II - Watch the Algorithm Execute, Step by Step
Watching each pointer move and swap helps you understand how the algorithm rearranges elements without extra space, which is hard to grasp from code alone.
Step 1/17
·Active fill★Answer cell
compare
1
0
1
1
1
2
4
3
5
4
6
5
compare
1
0
1
1
1
2
4
3
5
4
6
5
Result: 1
compare
i
1
0
1
1
1
2
4
3
5
4
right
6
5
compare
i
1
0
vi
1
1
1
2
4
3
5
4
6
5
move_right
left
1
0
vi
1
1
1
2
4
3
5
4
right
6
5
compare
1
0
i
1
1
1
2
vi
4
3
5
4
6
5
swap
1
0
left
4
1
i
1
2
vi
1
3
5
4
right
6
5
compare
1
0
4
1
i
1
2
1
3
5
4
vi
6
5
swap
1
0
4
1
left
1
2
i
6
3
5
4
vi
1
5
compare
vi
1
0
4
1
1
2
i
6
3
5
4
1
5
move_right
vi
1
0
4
1
left
1
2
6
3
i
5
4
right
1
5
compare
1
0
4
1
vi
1
2
6
3
i
5
4
1
5
move_right
1
0
4
1
vi
1
2
6
3
5
4
right
1
5
compare
1
0
4
1
1
2
6
3
vi
5
4
i
1
5
swap
1
0
4
1
1
2
left
6
3
vi
1
4
right
5
5
compare
1
0
4
1
1
2
left
6
3
1
4
right
5
5
record
1
0
6
1
1
2
5
3
1
4
4
5
Result: ["1","6","1","5","1","4"]

Key Takeaways

✓ Virtual indexing remaps array indices to place larger elements at odd indices and smaller at even indices.

This insight is hard to see from code alone because the index mapping formula is non-intuitive without visualization.

✓ Three pointers (left, i, right) partition the array into elements greater than, equal to, and less than the median in virtual index order.

Seeing pointer movements and swaps clarifies how the algorithm rearranges elements in-place without extra space.

✓ Elements equal to the median are left in the middle partition by simply advancing the i pointer.

This subtlety is easy to miss in code but critical to maintaining the wiggle property and avoiding unnecessary swaps.

Practice

(1/5)
1. Consider the following Python code for generating the largest number from a list of integers. What is the output when the input is [3, 30, 34]?
from functools import cmp_to_key

def largestNumber(nums):
    nums_str = list(map(str, nums))

    def compare(a, b):
        return -1 if a + b > b + a else 1 if a + b < b + a else 0

    nums_str.sort(key=cmp_to_key(compare))
    if nums_str[0] == '0':
        return '0'
    return ''.join(nums_str)

print(largestNumber([3, 30, 34]))
easy
A. '34330'
B. '33034'
C. '33430'
D. '34303'

Solution

  1. Step 1: Convert numbers to strings

    nums_str = ['3', '30', '34']
  2. Step 2: Sort with custom comparator

    Compare pairs: - '3' + '30' = '330' vs '30' + '3' = '303' -> '330' > '303', so '3' before '30' - '34' + '3' = '343' vs '3' + '34' = '334' -> '343' > '334', so '34' before '3' - '34' + '30' = '3430' vs '30' + '34' = '3034' -> '3430' > '3034', so '34' before '30' Sorted order: ['34', '3', '30']
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Concatenation '34330' is largest [OK]
Hint: Compare concatenations a+b and b+a to order strings [OK]
Common Mistakes:
  • Sorting lexicographically or numerically without custom comparator
2. You are given an array of integers and need to rearrange it so that no two adjacent elements are the same. Which approach guarantees an optimal solution for this problem?
easy
A. Count frequencies, then use a max heap to greedily place the most frequent elements first, ensuring spacing.
B. Use backtracking to generate all permutations and check for valid arrangements.
C. Sort the array and then swap adjacent elements to avoid duplicates.
D. Use dynamic programming to track valid sequences avoiding adjacent duplicates.

Solution

  1. Step 1: Understand problem constraints

    The problem requires rearranging elements so no two adjacent are equal, which demands careful placement of frequent elements.
  2. Step 2: Identify approach that handles frequency and spacing

    Using a max heap to always pick the most frequent element available and placing it ensures no two identical elements are adjacent, which brute force or simple sorting cannot guarantee efficiently.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Max heap approach handles frequency and spacing correctly [OK]
Hint: Max heap ensures spacing of frequent elements [OK]
Common Mistakes:
  • Thinking sorting alone avoids adjacent duplicates
  • Assuming DP solves this without frequency tracking
3. Consider the following Python code that sorts a matrix diagonally using min-heaps. What is the final matrix returned when the input is [[3,3,1,1],[2,2,1,2],[1,1,1,2]]?
from typing import List
import collections
import heapq

def diagonalSort(mat: List[List[int]]) -> List[List[int]]:
    m, n = len(mat), len(mat[0])
    diagonals = collections.defaultdict(list)
    for i in range(m):
        for j in range(n):
            heapq.heappush(diagonals[i - j], mat[i][j])
    for i in range(m):
        for j in range(n):
            mat[i][j] = heapq.heappop(diagonals[i - j])
    return mat

matrix = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
print(diagonalSort(matrix))
easy
A. [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
B. [[1,1,1,1],[1,2,2,2],[1,1,3,3]]
C. [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
D. [[1,1,1,1],[1,2,2,2],[1,1,1,2]]

Solution

  1. Step 1: Build min-heaps keyed by diagonal (row - col)

    For each element, push into heap keyed by i - j. For example, diagonal key 0 has elements [3,2,1], which heapify to [1,2,3].
  2. Step 2: Pop from heaps to replace matrix elements

    Traverse matrix again, replacing each element with the smallest from its diagonal heap. This sorts each diagonal ascending.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Diagonal keys and heaps produce sorted diagonals matching [[1,1,1,1],[1,2,2,2],[1,2,3,3]] [OK]
Hint: Heap per diagonal ensures sorted order on replacement [OK]
Common Mistakes:
  • Off-by-one errors in heap keys
  • Mixing up heap push/pop order
  • Incorrectly reconstructing matrix from heaps
4. You are given a sorted array of integers and a quadratic function f(x) = ax² + bx + c. You need to return a sorted array of the values f(x) applied to each element x in the input array. Which approach guarantees an optimal O(n) time solution?
easy
A. Use two pointers at the start and end of the input array and fill the output array from the end or start depending on the sign of a, leveraging the parabola shape.
B. Use dynamic programming to store intermediate results and build the sorted transformed array incrementally.
C. Apply the quadratic function to each element and then sort the resulting array using a standard sorting algorithm.
D. Use a max-heap to insert transformed values and then extract them in sorted order.

Solution

  1. Step 1: Understand the parabola shape

    The quadratic function forms a parabola. If a >= 0, the largest values are at the ends of the input array; if a < 0, the smallest values are at the ends.
  2. Step 2: Use two pointers to fill output

    By comparing transformed values at both ends, we can fill the output array from the appropriate end in O(n) time without sorting.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Two pointers leverage sorted input and parabola shape for linear time [OK]
Hint: Two pointers exploit parabola shape for O(n) [OK]
Common Mistakes:
  • Assuming sorting is always needed after transformation
  • Trying DP without leveraging input order
5. What is the time complexity of the optimal diagonal sorting algorithm that uses a min-heap per diagonal for an m x n matrix, where each diagonal is sorted independently?
medium
A. O(m * n * min(m, n)) because sorting each diagonal requires scanning all elements multiple times.
B. O(m * n * log(m * n)) because all elements are pushed into heaps together.
C. O(m * n) because each element is processed once with constant-time heap operations.
D. O(m * n * log(min(m, n))) because each diagonal has at most min(m, n) elements and heap operations cost log of diagonal length.

Solution

  1. Step 1: Identify diagonal lengths

    Each diagonal has at most min(m, n) elements, so heap operations per element cost O(log(min(m, n))).
  2. Step 2: Calculate total heap operations

    All m*n elements are pushed and popped once, each operation O(log(min(m, n))). Total time is O(m * n * log(min(m, n))).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Heap operations dominate and depend on diagonal length, not total elements [OK]
Hint: Heap ops cost log of diagonal length, not total elements [OK]
Common Mistakes:
  • Assuming log(m*n) instead of log(min(m,n))
  • Ignoring heap operation cost
  • Mistaking linear heap ops for constant time
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard