Count the non-zero elements of a NumPy array of any shape

Quick Overview

Write a NumPy function that counts the non-zero elements of an array of any shape and returns a built-in Python integer. It tests vectorized counting without loops, which values count as zero (NaN, negative zero, booleans, tiny floats), and control over return types.

Count the non-zero elements of a NumPy array of any shape

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write a function that takes a NumPy array of any shape and returns how many of its elements are non-zero. Return a plain Python `int`, not a NumPy scalar such as `np.int64`. For example: ```python import numpy as np arr = np.array([[0, 1, 2], [0, 0, 3]]) num_nonzero(arr) # 3, of type int ``` ```hint Check unusual values Before you settle on an approach, decide what your function should return for `np.nan`, `-0.0`, `False` and a value like `1e-17` left over from floating-point rounding. ``` ### What a Strong Answer Covers - A vectorized way to count that works for any number of dimensions, with no Python loop. - How the return type is made a built-in `int`. - Which values count as non-zero, including NaN, negative zero, booleans and tiny floats. - Time and extra space cost. ### Follow-up Questions - How would you count the non-zero elements in each row or each column? - How would you treat values within a small tolerance of zero, such as `1e-9`, as zero? - How does `np.count_nonzero(arr)` compare with `(arr != 0).sum()` and with `np.nonzero(arr)`? - How would you get the count for a large `scipy.sparse` matrix without converting it to a dense array?

Overview: Write a NumPy function that counts the non-zero elements of an array of any shape and returns a built-in Python integer. It tests vectorized counting without loops, which values count as zero (NaN, negative zero, booleans, tiny floats), and control over return types.

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

Write a function that takes a NumPy array of any shape and returns how many of its elements are non-zero. Return a plain Python int, not a NumPy scalar such as np.int64.

For example:

import numpy as np

arr = np.array([[0, 1, 2], [0, 0, 3]])
num_nonzero(arr)  # 3, of type int

What a Strong Answer Covers Guidance

  • A vectorized way to count that works for any number of dimensions, with no Python loop.
  • How the return type is made a built-in int .
  • Which values count as non-zero, including NaN, negative zero, booleans and tiny floats.
  • Time and extra space cost.

Follow-up Questions Guidance

  • How would you count the non-zero elements in each row or each column?
  • How would you treat values within a small tolerance of zero, such as 1e-9 , as zero?
  • How does np.count_nonzero(arr) compare with (arr != 0).sum() and with np.nonzero(arr) ?
  • How would you get the count for a large scipy.sparse matrix without converting it to a dense array?