Compute Grouped Sums by Integer Label With np.bincount

Quick Overview

Write a NumPy function that sums values by integer group label into an array of a fixed length, with zeros for empty groups, using np.bincount. It tests weighted bincount, the minlength argument, label validation, and extensions to grouped means and 2-D values.

Compute Grouped Sums by Integer Label With np.bincount

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `group_sums(labels, values, n)` that aggregates `values` by integer group `labels`. Return a length-`n` array where element `k` is the sum of all values whose label equals `k`. Requirements: - Return an array of length exactly `n`. - Compute via `np.bincount(labels, weights=values, minlength=n)`. - Groups with no matching labels must be `0`. For example: ```python labels = np.array([0, 2, 2, 1]) values = np.array([1.0, 2.0, 3.0, 4.0]) n = 4 group_sums(labels, values, n) # -> array([1., 4., 5., 0.]) ``` ```hint Output length `np.bincount` sizes its output from the largest label it sees. Think about what happens when the highest groups have no values in this input, and when a label is too large. ``` ### Constraints & Assumptions - `labels` are non-negative ints, each `< n`. - `labels` and `values` have the same length. ### What a Strong Answer Covers - A single `np.bincount` call with `weights` and `minlength`, and no Python loop. - What `minlength=n` guarantees and what it does not (it never shortens the output). - The value and dtype of empty groups in the result. - Checks on the labels against `n`, and on the lengths of `labels` and `values`. - Time and space cost in terms of the number of values and `n`. ### Follow-up Questions - How would you extend this to group means instead of sums? - What if `values` is 2-D and you want per-column grouped sums? - How would you get the same result with pandas, and how does it differ from `np.bincount`?

Overview: Write a NumPy function that sums values by integer group label into an array of a fixed length, with zeros for empty groups, using np.bincount. It tests weighted bincount, the minlength argument, label validation, and extensions to grouped means and 2-D values.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement group_sums(labels, values, n) that aggregates values by integer group labels. Return a length-n array where element k is the sum of all values whose label equals k.

Requirements:

  • Return an array of length exactly n .
  • Compute via np.bincount(labels, weights=values, minlength=n) .
  • Groups with no matching labels must be 0 .

For example:

labels = np.array([0, 2, 2, 1])
values = np.array([1.0, 2.0, 3.0, 4.0])
n = 4
group_sums(labels, values, n)
# -> array([1., 4., 5., 0.])

Constraints & Assumptions

  • labels are non-negative ints, each < n .
  • labels and values have the same length.

What a Strong Answer Covers Guidance

  • A single np.bincount call with weights and minlength , and no Python loop.
  • What minlength=n guarantees and what it does not (it never shortens the output).
  • The value and dtype of empty groups in the result.
  • Checks on the labels against n , and on the lengths of labels and values .
  • Time and space cost in terms of the number of values and n .

Follow-up Questions Guidance

  • How would you extend this to group means instead of sums?
  • What if values is 2-D and you want per-column grouped sums?
  • How would you get the same result with pandas, and how does it differ from np.bincount ?