Blackjack Hand Score
Updated
Problem statement
In Blackjack, each card in a hand is worth points:
- Cards
"2"through"10"are worth their number. "J","Q"and"K"are worth10each.- Each
"A"(ace) is worth either1or11. You choose the value for each ace separately.
Given the cards of a hand as cards, return the hand's score. The score is the largest total that is at most 21. If every possible total is over 21, the score is the smallest total.
Function
getScore(cards: String[]) → intExamples
Example 1
cards = ["A","A","J"]return = 12With both aces worth 1, the total is 1 + 1 + 10 = 12. Making one ace worth 11 gives 22, which is over 21. So the score is 12.
Example 2
cards = ["J","J","A","2"]return = 23The two jacks and the 2 already add up to 22. Even with the ace worth 1, the total is 23. Every total is over 21, so the score is the smallest one, 23.
Example 3
cards = ["A","2"]return = 13With the ace worth 11, the total is 13. With the ace worth 1, it is 3. Both are at most 21, and 13 is the larger one.
Example 4
cards = ["10","9","A","A"]return = 2110 + 9 = 19. With both aces worth 1, the total is 21. Making any ace worth 11 gives at least 31, so the score is 21.
Example 5
cards = ["K","Q","5"]return = 25There is no ace, so the only total is 10 + 10 + 5 = 25. It is over 21, but it is the smallest total, so the score is 25.
Constraints
1 <= cards.length <= 100- Each
cards[i]is one of"A","2","3","4","5","6","7","8","9","10","J","Q"or"K".
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TableOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- First Unique Character in a StringOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- First N Fibonacci NumbersOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2021
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Sep 2019
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- FactorialPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2014
- Find the Four Missing Numbers in a FileOA · PHONE SCREEN · Seen Feb 2014
Similar questions at other companies
- Best Stock Trade with Buy and Sell DaysGoogle · PHONE SCREENGoogle interview questions
- Can Place FlowersAgoda · PHONE SCREENAgoda interview questions
- Maximum Later Checkpoint Altitude GainYellow.ai · PHONE SCREEN
- Assign CookiesAmazon · PHONE SCREEN
- Maximum Profit with Exactly One Stock TransactionRetool · PHONE SCREEN · ONSITE INTERVIEWRetool interview questions
- Best Time to Buy and Sell Stock IIGroww · ONSITE INTERVIEW
- Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Cheapest Round TripUber · ONSITE INTERVIEWUber interview questions
- Best Time to Buy and Sell Stock IIPoint72 · PHONE SCREENPoint72 interview questions
- Best Time to Buy and Sell StockJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions