Asked at 6 companies
3 recent
- Morgan Stanley
- Amazon
- Microsoft
- Bloomberg
- Meta
Community-reported · source · updated 2026-10-08. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven 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]. In plain words: interleave the first half and the second half, one element from each in turn.ReadHide
Problem Statement
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]. In plain words: interleave the first half and the second half, one element from each in turn.
Added 2026-10-30 from Srujan's LeetCode solved list (photo) — it was open as a browser tab in the photo but not previously in the repo.
Input / Output
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]
Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code
Stuck? One hint at a time.
Approach
Pattern Recognition
Interleave by Index — the answer has a fixed slot for every input element. The i-th element of the first half (x, at index i) goes to slot 2*i; the i-th element of the second half (y, at index n + i) goes to slot 2*i + 1. Once you see the slot formula, there is nothing left to search for.
Approach
Brute Force — Split, Then Zip, O(n)
xs = nums[:n] # [x1, x2, ..., xn]
ys = nums[n:] # [y1, y2, ..., yn]
ans = []
for x, y in zip(xs, ys):
ans.append(x)
ans.append(y)
Optimized — One Pass, Direct Slot Mapping, O(n)
ans = [0] * (2 * n)
for i in range(n):
ans[2 * i] = nums[i] # x_i
ans[2 * i + 1] = nums[i + n] # y_i
Same O(n) time, but no sliced copies of the two halves and no zip — each input element is written straight to its final slot.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Split, then zip | O(n) | O(n) | Two extra half-sized copies |
| Direct slots | O(n) | O(n) | Output array is required |
Solution code
from typing import List
class SolutionBruteForce:
"""O(n) time | O(n) space — split into x-half and y-half, then zip them."""
def shuffle(self, nums: List[int], n: int) -> List[int]:
xs = nums[:n]
ys = nums[n:]
ans: List[int] = []
for x, y in zip(xs, ys):
ans.append(x)
ans.append(y)
return ans
class Solution:
"""O(n) time | O(n) space — one pass writes each element to its final slot."""
def shuffle(self, nums: List[int], n: int) -> List[int]:
ans = [0] * (2 * n)
for i in range(n):
ans[2 * i] = nums[i] # x_i
ans[2 * i + 1] = nums[i + n] # y_i
return ansOther ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language