The Aggregator
You are building a data pipeline that processes feature matrices from multiple sensors. Each row represents one sensor reading across different features, and each column represents one feature across all readings. Your downstream model needs both row-level and column-level aggregations.
Given a 2D matrix, return a list of two lists:
- First list: sum of each row (one value per row)
- Second list: sum of each column (one value per column)
Example 1
[[1, 2, 3], [4, 5, 6]][[6, 15], [5, 7, 9]]Row sums: [1+2+3, 4+5+6] = [6, 15]. Col sums: [1+4, 2+5, 3+6] = [5, 7, 9]. axis=1 for rows, axis=0 for columns.
Example 2
[[10], [20], [30]][[10, 20, 30], [60]]Single column matrix. Row sums: [10, 20, 30]. Col sum: [60].
Example 3
[[-1, -2], [-3, -4]][[-3, -7], [-4, -6]]All negatives. Row sums: [-1+(-2), -3+(-4)] = [-3, -7]. Col sums: [-1+(-3), -2+(-4)] = [-4, -6].
- ›1 <= rows <= 100
- ›1 <= cols <= 100
- ›-1000 <= matrix[i][j] <= 1000
Reference solution available after you attempt the question.
Ready to solve it?
Start a session on Mockbit #67. Write your code, run it against hidden tests, and get graded with specific critique on each axis.