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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

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
queue simulation
deque
permutation
reverse simulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported