Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

2D Array Region Sums

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is 2D Array Region Sums. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

OneTrust Data Privacy Impact Assessment workflows can represent control or risk relationships in a square matrix. Given an n x n integer matrix, compute the sums of the four regions formed by its two diagonals.

Exclude every cell on either diagonal: the main diagonal where row == column, and the anti-diagonal where row + column == n - 1. Classify every remaining cell into exactly one region:

  1. Top: strictly above both diagonals.
  2. Bottom: strictly below both diagonals.
  3. Left: strictly left of both diagonals.
  4. Right: strictly right of both diagonals.

Return a dictionary with keys top, bottom, left, and right.

Formal Specification

  • Input: matrix, a square list of lists containing integers.
  • Output: A dictionary mapping each region name to its integer sum.

Constraints

  • 2 <= n <= 500
  • matrix is an n x n square matrix
  • -10^6 <= matrix[row][column] <= 10^6
  • All region sums fit within Python integer limits

Function Signature

def sum_matrix_regions(matrix):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output