Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].
Return the array in the form [x1,y1,x2,y2,...,xn,yn].
Example 1:
Input: nums = [2,5,1,3,4,7], n = 3 Output: [2,3,5,4,1,7] Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
Example 2:
Input: nums = [1,2,3,4,4,3,2,1], n = 4 Output: [1,4,2,3,3,2,4,1]
Example 3:
Input: nums = [1,1,2,2], n = 2 Output: [1,2,1,2]
Constraints:
1 <= n <= 500nums.length == 2n1 <= nums[i] <= 10^3Problem summary: Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,5,1,3,4,7] 3
[1,2,3,4,4,3,2,1] 4
[1,1,2,2] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1470: Shuffle the Array
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[n << 1];
for (int i = 0, j = 0; i < n; ++i) {
ans[j++] = nums[i];
ans[j++] = nums[i + n];
}
return ans;
}
}
// Accepted solution for LeetCode #1470: Shuffle the Array
func shuffle(nums []int, n int) (ans []int) {
for i := 0; i < n; i++ {
ans = append(ans, nums[i])
ans = append(ans, nums[i+n])
}
return
}
# Accepted solution for LeetCode #1470: Shuffle the Array
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
return [x for pair in zip(nums[:n], nums[n:]) for x in pair]
// Accepted solution for LeetCode #1470: Shuffle the Array
impl Solution {
pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
let n = n as usize;
let mut ans = Vec::new();
for i in 0..n {
ans.push(nums[i]);
ans.push(nums[i + n]);
}
ans
}
}
// Accepted solution for LeetCode #1470: Shuffle the Array
function shuffle(nums: number[], n: number): number[] {
const ans: number[] = [];
for (let i = 0; i < n; ++i) {
ans.push(nums[i], nums[i + n]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.