← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round with a Mahjong hand validation problem. Classic backtracking/recursion territory, felt pretty clean on the surface but the edge cases sneak up on you.

Questions Asked (1)

Q1

Given 14 integer tiles representing a Mahjong hand, determine whether they can be partitioned into 4 melds (each a triplet of identical tiles or a sequence of three consecutive tiles) and 1 pair of identical tiles.

Algorithms & Data Structures
Author's notes

My first instinct was to sort and greedily consume sequences, which is wrong and I knew it was wrong while doing it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by sorting the tiles and using a recursive backtracking algorithm that tries each possible pair and then recursively checks if the remaining tiles can form four melds. Use memoization to avoid redundant computations and ensure efficiency.

Pro tip: Emphasize that the order of processing matters: always handle the smallest tile first to reduce branching, and consider using a frequency array for O(1) tile lookups. Mention that this problem is a constrained version of the classic Mahjong hand decomposition, and demonstrating awareness of edge cases (like multiple identical tiles) shows depth.

1. Understand the problem and constraints

Clarify that there are exactly 14 tiles, and we need to partition them into 4 melds (triplets or sequences) and 1 pair. Note that tiles are integers, likely representing suits and ranks, but for simplicity, assume they are just numbers.

2. Choose a representation

Decide on a data structure to store tile counts, such as a sorted list or a frequency map (e.g., array of size 34 for standard Mahjong tiles). This allows efficient checks for triplets and sequences.

3. Design a recursive backtracking algorithm

Iterate over possible pairs (tiles with count >= 2), remove them, and then recursively attempt to form melds from the remaining tiles. For melds, always take the smallest tile and try to form a triplet or a sequence, backtracking if unsuccessful.

4. Optimize with memoization and pruning

Use memoization to cache results for a given tile count state, and prune branches early if counts become negative or if remaining tiles cannot form valid melds. Also, consider symmetry and avoid duplicate pair choices.

5. Analyze complexity and edge cases

Discuss time complexity (exponential in worst case but small due to constraints) and space complexity. Mention edge cases like multiple pairs, sequences spanning gaps, and invalid tile counts.

Key Points to Mention

  • Backtracking with recursion is a natural fit for this combinatorial search problem.
  • Using a frequency array or hash map allows O(1) checks for tile availability.
  • Always process the smallest tile first to reduce branching and ensure sequences are formed correctly.
  • Memoization can drastically reduce redundant computations, especially for repeated tile configurations.
  • The problem is equivalent to checking if a multiset of tiles can be decomposed into melds and a pair, which is NP-complete in general but tractable for 14 tiles.
  • Consider handling multiple identical tiles carefully to avoid missing valid decompositions.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.