Count Sub Islands
A medium-tier problem at 73% community acceptance, tagged with Array, Depth-First Search, Breadth-First Search. Reported in interviews at X and 2 others.
Count Sub Islands is a medium-difficulty matrix traversal problem that shows up in real OAs at X, Zepto, and DoorDash. You're given two binary matrices and need to count how many islands in the first matrix are completely contained within islands in the second. The acceptance rate sits at 73%, which sounds safe until you realize most candidates fail because they misread the containment rule or pick the wrong traversal strategy. If this problem hits your live assessment and you blank on the approach, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Count Sub Islands"
Count Sub Islands is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe trap here is thinking 'island counting' when it's actually 'island subset validation'. You can't just do a standard DFS or BFS and tally results. Every land cell in a potential sub-island must have a corresponding land cell in the parent island. Most candidates start with Union Find or vanilla graph traversal, hit edge cases around validation, and run out of time. The real pattern is double traversal: iterate through island one, and for each cell, verify it has a match in island two before marking it visited. Array and Matrix manipulation matter because indexing errors cascade fast. StealthCoder is your hedge for the moment you realize your first approach leaves gaps.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count Sub Islands recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Sub Islands interview FAQ
Is this actually just island counting with extra steps?+
No. Standard island counting doesn't validate containment. You need to confirm every land cell in a potential sub-island maps to a land cell in the parent island. That validation step is what separates a wrong answer from a passing one.
Why do DFS and BFS both work here?+
Both traverse connected components. Choice depends on your stack space comfort and code clarity. DFS is recursive and tight; BFS is iterative and safer if you're nervous about stack overflow on large matrices. Neither is wrong if the validation logic is correct.
Do X and DoorDash ask the same variant?+
Input data isn't specified here, but yes, large tech companies often ask similar two-matrix containment problems. Variations swap the constraint (subset vs superset) but the traversal and validation logic stay the same.
What's the time complexity I should target?+
O(m*n) where m and n are matrix dimensions. You visit each cell once during traversal, and each cell's parent lookup is O(1). Union Find variants can add a log factor, but straightforward DFS/BFS avoids that overhead.
How do I avoid off-by-one errors on the second matrix lookup?+
Before you traverse, confirm both matrices have the same dimensions. Use the same indexing scheme for both. Walk through a 3x3 example by hand before submitting. Matrix problems are brittle. One wrong index kills ten test cases.
Want the actual problem statement? View "Count Sub Islands" on LeetCode →