[Solution]Problem 67: Designing a Card Game Class [Facebook/Meta]
Object Oriented Design, Logic, Programming
To learn more about the newsletter, check our detailed About Page + FAQs
To help me understand you better, please fill out this anonymous, 2-min survey. If you liked this post, make sure you hit the heart icon in this email.
Recommend this publication to Substack over here
Take the next step by subscribing here
How did you do yesterday?
This is one of the better Coding Interview Questions asked. No gimmicks, directly related to the kinds of work you’ll do. Working through these questions is a great way to develop your coding ability and establish your ability to build code for the future. Remember, as this Microsoft Study into great software engineers showed, these skills are crucial. Building your solution the right way can be your ticket into an amazing career.
Problem
Assume you’re designing a card game using the traditional 52-card deck. Design a Game
class representing the game and these following functions associated with the class.
add_card(suit, value)
: Creates a new card object with a suit from one of the following strings:Hearts
,Spades
,Clubs
,Diamonds
, and a value from one of the following strings:A
,2
~10
,J
,Q
,K
. This card is represented byi
, wherei
is an integer indicating how many cards have been created before.card_string(card)
: Returns the string representation of the card represented byi
. It follows the format<value> of <suit>
. For example, a card created byadd_card("Spades", "3")
should have a string representation of3 of Spades
.card_beats(card_a, card_b)
: Check if the card represented bycard_a
beats the one represented bycard_b
. A card beats another card if and only if it has a greater value. The value of the cards are ordered fromA
toK
.
You may implement these however you like. However, preferably this should be easily expandable to accommodate new requirements.
This kind of a problem might seem too easy for the coders amongst you, but building things up the right way will go a long way.
Keep reading with a 7-day free trial
Subscribe to Technology Made Simple to keep reading this post and get 7 days of free access to the full post archives.