Validate a Mahjong Hand Partition
Updated
Problem statement
A Mahjong-like hand is represented by an unordered array tiles whose values are digits from 1 through 9.
Return whether every tile can be partitioned into:
- exactly one pair of equal values (the pair of eyes); and
- zero or more three-tile groups, where each group is either three equal values or three consecutive values
x, x + 1, x + 2.
Every tile occurrence must belong to exactly one group. The order of tiles in the input does not matter.
Function
isValidMahjongHand(tiles: int[]) → booleanExamples
Example 1
tiles = [1,1,1,2,3,4,5,6]return = trueUse [1,1] as the pair, then form the runs [1,2,3] and [4,5,6].
Example 2
tiles = [1,1,1,2,2]return = trueThe hand partitions into the triple [1,1,1] and the pair [2,2].
Example 3
tiles = [1,1,1,1,2]return = falseThe only way to pick a pair is two of the four ones. That leaves one, one and two. These are not three equal tiles and not three tiles in a row, so the hand is not valid.
Constraints
2 <= tiles.length <= 20.- Every tile value is between
1and9. - A valid hand always has
3g + 2tiles for someg >= 0, so a hand of any other length is not valid.
More Duolingo Phone Screen questions
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
- Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
- Sum All Values in a Binary TreePHONE SCREEN · Seen Oct 2026
- User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
- Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026
- Directional Tiles: Find a Valid Reconfiguration PathPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Combination Sum with Reusable ValuesPinterest · PHONE SCREENPinterest interview questions
- All Subsets with a Given SumApple · PHONE SCREENApple interview questions
- Construct a Tree from Level-Order and Inorder TraversalsAmazon · PHONE SCREEN
- Optimal Account BalancingSalesforce · PHONE SCREENSalesforce interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Minimum Account SettlementsRippling · PHONE SCREENRippling interview questions
- Combination Sum With Unlimited ReuseMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- All Subsequences with Sum KExpedia · ONSITE INTERVIEWExpedia interview questions
- Optimal Account BalancingVirtu Financial · OAVirtu Financial interview questions
- Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions