MEDIUMasked at 1 company

Count Square Submatrices with All Ones

A medium-tier problem at 79% community acceptance, tagged with Array, Dynamic Programming, Matrix. Reported in interviews at Tinkoff and 0 others.

Founder's read

Count Square Submatrices with All Ones is a medium-difficulty dynamic programming problem that tests whether you can build an efficient 2D state table instead of brute-forcing every possible square. It's asked by companies like Tinkoff and has a 78% acceptance rate, which means most who study DP patterns solve it, but candidates who haven't seen this exact structure often get stuck on the recurrence relation. If this problem hits your live OA and you can't immediately see how to avoid O(n^4) enumeration, StealthCoder solves it in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
79%

Companies that ask "Count Square Submatrices with All Ones"

If this hits your live OA

Count Square Submatrices with All Ones 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. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code.

Get StealthCoder
What this means

The trap is trying to validate every possible square by checking all its cells. That's exponential. The trick: use a 2D DP array where dp[i][j] stores the side length of the largest all-ones square with bottom-right corner at (i, j). Each cell depends on only three neighbors: dp[i-1][j], dp[i][j-1], and dp[i-1][j-1]. If the current cell is 1, the largest square ending here is min(those three) + 1. Then sum all the dp values to count total submatrices. It's O(n*m) time and space. Most candidates understand DP in general but miss that the answer isn't just the maximum square, it's the sum of all side lengths. That's where the conceptual break happens on a timed assessment. StealthCoder covers this exact gap.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Count Square Submatrices with All Ones 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. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Square Submatrices with All Ones interview FAQ

How hard is this really compared to other DP matrix problems?+

It's solidly medium. The 78% acceptance rate reflects that anyone who's practiced standard DP patterns recognizes the structure. But the transition from 'find the max square' to 'count all squares' trips up candidates who haven't explicitly solved this before.

What's the key insight I'm missing if I think the answer is just the max square size?+

You need to count every square, not just find the largest one. The DP recurrence tells you the biggest square at each position, but summing all those side-length values (or more precisely, the side lengths) gives you the count of all valid submatrices.

Does this problem require modular arithmetic or special output formatting?+

Input data doesn't specify constraints that'd force modulo math. Standard integer output. Focus on the DP logic first, then verify against the full test suite.

How does this relate to the broader 'Matrix' and 'Array' topic grouping?+

It combines both. You're iterating a 2D array structure and maintaining a DP matrix in parallel. Mastering this pattern helps with maximal-rectangle and similar 2D-range-query problems.

If I blank on the recurrence during the OA, is there a brute-force backup?+

Yes, but it's slow. O(n^4) enumeration will TLE on larger inputs. That's why StealthCoder is your hedge if the DP formula doesn't click in the moment.

Want the actual problem statement? View "Count Square Submatrices with All Ones" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.