Magicsheet logo

Reveal Cards In Increasing Order

Medium
37.5%
Updated 8/1/2025

Reveal Cards In Increasing Order

What is this problem about?

The Reveal Cards In Increasing Order interview question gives you an integer array representing a deck of cards. You simulate a reveal process: deal the top card (reveal it), move the next top card to the bottom of the deck, and repeat until all cards are revealed. Given this process, find the initial arrangement of the deck such that the cards are revealed in increasing order.

Why is this asked in interviews?

This problem is asked at Apple, Microsoft, Amazon, Google, and Bloomberg because it requires reverse engineering a simulation — working backwards from the desired output. It tests understanding of queue operations (the reveal-and-requeue process) and simulation-in-reverse thinking. It is an excellent example of how sorting combined with queue simulation can replace complex mathematical derivation.

Algorithmic pattern used

The pattern is simulation in reverse with a queue. Sort the deck in decreasing order. Maintain a queue of indices [0, 1, 2, ..., n-1] (representing positions in the result array). For each card (from largest to smallest): assign the card to the front index of the queue, pop the front, and then move the new front to the back (simulating the reverse of the "move to bottom" step). Fill the result array at each popped index.

Alternatively, simulate forward: use a queue of indices, sort the card values, and assign them in order to the positions produced by the simulation.

Example explanation

Deck: [17, 13, 11, 2, 3, 5, 7]. Sorted ascending: [2, 3, 5, 7, 11, 13, 17].

Queue of indices: [0,1,2,3,4,5,6].

Assign cards in order:

  • Assign 2 to index 0, move index 1 to back. Queue: [2,3,4,5,6,1].
  • Assign 3 to index 2, move index 3 to back. Queue: [4,5,6,1,3].
  • Assign 5 to index 4, move index 5 to back. Queue: [6,1,3,5].
  • Assign 7 to index 6, move index 1 to back. Queue: [3,5,1].
  • Assign 11 to index 3, move index 5 to back. Queue: [1,5].
  • Assign 13 to index 1, move index 5 to back. Queue: [5].
  • Assign 17 to index 5.

Result at indices 0–6: [2,13,3,11,5,17,7].

Common mistakes candidates make

  • Simulating forward without using a queue of indices, directly placing cards and getting confused.
  • Not sorting the input first — the reveal order is increasing, so sort the values ascending.
  • Confusing the "move to bottom" step — only ONE card is moved per reveal cycle.
  • Not using a deque for O(1) front-pop operations.

Interview preparation tip

For the Reveal Cards In Increasing Order coding problem, the array, sorting, queue, and simulation interview pattern is the approach. Use Python's collections.deque for efficient front-pop operations. Interviewers at Apple and Google appreciate when you explain the simulation clearly — "sort values, simulate the index queue" — before coding. Practice tracing through a 4-card example manually to solidify the queue mechanics.

Similar Questions