Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Blackjack in Python Classes

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Blackjack in Python Classes. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Apple Arcade needs a deterministic blackjack engine for testing game logic. Implement a class-based simulation that deals cards, processes the player's predefined actions, and applies standard dealer behavior.

Use classes such as Card, Hand, and BlackjackGame. The function must consume cards from the supplied deck in order, so the result is deterministic.

Rules

  1. Deal two cards to the player and two cards to the dealer in alternating order: player, dealer, player, dealer.
  2. Card values are 2 through 10, J, Q, K, and A. Face cards count as 10, and an ace counts as 11 when possible, otherwise 1.
  3. Process each player action in order. An action is either "hit" or "stand".
  4. If the player busts, the dealer takes no additional cards and the dealer wins.
  5. Otherwise, the dealer hits while the hand score is below 17 and stands at 17 or higher.
  6. A natural blackjack is an initial two-card score of 21. A player natural beats every non-natural hand, while two naturals produce a push.
  7. Return a dictionary containing player_score, dealer_score, and outcome, where outcome is "player_win", "dealer_win", or "push".

Formal Specification

Implement blackjack_game(deck, actions), where deck is a non-empty list of rank strings and actions is a list of player action strings. Return the result dictionary described above.

Constraints

  • 4 <= len(deck) <= 52
  • Every rank is one of 2 through 10, J, Q, K, or A
  • Actions contain only "hit" and "stand"
  • The deck contains enough cards for all requested hits and dealer draws
  • The player stands or busts before actions are exhausted

Function Signature

def blackjack_game(deck, actions):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output