Magicsheet logo

Zuma Game

Hard
97.8%
Updated 6/1/2025

Zuma Game

What is this problem about?

The Zuma Game coding problem is a complex simulation challenge inspired by the popular arcade game. You are given a string representing a row of colored balls and a hand of additional balls. Your task is to find the minimum number of balls you must insert into the row to eliminate all of them. When three or more balls of the same color are adjacent, they vanish, potentially triggering a chain reaction where remaining sections collapse together and create new groups that also vanish. This Hard-level problem requires searching through a vast state space while correctly simulating the physics of the "collapse."

Why is this asked in interviews?

Companies like Amazon and PhonePe ask the Zuma Game interview question because it tests multi-layered problem-solving skills. It requires a combination of string manipulation, recursive thinking, and performance optimization. You must demonstrate that you can break down a complex system into smaller parts: first, a simulation of the "collapse" logic, and second, an efficient search algorithm to find the shortest path. It’s a comprehensive test of both algorithmic knowledge and the ability to implement a sophisticated simulation without losing track of state.

Algorithmic pattern used

The primary algorithmic pattern used is Breadth-First Search (BFS) combined with Memoization and String/Stack manipulation. Because we seek the "minimum number of balls," BFS is a natural choice. Each state in our search consists of the current row and the remaining hand. To handle the chain reaction, we use a simulation function—often implemented with a stack—that repeatedly scans and removes sequences of three or more identical characters. We use memoization to store visited (row, hand) pairs, and pruning is essential to ignore redundant or impossible paths.

Example explanation

Suppose the row is "WRRBBW" and your hand is "RB". If you insert 'R' between the two existing 'R's, you get "WRRRBBW". The "RRR" group immediately disappears, leaving "WBBW". Now the two 'B's are adjacent. If you insert your 'B' between them, you get "WBBBW". The "BBB" group disappears, leaving "WW". The algorithm systematically tries all colors at all valid positions to find the absolute minimum number of insertions needed to empty the row completely.

Common mistakes candidates make

The most frequent mistake is an incorrect implementation of the "collapse" logic. Many candidates only remove the first group of three they find, forgetting that new adjacencies can trigger further removals. Another pitfall is failing to implement memoization or pruning, leading to Time Limit Exceeded (TLE) errors due to the exponential number of moves. Some also forget that you can insert a ball anywhere—at the beginning, end, or between any existing balls.

Interview preparation tip

To master the Zuma Game interview question, perfect your "string collapse" function using a stack-based approach; it is much more efficient than repeated slicing. Then, practice your BFS and DFS skills on state-space search problems. When explaining your solution, emphasize how you plan to handle the state space and what pruning strategies you'll use. This shows you are aware of the performance constraints of Hard-level problems and can proactively address them.

Similar Questions