Google VO Interview Question: Valid Playing Card Set

19 Views
No Comments

Consider a deck of normal playing cards.

A playing card is represented as a string, where the first part represents the rank (one of Ace A, 2, 3, …, 10, Jack J, Queen Q, King K) and the second part represents the suit (one of Clubs C, Diamonds D, Hearts H, Spades S).

A valid set of playing cards is a set of playing cards that fulfills one of the two following criteria:

Criterion 1: the set has 3 or more cards and all cards in the set have the same rank and any suit. Examples:

  • "AC", "AD", "AS" (i.e. Ace of Clubs, Diamonds and Spades)
  • "5C", "5D", "5H", "5S" (i.e. Five of Clubs, Diamonds, Spades and Hearts)

Criterion 2: the set has 3 or more cards and all cards in the set have the same suit and consecutive ascending ranks. Examples:

  • "2C", "3C", "4C" (i.e. 2, 3 and 4 of Clubs)
  • "9H", "10H", "JH", "QH", "KH" (i.e. 9, 10, Jack, Queen and King of Hearts)

Write a function that takes as input a list of cards, and determines if the input is a valid playing card set.

This problem asks you to validate whether a list of playing cards forms a legal set under one of two rules: either all cards share the same rank, or all cards share the same suit and form a consecutive increasing sequence. A practical solution is to parse each card into rank and suit, then check these two conditions separately using counting, set membership, and rank ordering.

END
 0