Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Best 5-Card Hand From N Cards

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

Your question is Best 5-Card Hand From N Cards. 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

A Terra workflow validation utility receives a collection of unique playing cards and must identify the strongest possible five-card poker hand. Implement a function that returns the selected cards' original indices in ascending order.

Formal Specification

Each card is a two-character string: a rank from 2 through 9, T, J, Q, K, or A, followed by a suit from C, D, H, or S. The input contains at least five cards and may be in any order.

Return the indices of the best five-card hand. Rank hands using standard poker categories, from strongest to weakest: straight flush, four of a kind, full house, flush, straight, three of a kind, two pair, one pair, and high card. Resolve ties using the standard rank tie-breakers. If multiple selections have exactly the same poker value, return the lexicographically smallest sorted index list.

Constraints

  • 5 <= len(cards) <= 25
  • Each card is a two-character string with a valid rank and suit
  • All cards are unique
  • Aces can be high or low only when forming a straight
  • Return exactly five indices in ascending order

Function Signature

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