Ctrl K

Masha and Pete Stack the Cards

Easy
Numbered cards lie on a table, given as the array cards of even length. Masha and Pete play in rounds. In each round Masha first takes the card with the smallest number, then Pete does the same among the cards that are left. Then both put their cards on a shared pile: Pete first, Masha second. The game goes on while cards remain on the table. Return the numbers on the pile in the order the cards were placed.
Example 1
cards=[7, 3, 9, 1] → [3, 1, 9, 7]

Round one: Masha takes 1, Pete takes 3; the pile gets 3, then 1. Round two: 7 and 9, so the pile gets 9, then 7.

Example 2
cards=[4, 4] → [4, 4]

Both cards are equal, so the swap changes nothing.

Constraints
  • 2 ≤ len(cards) ≤ 100
  • len(cards) is even
  • 1 ≤ cards[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClearing the Stacks Evenly EasyPeel Off the Row Maximums EasyShuttle Seat Plan MediumBusiest Meeting Room HardCross Out the Smallest First Medium
loading Python…
Input
cards=[7, 3, 9, 1]
Expected
[3, 1, 9, 7]

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