Reveal Cards In Increasing Order
Core idea: The reveal rule (take top, then move the next card to the bottom) is fixed and deterministic. Don't guess the starting order β let an empty-slot queue deal the already-sorted cards into their final homes, replaying the exact rule. One sort + one queue pass gives the arrangement.
1. The problem, in a fresh setting
You're building a deterministic dealer for an online card game. The client reveals a deck by a fixed ritual: reveal the top card, then move the new top card to the bottom, repeat until the deck is empty. You want the revealed cards to come out in strictly increasing order β so you must hand the client the initial stacking that makes the ritual produce a sorted reveal.
Given a deck of distinct integers, return an ordering of them so that applying the reveal ritual yields them in increasing order.
deck (any order) |
One valid initial stacking (topβbottom) | Reveal ritual yields |
|---|---|---|
[8, 2, 5, 1, 3] |
[1, 8, 2, 5, 3] |
1, 2, 3, 5, 8 β |
[1, 2] |
[1, 2] |
1, 2 β |
[7] |
[7] |
7 β |
The values are whatever you're given; what you control is their order in the returned deck.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson