Ctrl K

Two Piles by the Top Card

Easy
You have a row of cards cards, each with a different number. You deal them in order onto two piles, left and right: - the first card goes on the left pile, the second on the right pile; - each following card goes on the left pile if the number on top of the left pile is **greater** than the number on top of the right pile; otherwise it goes on the right pile. Once every card is dealt, list the left pile from bottom to top, followed by the right pile, also from bottom to top. Return that list.
Example 1
cards=[6, 9, 4, 7] → [6, 7, 9, 4]

6 goes to the left pile and 9 to the right. The tops are 6 and 9; the left one is not larger, so 4 goes right. Now the tops are 6 and 4; the left one is larger, so 7 goes left. Left pile [6, 7], right pile [9, 4].

Example 2
cards=[10, 3, 8, 6, 5] → [10, 8, 6, 5, 3]

The left pile's top stays larger than the right pile's top the whole time, so every card after the second goes left: [10, 8, 6, 5] and [3].

Example 3
cards=[1, 2, 3] → [1, 2, 3]
Constraints
  • 3 ≤ len(cards) ≤ 50
  • 1 ≤ cards[i] ≤ 100
  • All numbers in cards are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Long the Light Stayed On EasyRepack the Tray EasyQuiz Score Log EasyMirror and Negate a Bitmap EasySwap Rows and Columns Easy
loading Python…
Input
cards=[6, 9, 4, 7]
Expected
[6, 7, 9, 4]

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