Detect a Straight Flush
Updated
Problem statement
You are given an array cards of playing cards from a standard 52-card deck. Each card is a string: its suit letter followed by its rank. For example, "CA" is the ace of clubs, "H10" is the 10 of hearts, "SK" is the king of spades and "D5" is the 5 of diamonds.
- Suits:
S(spades),H(hearts),C(clubs) andD(diamonds). - Ranks, from low to high:
A,2,3,4,5,6,7,8,9,10,J,Q,K.
Return true if you can pick 5 of the cards that form a straight flush, and false otherwise. Five cards form a straight flush when both of these hold:
- All five cards have the same suit.
- Their ranks are five consecutive ranks.
The ace can be used as the lowest rank or as the highest rank, so A-2-3-4-5 and 10-J-Q-K-A are both straights. A straight cannot wrap around through the ace: Q-K-A-2-3 is not a straight.
Function
hasStraightFlush(cards: String[]) → booleanExamples
Example 1
cards = ["S9","HK","HQ","C10","HA","HJ","H10"]return = true- The hearts are
HK,HQ,HA,HJandH10. - Their ranks are 10, J, Q, K and A, and the ace may count as the highest rank, so these five hearts form a straight flush.
Example 2
cards = ["SQ","SK","SA","S2","S3","D4"]return = false- The five spades have ranks Q, K, A, 2 and 3.
- A straight cannot wrap around past the ace, so Q-K-A-2-3 is not a straight. No other suit has five cards, so the answer is
false.
Example 3
cards = ["C5","D6","C7","C8","C9","CA","C4"]return = false- The ranks 5, 6, 7, 8 and 9 are consecutive, but
D6is a diamond, so they are not all one suit. - The clubs are 4, 5, 7, 8, 9 and A. They have no five consecutive ranks (6 and 3 are missing), so the answer is
false.
Constraints
5 <= cards.length <= 52- Each
cards[i]is one suit letter (S,H,CorD) followed by one rank (A,2to10,J,QorK), so it has 2 or 3 characters. - All cards in
cardsare distinct.
More Temu interview questions
- Next Taller Building to the RightPHONE SCREEN · Seen Nov 2026
- SubsetsPHONE SCREEN · Seen Nov 2026
- Spiral Order with ObstaclesPHONE SCREEN · Seen Oct 2026
- Generate Spiral MatrixPHONE SCREEN · Seen Aug 2026
- Merge Two Sorted Collections Without DuplicatesPHONE SCREEN · Seen Aug 2026
- Spiral Matrix with ObstaclesONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Most Frequent NameAmazon · PHONE SCREEN
- Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
- Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
- Product Keyword SearchTesco · PHONE SCREEN
- Verify Words Sorted by a Custom AlphabetTrueFoundry · PHONE SCREEN
- Character Frequencies Across Nested String ListsSnowflake · PHONE SCREENSnowflake interview questions
- Neighboring Record IDsZipRecruiter · PHONE SCREENZipRecruiter interview questions