Reveal Cards in Increasing Order
Tags: computerscience, 100daysofcode, java
Published: 2021-01-03 23:19:40

Although I continue my practice of algorithms, I am still having trouble identifying when to use the appropriate data structure. I recently attempted to solve a problem on LeetCode and completely missed the obvious hints that a queue would be a good data structure to use for the algorithm. This post will provide a detailed solution with illustrations for that problem. While the main purpose of this post is to help others get a clear mental picture of what is happening with the algorithm, I also write this post with the hope that I can work to strengthen my intuition of when to leverage the correct data structure.

## The Problem

In a deck of cards, every card has a unique integer. You can order the deck in any order you want.

Initially, all the cards start face down (unrevealed) in one deck.

Now, you 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.

1. If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.

1. 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.**

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

## Example

**Input:** [17, 13, 11, 2, 3, 5, 7]
**Output:** [2, 13, 3, 11, 5, 17, 7]
**Explanation:**

* We get the deck in the order [17,13,11,2,3,5,7] (this order doesn’t matter), and reorder it.

* After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.

* We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].

* We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].

* We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].

* We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].

* We reveal 11, and move 17 to the bottom. The deck is now [13,17].

* We reveal 13, and move 17 to the bottom. The deck is now [17].

* We reveal 17. Since all the cards revealed are in increasing order, the answer is correct.

[Link to Problem](https://leetcode.com/problems/reveal-cards-in-increasing-order/)

## Some Thoughts

Before we get to the optimal solution, I wanted to briefly talk about my attempted solution.

{% gist
https://gist.github.com/djl218/9b0736fb9351fc1ca259ec90e618403a %}

My solution only works for the example test case. I was too focused on trying to transform the provided input into the target output. I did not take enough time to consider all possible test cases, or at the very minimum at least test cases of different lengths.

I think it would have helped if I had slowed down a bit. I should have thought more about the problem and asked myself which data structures would be good candidates. A stack or a queue should have been an obvious choice for a problem that is about a deck of cards.

The optimal solution for this problem uses a queue. If you need a refresher on queues then check out this [post about queues](https://www.danielleskosky.com/stacks-queues/). The solution will also be storing index values inside of the queue. This is a great technique and I definitely want to get better at recognizing when it is appropriate to use. Here is an [algorithm](https://www.danielleskosky.com/daily-temperatures/) that uses that technique except with a stack.

## How to Solve

The solution that will be covered was submitted by LeetCode user @caraxin. Here is the [submission](https://leetcode.com/problems/reveal-cards-in-increasing-order/discuss/200526/Java-Queue-Simulation-Step-by-Step-Explanation). Here are the steps:

1. Sort the deck, it is actually the “final sequence” we want to get according to the question.

1. Then put it back to the result array, we just need to deal with the index now!

1. Simulate the process with a queue (initialized with 0,1,2…(n-1)), now how do we pick the card?

1. We first pick the index at the top: res[q.poll()] = deck[i]

1. Then we put the next index to the bottom: q.add(q.poll())

1. Repeat it n times, and you will have the result array!

## The Code

Here is the code for the @caraxin approach. As you can see, the solution is quite succinct and does an amazing job of utilizing a queue. Super smart!

{% gist
https://gist.github.com/djl218/98cac614a296f110a0a1462328661263 %}

## Some Illustrations

This is a perfect problem to use some illustrations to get a clear mental picture of what is going on.

Figure A just shows the initial setup. The queue is filled with index values 0–6, result is initialized, and i = 0. deck has been sorted.

![Figure A](https://cdn-images-1.medium.com/max/2000/1*_3Nsjnhg7UGoIJsUNIli4A.png)<center>*Figure A*</center>

q.poll() gives a value of 0. This 0 corresponds to the index for result. result[q.poll()] = deck[i] == result[0] = deck[0]. q is polled again and that polled value is added to the queue. This is shown in Figure B.

![Figure B](https://cdn-images-1.medium.com/max/2000/1*0e7_HLOcsKu5xx9ZMSiIVQ.png)<center>*Figure B*</center>

q.poll() gives a value of 2. This 2 corresponds to the index for result. result[q.poll()] = deck[i] == result[2] = deck[1]. q is polled again and that polled value is added to the queue. This is shown in Figure C.

![Figure C](https://cdn-images-1.medium.com/max/2000/1*-r_1ZXHdJU-kD_u2GJvXIg.png)<center>*Figure C*</center>

q.poll() gives a value of 4. This 4 corresponds to the index for result. result[q.poll()] = deck[i] == result[4] = deck[2]. q is polled again and that polled value is added to the queue. This is shown in Figure D.

![Figure D](https://cdn-images-1.medium.com/max/2000/1*azBOHftzGLdBc1VV3kvYmw.png)<center>*Figure D*</center>

q.poll() gives a value of 6. This 6 corresponds to the index for result. result[q.poll()] = deck[i] == result[6] = deck[3]. q is polled again and that polled value is added to the queue. This is shown in Figure E.

![Figure E](https://cdn-images-1.medium.com/max/2000/1*BL8eZ2SYFRVjlZwA_djlgw.png)<center>*Figure E*</center>

q.poll() gives a value of 3. This 3 corresponds to the index for result. result[q.poll()] = deck[i] == result[3] = deck[4]. q is polled again and that polled value is added to the queue. This is shown in Figure F.

![Figure F](https://cdn-images-1.medium.com/max/2000/1*y1WPJYvR30W2_KF1s6V4XA.png)<center>*Figure F*</center>

q.poll() gives a value of 1. This 1 corresponds to the index for result. result[q.poll()] = deck[i] == result[1] = deck[5]. q is polled again and that polled value is added to the queue. This is shown in Figure G.

![Figure G](https://cdn-images-1.medium.com/max/2000/1*rmjh7ynWX96YZy2KGOwq_g.png)<center>*Figure G*</center>

q.poll() gives a value of 5. This 5 corresponds to the index for result. result[q.poll()] = deck[i] == result[5] = deck[6]. There are no additional values to poll from the queue. This is shown in Figure H.

![Figure H](https://cdn-images-1.medium.com/max/2000/1*MtdSF0w93JJi95-XK-wH2w.png)<center>*Figure H*</center>

```code
result: [2, 13, 3, 11, 5, 17, 7]
```

## Time Complexity

The time complexity of this algorithm is O(n * log(n)), where ’n’ is the number of cards in the deck. Let’s go over this a bit.

The array is sorted. Now I think that it depends on the version of Java, but either [Timsort](https://en.wikipedia.org/wiki/Timsort), or [Dual-Pivot QuickSort](https://www.baeldung.com/arrays-sortobject-vs-sortint#:~:text=%2C%2010%2C%2022%5D-,Arrays.,sort%20and%20the%20MergeSort%20algorithms.) is used for Arrays.sort(). This is O(n * log(n)). [This](https://stackoverflow.com/questions/22571586/will-arrays-sort-increase-time-complexity-and-space-time-complexity) might help explain it better too.

Then we need to iterate the length of n to add the index values to the queue. This is O(n). This gives O(2n * log(n)). Then the length of n needs to be iterated for adding the values to result. This is O(n). This gives O(3n * log(n)). The constant is dropped giving O(n * log(n)).

## Space Complexity

The space complexity will be O(n). Let’s break it down like we did for the time complexity.

O(n) is required for the result array. O(n) is required for the queue. O(n) is required for the sorting. This totals to O(3n). The constant is then dropped giving O(n).

If you are looking for more tips and tricks for Big-O then you might find [this post](https://www.danielleskosky.com/big-o/) helpful.

## Get Better at Algorithms!

Algorithms and data structures are pretty tough. They are definitely taking a while for me to get the hang of them. However, there are some great resources out there, and I feel obligated to share some that have been most helpful to me. If I missed any that have been helpful to you, be sure to mention them in the comments.

* **Cracking the Coding Interview** — Great resource. Really gets you in the right mindset for interviews. You can find it [here](https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850).

* **Elements of Programming Interviews** — Another great book. Personally, I like this one more than CTCI, but YMMV. You can find it [here](https://www.amazon.com/Elements-Programming-Interviews-Java-Insiders/dp/1517671272/ref=pd_lpo_14_t_0/134-2745636-3821839?_encoding=UTF8&pd_rd_i=1517671272&pd_rd_r=4eebd030-1368-436b-9bdd-22a403a57eae&pd_rd_w=ku4HH&pd_rd_wg=qY5q5&pf_rd_p=7b36d496-f366-4631-94d3-61b87b52511b&pf_rd_r=Y44KR67YH071M3P9JGSH&psc=1&refRID=Y44KR67YH071M3P9JGSH).

* **Grokking the Coding Interview** — Can’t emphasize this one enough. Haven’t seen it mentioned it too often. Explains patterns that occur in different coding challenge problems. Great at providing a big-picture of all the different algorithm problems you might encounter. Check it out [here](https://www.educative.io/courses/grokking-the-coding-interview).

* **Grokking Dynamic Programming** — Dynamic programming is tough. This course has definitely helped me get a better understanding.

* **Tushar** **Roy** — Tushar really knows his stuff. His dynamic programming playlist is especially good. Check out his awesome [YouTube channel](https://www.youtube.com/user/tusharroy2525/featured).

* **Back To Back SWE** — Great [YouTube Channel](https://www.youtube.com/channel/UCmJz2DV1a3yfgrR7GqRtUUA). Highly recommend.

* **Kevin Naughton Jr.** — Another awesome [YouTube channel](https://www.youtube.com/channel/UCKvwPt6BifPP54yzH99ff1g). Great at going over problems and gives helpful advice.

* **Base CS** — Vaidehi Joshi does a great job of laying out the fundamentals of algorithms and data structures. Check out the blog series [here](https://medium.com/basecs). She also has a [podcast](https://www.codenewbie.org/basecs) that I give two thumbs up.

* **Coding Challenge Website** — There are plenty of different ones to choose from. [HackerRank](https://www.hackerrank.com/), [CodeWars](https://www.codewars.com/), and [Edabit](https://edabit.com/) all seem to be pretty popular. I personally use [LeetCode](https://leetcode.com/). Find the one that works for you!

* **Pramp** — Do mock interviews! The sooner the better! [Pramp](https://www.pramp.com/) has been a huge help to me.

Well, I hope that was useful. Thanks for reading my post and best of luck with your learning about data structures and algorithms!