Row Aggregates

A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
medium
Seniority
L3

Problem

Given a 2D matrix of numbers, return a list where each element is the sum of the corresponding row.

Each row holds its own summary.

Example 1

Input:

matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output:

[6,15,24]

Example 2

Input:

matrix = [[10,20],[30,40]]

Output:

[30,70]

Worked solution and explanation

Why this problem exists in real interviews

Summing each row of a matrix tests basic nested iteration and whether you can map a 2D structure to a 1D result. It is the Python equivalent of a GROUP BY with SUM.


Break down the requirements

Step 1: Iterate over each row

Each row is a list of numbers within the matrix.

Step 2: Sum the elements of each row

Accumulate the row total and add it to the result list.


The solution

Row-wise summation with nested iteration
def row_sums(matrix):
    result = []
    for row in matrix:
        total = 0
        for val in row:
            total += val
        result.append(total)
    return result

Common follow-up questions