Quick Overview

Sort a list of two-character playing card codes by suit and then by rank, where the ranks include the letters T, J, Q, K and A. It tests building a composite sort key from custom orderings and spotting where plain string ordering gives the wrong result.

Sort a Deck of Cards by Suit, Then by Rank

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an array of strings `deck`, where each string represents a playing card. Each card is encoded as a 2-character string: the first character is the rank and the second character is the suit. Ranks, in increasing order, are `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T` (10), `J` (Jack), `Q` (Queen), `K` (King), `A` (Ace). Suits, in increasing order, are `C` (Clubs), `D` (Diamonds), `H` (Hearts), `S` (Spades). Return the deck sorted first by suit (Clubs, Diamonds, Hearts, Spades), and then by rank within each suit in increasing order. ### Function Signature ```python def sort_deck(deck: list[str]) -> list[str]: ``` ### Rules - Ace is high: within a suit, `A` comes after `K`. - The deck can hold the same card more than once, since it can be longer than 52 cards. Copies of a card are identical strings and end up next to each other in the output. ### Constraints - `1 <= len(deck) <= 10^4` - Each card is a 2-character string. The first character is a rank from `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T`, `J`, `Q`, `K`, `A`. - The second character is a suit from `C` (clubs), `D` (diamonds), `H` (hearts), `S` (spades). ### Examples ```text Input: deck = ["AS", "2C", "KH", "TD", "5S", "3C", "JH", "9D"] Output: ["2C", "3C", "9D", "TD", "JH", "KH", "5S", "AS"] ``` Grouped by suit in the order C, D, H, S and then by rank, the groups are clubs [2C, 3C], diamonds [9D, TD], hearts [JH, KH] and spades [5S, AS]. ```text Input: deck = ["7H", "7C", "7S", "7D"] Output: ["7C", "7D", "7H", "7S"] ``` All cards have rank 7, so only the suit order C, D, H, S decides the result. ```text Input: deck = ["QH", "AS", "KH", "2S", "TS", "AS"] Output: ["QH", "KH", "2S", "TS", "AS", "AS"] ``` Hearts come before spades. Within hearts the Queen ranks below the King, and within spades the order is 2, T (10), then both copies of the Ace.

Overview: Sort a list of two-character playing card codes by suit and then by rank, where the ranks include the letters T, J, Q, K and A. It tests building a composite sort key from custom orderings and spotting where plain string ordering gives the wrong result.

You are given an array of strings `deck`, where each string represents a playing card. Each card is a 2-character string: the first character is the rank and the second character is the suit. Ranks, in increasing order, are `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T` (10), `J` (Jack), `Q` (Queen), `K` (King), `A` (Ace). Suits, in increasing order, are `C` (Clubs), `D` (Diamonds), `H` (Hearts), `S` (Spades). Implement `sort_deck(deck)`, which returns the cards of `deck` sorted first by suit (Clubs, Diamonds, Hearts, Spades), and then by rank within each suit in increasing order. ### Rules - Ace is high: within a suit, `A` comes after `K`. - The deck can hold the same card more than once, since it can be longer than 52 cards. Copies of a card are identical strings and end up next to each other in the output. - The output holds exactly the cards of `deck`, with every copy kept. ### Examples ```text Input: deck = ["AS", "2C", "KH", "TD", "5S", "3C", "JH", "9D"] Output: ["2C", "3C", "9D", "TD", "JH", "KH", "5S", "AS"] ``` Grouped by suit in the order C, D, H, S and then by rank, the groups are clubs [2C, 3C], diamonds [9D, TD], hearts [JH, KH] and spades [5S, AS]. ```text Input: deck = ["QH", "AS", "KH", "2S", "TS", "AS"] Output: ["QH", "KH", "2S", "TS", "AS", "AS"] ``` Hearts come before spades. Within hearts the Queen ranks below the King, and within spades the order is 2, T (10), then both copies of the Ace. ### Constraints - `1 <= len(deck) <= 10^4` - Each card is a 2-character string. The first character is a rank from `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T`, `J`, `Q`, `K`, `A`. - The second character is a suit from `C` (clubs), `D` (diamonds), `H` (hearts), `S` (spades). - The same card can appear more than once.

Constraints

  • 1 <= len(deck) <= 10^4
  • Each card is a 2-character string. The first character is a rank from 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A.
  • The second character is a suit from C (clubs), D (diamonds), H (hearts), S (spades).
  • The same card can appear more than once.

Examples

Input: (['5H'],)

Expected Output: ['5H']

Explanation: Minimum valid deck: a single card comes back unchanged.

Input: (['AS', '2C', 'KH', 'TD', '5S', '3C', 'JH', '9D'],)

Expected Output: ['2C', '3C', '9D', 'TD', 'JH', 'KH', '5S', 'AS']

Explanation: First source example: two cards per suit, grouped C, D, H, S and ordered by rank inside each group.

Hints

  1. The suit decides the order before the rank does, so a high card in an earlier suit still comes before a low card in a later suit.
  2. Character codes put A, J, K, Q and T in a different order from the card ranks, so compare ranks by their place in the list 2 to A.
  3. Copies of the same card are identical strings, so their order among themselves never affects the answer.

Sort a Deck of Cards by Suit and Rank

easyGeneralSoftware Engineer

You are given an array of strings deck, where each string represents a playing card. Each card is a 2-character string: the first character is the rank and the second character is the suit.

Ranks, in increasing order, are 2, 3, 4, 5, 6, 7, 8, 9, T (10), J (Jack), Q (Queen), K (King), A (Ace). Suits, in increasing order, are C (Clubs), D (Diamonds), H (Hearts), S (Spades).

Implement sort_deck(deck), which returns the cards of deck sorted first by suit (Clubs, Diamonds, Hearts, Spades), and then by rank within each suit in increasing order.

Rules

  • Ace is high: within a suit, A comes after K.
  • The deck can hold the same card more than once, since it can be longer than 52 cards. Copies of a card are identical strings and end up next to each other in the output.
  • The output holds exactly the cards of deck, with every copy kept.

Examples

Input: deck = ["AS", "2C", "KH", "TD", "5S", "3C", "JH", "9D"]
Output: ["2C", "3C", "9D", "TD", "JH", "KH", "5S", "AS"]

Grouped by suit in the order C, D, H, S and then by rank, the groups are clubs [2C, 3C], diamonds [9D, TD], hearts [JH, KH] and spades [5S, AS].

Input: deck = ["QH", "AS", "KH", "2S", "TS", "AS"]
Output: ["QH", "KH", "2S", "TS", "AS", "AS"]

Hearts come before spades. Within hearts the Queen ranks below the King, and within spades the order is 2, T (10), then both copies of the Ace.

Constraints

  • 1 <= len(deck) <= 10^4
  • Each card is a 2-character string. The first character is a rank from 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A.
  • The second character is a suit from C (clubs), D (diamonds), H (hearts), S (spades).
  • The same card can appear more than once.

Examples

Example 1
Input
(['5H'],)
Output
['5H']
Notes
Minimum valid deck: a single card comes back unchanged.
Example 2
Input
(['AS', '2C', 'KH', 'TD', '5S', '3C', 'JH', '9D'],)
Output
['2C', '3C', '9D', 'TD', 'JH', 'KH', '5S', 'AS']
Notes
First source example: two cards per suit, grouped C, D, H, S and ordered by rank inside each group.

Constraints

  • •1 <= len(deck) <= 10^4
  • •Each card is a 2-character string. The first character is a rank from 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A.
  • •The second character is a suit from C (clubs), D (diamonds), H (hearts), S (spades).
  • •The same card can appear more than once.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def sort_deck(deck: list[str]) -> list[str]:
Sample call
sort_deck(['5H'])
Language
Case 1
Input
(['5H'],)
Expected
['5H']
Case 2
Input
(['AS', '2C', 'KH', 'TD', '5S', '3C', 'JH', '9D'],)
Expected
['2C', '3C', '9D', 'TD', 'JH', 'KH', '5S', 'AS']

Browse More Questions

Show the approach

Approach

Give every card a position from 0 to 51: its suit index times 13 plus its rank index, with suits indexed C=0, D=1, H=2, S=3 and ranks indexed 2=0 up to A=12. A smaller position means an earlier place in the output: every club comes before every diamond, every diamond before every heart and every heart before every spade, and inside one suit the cards follow rank order with the Ace last. Sorting the deck by this position therefore produces exactly the required order. Two cards share a position only when they have the same rank and suit, so they are identical strings; the relative order of equal cards cannot change the output, which makes the result unique without needing a stable sort. Comparing the raw rank characters would go wrong because character codes order the high ranks as A, J, K, Q, T. A single card, a deck missing whole suits and many copies of one card all follow from the same rule. Because there are only 52 positions, counting how often each position occurs and writing the cards out in position order would also work in O(n) time.

Time complexity:
O(n log n)
Space complexity:
O(n)