Reveal Cards In Increasing Order - Problem

You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

You will do the following steps repeatedly until all cards are revealed:

  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. If there are still cards in the deck, then put the next top card of the deck at the bottom of the deck.
  3. If there are still unrevealed cards, go back to step 1. Otherwise, stop.

Return an ordering of the deck that would reveal the cards in increasing order.

Note that the first entry in the answer is considered to be the top of the deck.

Input & Output

Example 1 — Basic Case
$ Input: deck = [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
💡 Note: When we follow the reveal process: reveal 2, move 13 to bottom → reveal 3, move 11 to bottom → reveal 5, move 17 to bottom → reveal 7, move 13 to bottom → reveal 11, move 17 to bottom → reveal 13 → reveal 17. The revealed order is [2,3,5,7,11,13,17] which is increasing.
Example 2 — Small Deck
$ Input: deck = [1,2,3,4]
Output: [1,3,2,4]
💡 Note: Reveal process: reveal 1, move 3 to bottom → reveal 2, move 4 to bottom → reveal 3, move 4 to bottom → reveal 4. Result: [1,2,3,4] in order.
Example 3 — Two Cards
$ Input: deck = [1,2]
Output: [1,2]
💡 Note: With two cards: reveal 1, move 2 to bottom → reveal 2. Result: [1,2] which is already in order.

Constraints

  • 1 ≤ deck.length ≤ 1000
  • 1 ≤ deck[i] ≤ 106
  • All the values of deck are unique.

Visualization

Tap to expand
Reveal Cards In Increasing Order INPUT Original Deck (unsorted) 17 13 11 2 3 5 7 Sorted: 2,3,5,7,11,13,17 deck = [17,13,11,2,3,5,7] ALGORITHM STEPS Reverse Simulation 1 Sort deck descending [17,13,11,7,5,3,2] 2 Init empty queue queue = [] 3 For each card: Move back to front, add card 4 Queue is result Return queue as array Simulation Trace: +17: [17] +13: [13,17]-->[17,13] +11: [11,17,13]-->[13,11,17] +7: [7,13,11,17] +5: [5,17,7,13,11] +3: [3,11,5,17,7,13] +2: [2,13,3,11,5,17,7] FINAL RESULT Ordered Deck (top to bottom) 2 --> 1st (2) 13 skip 3 --> 2nd (3) 11 skip 5 --> 3rd (5) 17 skip 7 --> 4th (7) Reveals: 2,3,5,7,11,13,17 OK - Increasing Order! [2,13,3,11,5,17,7] Key Insight: Simulate the reveal process in REVERSE! Start with largest card, then for each smaller card: 1) Move the last element to the front (reverse of "put top at bottom") 2) Insert the current card at front. This ensures cards reveal in sorted order. Time: O(n log n) TutorialsPoint - Reveal Cards In Increasing Order | Reverse Simulation with Queue
Asked in
Google 15 Amazon 12 Microsoft 8
89.6K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen