Ctrl K

Riffle the Two Halves

Easy
The deck deck holds 2 · half cards. Split it into two halves: the first half cards and the last half cards. Riffle them together: the first card of the first half, then the first card of the second half, then the second card of the first half, the second card of the second half, and so on. Return the resulting order of cards.
Example 1
deck=[1, 2, 3, 10, 20, 30], half=3 → [1, 10, 2, 20, 3, 30]

The first half is 1, 2, 3 and the second is 10, 20, 30. Taking one from each in turn gives 1, 10, 2, 20, 3, 30.

Example 2
deck=[7, 7], half=1 → [7, 7]

Each half holds a single card, so the order stays the same.

Constraints
  • 1 ≤ half ≤ 500
  • len(deck) = 2 · half
  • 0 ≤ deck[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
deck=[1, 2, 3, 10, 20, 30], half=3
Expected
[1, 10, 2, 20, 3, 30]

Run checks the first 3 tests in your browser. Submit checks all 52 tests on the server, hidden ones included.