Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)
Updated
Problem statement
Given a 2-D integer array matrix, return [resRow, resCol]: the cut that splits matrix into 4 rectangular partitions with the smallest difference between the largest and the smallest partition average.
Let rows and cols be the number of rows and columns. Cutting after row resRow and after column resCol gives these partitions, where a..b includes both ends:
matrix[0..resRow][0..resCol]matrix[0..resRow][resCol+1..cols-1]matrix[resRow+1..rows-1][0..resCol]matrix[resRow+1..rows-1][resCol+1..cols-1]
The average value of a partition is the sum of its non-negative values divided by the count of its non-negative values, rounded down. Negative values are left out of both the sum and the count.
A cut is allowed only when each of the 4 partitions has at least one non-negative value. If several allowed cuts give the same smallest difference, return the one with the smallest resRow, and among those the one with the smallest resCol.
Note: 0 is a non-negative value, so it counts toward the average.
Function
findBestSplit(matrix: int[][]) → int[]Examples
Example 1
matrix = [[1,-3,4,2],[6,0,-1,5]]return = [0,2]Cutting after row 0 and column 2 gives the partitions [1, -3, 4], [2], [6, 0, -1] and [5]. Their averages are 5 / 2 = 2, 2, 6 / 2 = 3 and 5 (rounded down), so the difference is 3. Cutting after column 0 or column 1 instead gives a difference of 5 or 4.
Example 2
matrix = [[9,-1,3],[7,1,3],[-1,5,5]]return = [1,1]Cutting after row 1 and column 1 gives the averages 17 / 3 = 5, 3, 5 and 5, a difference of 2. The cut [1, 0] is not allowed because its bottom-left partition holds only -1, and the cuts [0, 0] and [0, 1] both give a difference of 6.
Example 3
matrix = [[-5,6,8],[7,0,-5],[2,3,7]]return = [0,1]The cut [0, 0] is not allowed because its top-left partition holds only -5. The cuts [0, 1] (averages 6, 8, 3, 7) and [1, 0] (averages 7, 4, 2, 5) both give the smallest difference, 5, and [0, 1] has the smaller resRow. The cut [1, 1] gives 6.
Constraints
2 <= matrix.length <= 402 <= matrix[i].length <= 40- All rows of
matrixhave the same length. -1000 <= matrix[i][j] <= 1000- At least one cut is allowed.
More Boston Consulting Group interview questions
- Build Taxi Driver Performance FeaturesOA · Seen Nov 2026
- Summarize Taxi Drivers and RidesOA · Seen Nov 2026
- Competition Winner and Runner-Up (Forward Deployed Engineer)OA · Seen Nov 2026
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)OA · Seen Nov 2026
- Robot Stops Left or Right (Forward Deployed Engineer)OA · Seen Nov 2026
- Predict Taxi Driver ClassesOA · Seen Oct 2026
- Prepare Taxi Driver Classification DataOA · Seen Oct 2026
- Even-Position MonotonicityOA · Seen Jul 2026
- Shortest Distances From an Adjacency MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
- Maximum Sum RectangleDeloitte · OADeloitte interview questions
- Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
- Inclusive Rectangle Coverage CountsGoogle · ONSITE INTERVIEWGoogle interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Matrix Region and Ring SumsNVIDIA · PHONE SCREENNVIDIA interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- Most Visited Sprint MarkerMicrosoft · OAMicrosoft interview questions