Write better code: Day 2 - Single Riffle Shuffle

Updated on Jan 31, 2019 惻1 min read

This was the previous question I solved today.

Day 2: Question 2

Write a method to tell us if a full deck of cards shuffled_deck is a single riffle of two other halves half1 and half2.

We'll represent a stack of cards as an array of integers in the range 1..52 (since there are 52 distinct cards in a deck).


Question from InterviewCake

If you want to follow along, feel free to post your answers in the comment.
My answers are in the comments.


Logic was to get the shuffled deck and check if the last card in shuffled deck belongs to the top of half 1 of half 2.

-do while loop until shuffled deck is empty.
-pick a card from the shuffled deck - check if it's the top card of half 1 or half2.
-if it matches top of half1 - pop it from half1, pop from deck.
-else if is top of half2, repeat.
-if the card does not match the top of either halves, it's not a single shuffle.


I think I could improve this. I am not sure yet what the complexity of using pop is.
If its always O[1] then this logic is good. But anyhow since the max deck of cards is always 52, change in performance wont make much difference even with better code than this.

Classic DEV Post from Sep 18 '18

Software People ā€” How Do You Deal With Stress/Bullies/Frustration?

Dealing with stress is difficult. Having more answers to this question from many different can help many people.

Iā€™m a software developer primarily building web apps using Ruby on Rails.

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ā¤ļø

(There is also a pink mode)