Order a Card Deck So Reveal-and-Rotate Shows Cards Sorted
Last updated: November 29, 2026
Quick Overview
A deck of distinct cards is revealed by repeatedly taking the top card face up and then moving the next top card to the bottom, until all cards are revealed. Return an ordering of the deck so that the cards are revealed in increasing order.
0
0
0 solved
A deck of distinct cards is revealed by repeatedly taking the top card face up and then moving the next top card to the bottom, until all cards are revealed. Return an ordering of the deck so that the cards are revealed in increasing order.
A Google phone screen problem that is solved by simulating the process on positions instead of values. The interviewer is looking for the insight that the reveal order of positions does not depend on the card values, and a clean deque simulation.
What the Interviewer Expects
- Separate the positional process from the card values
- Simulate reveal-and-rotate on a deque of indices
- Assign sorted values to positions in reveal order
- Alternatively run the process in reverse on values
- State O(n log n) time due to sorting
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Solve it by reversing the process directly on the sorted values.
- What if the rule moves two cards to the bottom after each reveal?
- Given the final ordering, verify it reveals sorted cards in O(n).
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of distinct integers deck. The process: reveal the top card, then if cards remain move the new top card to the bottom; repeat. Output a list (top first) such that the revealed sequence i...
High-Level Design
The positions revealed form a fixed sequence for a given n. Simulate it with q = deque(range(n)): pop the front as the next revealed position, then rotate the new front to the back. The k-th popped ...