FastPrepCount Squares After Removing Middle-Row Cells
Problems/Count Squares After Removing Middle-Row Cells

Count Squares After Removing Middle-Row Cells

JP Morgan Chase logoJP Morgan Chase● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a JP Morgan Chase intern onsite interview. The author reports counting squares from n vertical lines and m horizontal lines after removing l blocks from the left of the middle horizontal strip. We chose unit spacing, even m, whole-cell validity, the limits and the test data. Our version is about 90% the same as the reported counting task.

A grid has n vertical lines and m horizontal lines. For this exercise, the lines are unit-spaced, so the grid has n-1 columns and m-1 rows of cells.

The even number m gives two middle horizontal lines. The cell row between them has zero-based index m/2 - 1. Remove its l leftmost cells.

Return the number of axis-aligned squares made entirely of remaining cells. Count every size and position. A square containing any removed cell does not count; removing cells does not shift the other cells.

Function

countSquares(n: int, m: int, l: int) → int

Examples

Example 1

n = 4m = 4l = 1return = 10

The original 3-by-3 cell grid has 14 squares. Removing the leftmost middle cell destroys one unit square, two 2-by-2 squares and the 3-by-3 square, leaving 10.

Example 2

n = 4m = 4l = 0return = 14

No cells are removed. Count 9 unit squares, 4 squares of side 2 and 1 square of side 3.

Constraints

  • For this exercise, assume 2 <= n, m <= 30, and m is even.
  • 0 <= l < n.
  • Rows and columns are unit-spaced, and only axis-aligned squares made of complete remaining cells count.

More JP Morgan Chase Onsite Interview questions

  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU CacheONSITE INTERVIEW · Seen Nov 2026
  • Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
  • Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
  • K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
  • Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
  • Two SumONSITE INTERVIEW · Seen Nov 2025
  • Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
  • Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
  • Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024

Similar questions at other companies

  • Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Best Restart Cutoff for a Long-Tailed JobOpenAI · ONSITE INTERVIEWOpenAI interview questions
  • Random Pick with Weight Using Recorded DrawsAmazon · ONSITE INTERVIEW
  • Multinomial Sampling with Distribution ChecksNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Volunteer Awards: Totals Divisible by KNutanix · ONSITE INTERVIEWNutanix interview questions
  • Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
  • Count Attack Vectors by Divisor RangeVisa · OAVisa interview questions
  • Maximum Trailing Zeros on a Path with at Most One TurnMarshall Wace · OAMarshall Wace interview questions
  • Count Analogous ArraysAtlassian · OAAtlassian interview questions
  • Growth in 2 Dimensions 🍂IXL Learning · OA

Interview reports that mention this question

  • JPMorgan Tech Intern Interviews: 43 Experiences (Part 1 of 5)

JP Morgan Chase interview experiences

  • JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
  • JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
  • JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
  • JP Morgan Chase Data and AI Intern Superday: 11 Experiences
  • JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
class Solution {
    public int countSquares(int n, int m, int l) {
        // Write your code here.
    }
}
Checking account…