As reported: F5 Networks SDE-3 (Bengaluru), 2025, round 1 (coding): rearrange an array into alternating positive and negative numbers, followed by a discussion of optimisations, then OS, Linux and networking questions.
Given an unsorted array arr of N positive and negative numbers, rearrange it in place so that positive and negative numbers alternate, without changing the relative order of the positive numbers among themselves or of the negative numbers among themselves. The array must start with a positive number. 0 counts as a positive number.
If one sign runs out, the remaining numbers of the other sign go at the end, in their original order.
Example 1
arr = [9, 4, -2, -1, 5, 0, -5, -3, 2]
Output: [9, -2, 4, -1, 5, -5, 0, -3, 2]
Positives in order: 9, 4, 5, 0, 2. Negatives in order: -2, -1, -5, -3. They alternate, and the extra positive 2 goes last.
Example 2
arr = [-5, -2, 5, 2, 4, 7, 1, 8, 0, -8]
Output: [5, -5, 2, -2, 4, -8, 7, 1, 8, 0]
After three pairs the negatives run out, so 7, 1, 8, 0 follow in order.
Constraints: 1 <= N <= 10^5, -10^9 <= arr[i] <= 10^9.
Code runner format: one JSON array, arr. Implement rearrange(arr); it returns nothing, and the runner prints arr afterwards.