Deterministic Zigzag Sort
Updated
Problem statement
Rearrange nums so that a[0] <= a[1] >= a[2] <= a[3] ..., and return the result.
To get one fixed answer, sort the array ascending, then swap each adjacent pair at indices (1,2), (3,4), and so on. Keep every value from the input, including duplicates.
Function
wiggleSort(nums: int[]) → int[]Examples
Example 1
nums = [1,2,3,4,5]return = [1,3,2,5,4]The array is already sorted. Swap the first pair, at positions one and two, and then the next pair, at positions three and four. The result is one, three, two, five, four.
Example 2
nums = [3,1,2]return = [1,3,2]Sorted, the array is one, two, three. Swap the last two to get one, three, two. The three is a peak, bigger than both of its neighbors.
Example 3
nums = [2,2,1,1]return = [1,2,1,2]Sorted, the array is one, one, two, two. Swap the middle pair to get one, two, one, two. Equal neighbors are fine, because the rule allows them.
Constraints
1 <= nums.length <= 100000.-10^9 <= nums[i] <= 10^9.
More SambaNova Systems Onsite Interview questions
- Vectorized K-Means AssignmentsONSITE INTERVIEW · Seen Apr 2024
- 4Sum II Tuple CountONSITE INTERVIEW · Seen Jun 2022
- Bomb Enemy Best CoordinatesONSITE INTERVIEW · Seen Jun 2022
- Sum Nodes with an Even-Valued GrandparentONSITE INTERVIEW · Seen Jun 2022
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jun 2022
- Maximum Area of IslandONSITE INTERVIEW · Seen Jun 2022
- Task Scheduler with CooldownPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Point Inside a TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Parse Variable, Expression, and Application TreesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Flip Equivalent Binary TreesONSITE INTERVIEW · Seen Oct 2021
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Total Years of Work ExperienceLinkedIn · PHONE SCREENLinkedIn interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Assign CookiesAmazon · PHONE SCREEN