Apple Interview Question: How would you implement a dec... |

Interview Question

Intern Interview Houston, TX (US)

How would you implement a deck of cards? How would you

  shuffle it?

Interview Answer

1 Answer


I would create an interface that would set the functionality standards for all Cards. I would then implement all 52 cards as separate classes that implement the interface Card and add their own individual unique functionality.

I would shuffle the cards by also implementing the Comparator or Comparable interface and then adding all cards to a Collection such as an ArrayList and using the shuffle method from the Collection interface.

If they wouldn't allow me to use API's, I would say I would add all the cards into an Array and in a loop generate two random numbers < 52 to use as an index and swap the cards at those indexes. That would be a LogN solution.

Jean on 29 Jan 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.