Quick Overview

A matrix problem that considers every k by k square in a grid, sums the values on each square's border, and asks for the sum of the distinct values appearing on all borders that reach the maximum border sum. It tests border enumeration without double counting corners, tie handling across squares, and value de-duplication.

Sum of Distinct Values on Maximum-Sum Square Borders in a Matrix

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

You are given an `m x n` matrix of positive integers and an integer `k`. Consider every `k x k` square that fits entirely inside the matrix. The **border** of a square is the set of its cells that lie in its first row, last row, first column or last column. For `k = 1` the border is the single cell, and for `k = 2` it is all four cells. Find the largest border sum over all squares. Then take every square whose border sum equals that maximum, collect the values that appear on any of their borders, and return the sum of the distinct values, counting each value once no matter how many times or on how many borders it appears. ### Function Signature ```python def distinct_sum_of_max_borders(matrix: list[list[int]], k: int) -> int: ``` ### Rules - A square is identified by its top-left cell; every top-left position that keeps the whole square inside the matrix is considered. - Each border cell counts once in its square's border sum, including the corners where a border row meets a border column. - Cells inside the border do not count toward the border sum and do not contribute values. - "Distinct" refers to values, not cells: cells holding the same value contribute that value once. ### Constraints - `1 <= m, n <= 100`, where `m = len(matrix)` and `n = len(matrix[0])`; every row has length `n`. - `1 <= matrix[i][j] <= 10^4` - `1 <= k <= min(m, n)` ### Examples **Example 1** ```text Input: matrix = [[1, 1, 1, 1], [1, 9, 9, 1], [1, 1, 1, 1]], k = 3 Output: 10 ``` Two `3 x 3` squares fit, with top-left cells `(0, 0)` and `(0, 1)`. Each border holds seven 1s and one 9 (the other 9 is inside that square), so both border sums are 16. The values on these borders are 1 and 9, and their distinct sum is 10. **Example 2** ```text Input: matrix = [[3, 8], [8, 1]], k = 1 Output: 8 ``` Each `1 x 1` border is a single cell. The maximum, 8, is reached by two cells holding the same value, which counts once. **Example 3** ```text Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], k = 2 Output: 28 ``` The four border sums are 12, 16, 24 and 28. Only the bottom-right square reaches 28, and its distinct values 5, 6, 8 and 9 sum to 28.

Overview: A matrix problem that considers every k by k square in a grid, sums the values on each square's border, and asks for the sum of the distinct values appearing on all borders that reach the maximum border sum. It tests border enumeration without double counting corners, tie handling across squares, and value de-duplication.

Read the full Capital One Machine Learning Engineer interview experience this question came from

You are given an `m x n` matrix of positive integers and an integer `k`. Consider every `k x k` square that fits entirely inside the matrix. A square is identified by its top-left cell, and every top-left position that keeps the whole square inside the matrix is considered. The **border** of a square is the set of its cells that lie in its first row, last row, first column or last column. For `k = 1` the border is the single cell, and for `k = 2` it is all four cells. A square's **border sum** adds each border cell exactly once, including the corners where a border row meets a border column. Cells inside the border do not count toward the border sum and do not contribute values. Find the largest border sum over all squares. Then take every square whose border sum equals that maximum, collect the values that appear on any of their borders, and return the sum of the distinct values. Distinct refers to values, not cells: a value counts once no matter how many cells hold it or on how many of those borders it appears. Implement `distinct_sum_of_max_borders(matrix, k)` and return that sum as an integer. The answer always fits in a signed 32-bit integer: a border sum is at most 3,960,000 and the answer is at most 50,005,000. ### Constraints - `1 <= m, n <= 100`, where `m = len(matrix)` and `n = len(matrix[0])`; every row has length `n`. - `1 <= matrix[i][j] <= 10^4` - `1 <= k <= min(m, n)` ### Example 1 ```text Input: matrix = [[1, 1, 1, 1], [1, 9, 9, 1], [1, 1, 1, 1]], k = 3 Output: 10 ``` Two `3 x 3` squares fit, with top-left cells `(0, 0)` and `(0, 1)`. Each border holds seven 1s and one 9 (the other 9 is inside that square), so both border sums are 16. The values on these borders are 1 and 9, and their distinct sum is 10. ### Example 2 ```text Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], k = 2 Output: 28 ``` The four border sums are 12, 16, 24 and 28. Only the bottom-right square reaches 28, and its distinct values 5, 6, 8 and 9 sum to 28.

Constraints

  • 1 <= m, n <= 100, where m = len(matrix) and n = len(matrix[0]); every row has length n
  • 1 <= matrix[i][j] <= 10^4
  • 1 <= k <= min(m, n)

Examples

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

Expected Output: 10

Explanation: Source Example 1: the two 3 x 3 squares tie at border sum 16, each with one 9 inside its border; the union of border values {1, 9} sums to 10.

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

Expected Output: 8

Explanation: Source Example 2: with k = 1 two cells holding 8 tie for the maximum, and the value 8 counts once.

Hints

  1. Each border cell counts once: for k = 1 and k = 2 every cell of the square is on the border, and a corner belongs to both a border row and a border column but is still counted only once.
  2. Several squares can share the maximum border sum, so you need the maximum before you know which borders to collect values from.
  3. Distinctness is by value, not by position: two cells holding the same number contribute that number once.

Distinct Values on the Heaviest Square Borders

hardCapital OneMachine Learning Engineer

You are given an m x n matrix of positive integers and an integer k. Consider every k x k square that fits entirely inside the matrix. A square is identified by its top-left cell, and every top-left position that keeps the whole square inside the matrix is considered.

The border of a square is the set of its cells that lie in its first row, last row, first column or last column. For k = 1 the border is the single cell, and for k = 2 it is all four cells. A square's border sum adds each border cell exactly once, including the corners where a border row meets a border column. Cells inside the border do not count toward the border sum and do not contribute values.

Find the largest border sum over all squares. Then take every square whose border sum equals that maximum, collect the values that appear on any of their borders, and return the sum of the distinct values. Distinct refers to values, not cells: a value counts once no matter how many cells hold it or on how many of those borders it appears.

Implement distinct_sum_of_max_borders(matrix, k) and return that sum as an integer. The answer always fits in a signed 32-bit integer: a border sum is at most 3,960,000 and the answer is at most 50,005,000.

Constraints

  • 1 <= m, n <= 100, where m = len(matrix) and n = len(matrix[0]); every row has length n.
  • 1 <= matrix[i][j] <= 10^4
  • 1 <= k <= min(m, n)

Example 1

Input:  matrix = [[1, 1, 1, 1],
                  [1, 9, 9, 1],
                  [1, 1, 1, 1]], k = 3
Output: 10

Two 3 x 3 squares fit, with top-left cells (0, 0) and (0, 1). Each border holds seven 1s and one 9 (the other 9 is inside that square), so both border sums are 16. The values on these borders are 1 and 9, and their distinct sum is 10.

Example 2

Input:  matrix = [[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]], k = 2
Output: 28

The four border sums are 12, 16, 24 and 28. Only the bottom-right square reaches 28, and its distinct values 5, 6, 8 and 9 sum to 28.

Examples

Example 1
Input
([[1, 1, 1, 1], [1, 9, 9, 1], [1, 1, 1, 1]], 3)
Output
10
Notes
Source Example 1: the two 3 x 3 squares tie at border sum 16, each with one 9 inside its border; the union of border values {1, 9} sums to 10.
Example 2
Input
([[3, 8], [8, 1]], 1)
Output
8
Notes
Source Example 2: with k = 1 two cells holding 8 tie for the maximum, and the value 8 counts once.

Constraints

  • •1 <= m, n <= 100, where m = len(matrix) and n = len(matrix[0]); every row has length n
  • •1 <= matrix[i][j] <= 10^4
  • •1 <= k <= min(m, n)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def distinct_sum_of_max_borders(matrix: list[list[int]], k: int) -> int:
Sample call
distinct_sum_of_max_borders([[1, 1, 1, 1], [1, 9, 9, 1], [1, 1, 1, 1]], 3)
Language
Case 1
Input
([[1, 1, 1, 1], [1, 9, 9, 1], [1, 1, 1, 1]], 3)
Expected
10
Case 2
Input
([[3, 8], [8, 1]], 1)
Expected
8

Browse More Questions

Show the approach

Approach

Build a 2D prefix-sum table so the sum of any axis-aligned rectangle takes O(1). For the square with top-left (i, j), the border sum is the sum of the whole k x k block minus, when k >= 3, the sum of its (k-2) x (k-2) interior. For k = 1 and k = 2 there is no interior, so the block sum is already the border sum; every cell appears once in the block, so corners are counted exactly once. Scan all (m-k+1)(n-k+1) top-left positions while keeping the running maximum and the list of positions that reach it: a strictly larger sum resets the list, an equal sum appends to it. Then walk the border of every winning square (its top row, its bottom row, and the left and right columns strictly between them), put each value into a set, and add a value to the total only the first time it enters the set. Correctness: the first pass computes exactly the defined border sum of every valid square, so afterwards the list holds exactly the squares at the maximum. The second pass visits exactly the border cells of those squares and never an interior cell, and the set makes each value count once however many cells or borders hold it. Edge cases: k = 1 (each square is one cell, so the answer is the largest value), k = 2 (no interior), k = min(m, n) on a square matrix (a single square), single-row or single-column matrices, and an all-equal matrix where every square ties and the answer is that one value. Every border sum is at most 3,960,000 and the answer at most 50,005,000, so 32-bit integers suffice for the result.

Time complexity:
O(m * n * k)
Space complexity:
O(m * n)