Code Interview – Deck of Cards

Each code interview post starts with some tips to help get you through coding interviews. Each writer has had their own experience interviewing code candidates; I will provide my experience and things I’ve noticed from good and not-so-good interviews.

Do

  • Ask the recruiter if the topic of the coding interview is known. Knowing if the coding interview will be related to structures, algorithms, general knowledge, or other topics helps mentally prepare you and focus your study material.
  • Practice coding as often as possible. Websites such as HackerRank, CodeWars, and Excercism.io are free resources at your disposal.
  • Use comments, practice OOP, and be ready to explain your reasoning.
  • Communicate with your interviewer. We evaluate you on how well you communicate and work as a team when overcoming an unfamiliar problem.

Don’t

  • Struggle without asking for help
  • lose your cool
  • Use an inappropriate programming language. If you are being asked to do node trees and you choose bash (this happened to me), then you might have a tough time!
  • Know the programming language. Don’t switch to the one you just started learning. A candidate was asked to traverse a node tree, and they tried Javascript. They had a working knowledge of interacting with page elements but were unfamiliar with JS’s true power.

The Code Question

Create a standard deck of 52 cards. Shuffle the cards and deal them between two people.

The Answer

import random

cardValue = ["A", "K","Q","J","10","9","8","7","6","5","4","3","2"]
cardSuit = ["H","D","C","S"]
deck = []
person1 = []
person2 = []
for v in cardValue:
    for s in cardSuit:
        deck.append(v+s)

random.shuffle(deck)

for i in range(len(deck)-1):
    person1.append(deck[i])
    i+=1
    person2.append(deck[i])

print(person1)
print(person2)

Discussion

For this question, I went with Python. I went with Python because I knew the random library had a shuffle function. One important aspect of a coding interview is to stick with what you know. The interview is not the time or place to try new tactics. The answer is straightforward. We have a list of card values and suits; we create a card for every value and assign it a suit, also of each type. The result is stored in an array of 52 cards.

The toughest part about this application is how to distribute the cards. One way I have seen candidates try is to use the built-in slice feature of Python. They accomplish this by doing person1=deck[:26] and person2=deck[26:]. This assigns the first half of the deck to one and the last half to two. The problem is that this shows a lack of logic. As interviewers, we want to see how you problem-solve in more in-depth ways; that is why the for loop here is used.

The loop just uses a range minus 1; remember that the index starts at 0. Once we append the index result to person 1, we increase the index and request the new result to append to person 2. When we increase the value of i, it gets reassessed at the beginning of the loop by Python. Once you append everything, we just need to print!

The last thing I would like to cover here is the common mistakes with this problem. I covered one as trying just to split the deck in half and give one to each player. The second mistake has the index out of range. Remember, it is important to note that the index starts at 0. Lastly, we do not normally accept answers that create the deck manually; do you really want to write 52 elements in one array?

Leave a Comment