PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates dynamic programming, matrix traversal, and algorithmic optimization skills for identifying and counting structured patterns in grids.

  • medium
  • Purestorage
  • Coding & Algorithms
  • Software Engineer

Count All-One Square Submatrices

Company: Purestorage

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an `n x n` binary matrix, count how many square submatrices contain only `1`s. A square submatrix can have any side length from `1` to `n`. Example: ```text matrix = [ [1, 0, 1], [1, 1, 0], [1, 1, 0] ] ``` The all-one square submatrices are: - Five `1 x 1` squares - One `2 x 2` square in the bottom-left corner So the answer is `6`. Describe or implement approaches that improve from a brute-force solution toward an `O(n^2)` solution.

Quick Answer: This question evaluates dynamic programming, matrix traversal, and algorithmic optimization skills for identifying and counting structured patterns in grids.

Given an n x n binary matrix, return the total number of square submatrices whose cells are all 1. A square submatrix may have any side length from 1 to n. If the matrix is empty, return 0. A brute-force solution would try every possible square and check whether all its cells are 1, but that becomes too slow as n grows. Aim for a dynamic programming solution that runs in O(n^2) time. Example: for matrix = [[1, 0, 1], [1, 1, 0], [1, 1, 0]], there are six 1 x 1 all-one squares and one 2 x 2 all-one square, so the answer is 7.

Constraints

  • 0 <= n <= 300
  • matrix has n rows and n columns
  • Each matrix[i][j] is either 0 or 1

Examples

Input: ([[1, 0, 1], [1, 1, 0], [1, 1, 0]],)

Expected Output: 7

Explanation: There are six 1 x 1 all-one squares and one 2 x 2 square in the bottom-left area.

Input: ([],)

Expected Output: 0

Explanation: An empty matrix contains no submatrices.

Input: ([[0]],)

Expected Output: 0

Explanation: The only cell is 0, so there are no all-one squares.

Input: ([[1, 1], [1, 1]],)

Expected Output: 5

Explanation: There are four 1 x 1 squares and one 2 x 2 square.

Input: ([[1, 1, 1], [1, 1, 1], [0, 1, 1]],)

Expected Output: 11

Explanation: There are eight 1 x 1 squares and three 2 x 2 squares.

Input: ([[1, 1, 1], [1, 1, 1], [1, 1, 1]],)

Expected Output: 14

Explanation: There are nine 1 x 1 squares, four 2 x 2 squares, and one 3 x 3 square.

Hints

  1. For each cell, think about the size of the largest all-one square that ends at that cell.
  2. If matrix[i][j] is 1, its square size depends on the cells directly above, directly left, and diagonally up-left.
Last updated: Jun 6, 2026

Related Coding Questions

  • Determine whether points form squares - Purestorage (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.