9 NumPy Interview Questions and Answers You Must Know

NumPy interview questions

NumPy is a library for the Python programming language that is used for scientific computing and data analysis. It provides a powerful array object, as well as a large number of mathematical functions and tools to work with arrays. It is beneficial for working with large, multi-dimensional arrays and matrices of numerical data and provides a high-performance alternative to Python’s built-in list and tuple types.

Due to its popularity and wide usage by data scientists, interviewees expect their candidates to have extensive knowledge of NumPy. In this article, we will address some vital questions and look through their answers so you are confident during your interview. Let’s dive in! 

H2: Why Choose NumPy Rather Than Matlab, Yorick, or Octave?

Aside from being open-source, which enables a broader range of users to free access, an essential advantage of NumPy is its integrations with other Python libraries such as SciPy, Pandas, and Scikit-learn. Additionally, NumPy has a large and active community of users and developers, which means that there are many resources available for learning and troubleshooting.

Finally, NumPy is known for its high performance and efficient array manipulation capabilities, which are crucial for many scientific computing tasks. This is due to its use of C and Fortran code for low-level operations, which allows for faster computations than can be achieved in pure Python. 

H2: What Unique Features Does NumPy have?

  1. N-dimensional arrays: NumPy provides a convenient way to work with multi-dimensional arrays, which are essential for many scientific and mathematical applications.
  2. Broadcasting: NumPy allows you to perform operations on arrays with different shapes by broadcasting the smaller array to match the shape of the larger one.
  3. Mathematical functions: NumPy provides a wide range of mathematical functions, including linear algebra operations, Fourier transforms, and more.
  4. Interoperability: NumPy arrays can be seamlessly integrated with other libraries, such as pandas and scikit-learn, making it easy to use in data analysis and machine learning workflows.
  5. Memory-efficient: NumPy arrays use a compact and efficient representation, making it faster to work with large arrays and reducing your applications’ memory footprint.
  6. Vectorization: NumPy enables vectorized operations, allowing you to perform operations simultaneously on entire arrays rather than looping through the elements individually. This can significantly increase the performance of numerical computations.
  7. Built-in support for parallel computing: NumPy has built-in support for parallel computing via multi-core processors and GPUs, which can significantly speed up computationally intensive tasks.

H2: How to create a 1D Array?

If you are asked to create a 1D array, here are the steps to complete it. 

To create a 1D array in Python, you can use the built-in array module and the array() constructor. For example, to create an array of integers, you can use the following code:

import array

my_array = array.array(“i”, [1, 2, 3, 4, 5])

You can also create an array using a list comprehension or by using the * operator to repeat an existing array.

my_array = [i for i in range(10)]

my_array = [0] * 10

H2: How To Create 3D Array or ND Array ?

In NumPy, you can create a 3D array, also known as a multidimensional array or ND array, using the numpy.array() function. You can pass in a list of lists of lists to the function, where each innermost list represents a set of values for one of the dimensions. Here is an example of creating a 3x3x3 3D array:

import numpy as np

# Create a 3x3x3 array filled with zeros

my_3d_array = np.zeros((3, 3, 3))

# Alternatively, you can create an array filled with random values

import random

my_3d_array = np.array([[[random.random() for _ in range(3)] for _ in range(3)] for _ in range(3)])

Alternatively, you can use numpy.empty(), numpy.ones(), numpy.eye() to create an empty one or identity matrix, respectively, of the 3D array. Alternatively, you can use reshaping methods to convert a 1D or 2D array to a 3D array.

# Create an empty 3D array of shape (3,3,3)

my_3d_array = np.empty((3, 3, 3))

# Create an 3D array of ones of shape (3,3,3)

my_3d_array = np.ones((3, 3, 3))

# Create an 3D identity matrix of shape (3,3,3)

my_3d_array = np.eye(3,3,3)

H2: How to Compute The Min/Max for Each Row for a NumPy 2D Array?

During your interview, you might be asked to solve a similar problem: e.g., you might be asked to compute the min-by-max for each row that they give you. 

To compute the minimum and maximum for each row of a 2D NumPy array, you can use the numpy.amin() and numpy.amax() functions along with the axis parameter. The axis parameter is used to specify along which axis the minimum and maximum should be computed. By setting the axis parameter to 1, you can compute the minimum and maximum for each row of the 2D array. 

Here is an example:

import numpy as np

# Create a 2D array

my_2d_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Compute the minimum for each row

min_values = np.amin(my_2d_array, axis=1)

print(min_values)  # Output: [1 4 7]

# Compute the maximum for each row

max_values = np.amax(my_2d_array, axis=1)

print(max_values)  # Output: [3 6 9]

You can also use numpy.min() and numpy.max() in the same way but it depends on the version you are using.

Alternatively, if you want to get both min and max values for each row in a single step, you can use the numpy.ptp() function. It returns the range (maximum-minimum) along a given axis.

# Compute the range (max – min) for each row

range_values = np.ptp(my_2d_array, axis=1)

print(range_values) # Output: [2 2 2]

H2: How to Find the Element Data Type Stored in the NumPy Arrays?

To find the data type of the elements stored in a NumPy array, you can use the dtype attribute of the array. The dtype attribute returns an object that describes the type of elements in the array. 

import numpy as np

# Create a NumPy array

my_array = np.array([1, 2, 3])

# Get the data type of the elements in the array


This would output “int32” or “int64” depending on the version of numpy you are using. You can also use numpy.array() function and pass the type as an argument or use numpy.astype() function to change the data type of an array.

# Create an array with a specific data type

my_array = np.array([1, 2, 3], dtype=np.float64)

# change data type of an array

my_array = my_array.astype(np.int32)

It’s important to note that the data type of an array should be consistent, meaning all elements in the array should be of the same type. If you try to assign a value of a different type to an element in the array, it will be automatically cast to the data type of the array.

H2: How to Check for an Empty Array?

One way to find all the local maxima (or peaks) in a 1D array is to iterate through the array and, for each element, compare it to its adjacent elements. It is a local maxima if an element is greater than its left and right neighbors.


peaks = []

for i in range(1, len(arr) – 1):

    if arr[i] > arr[i-1] and arr[i] > arr[i+1]:


Another way is using the scipy library in Python, which have scipy.signal.find_peaks function. It will find the peaks by using different techniques. 

from scipy.signal import find_peaks

peaks, _ = find_peaks(arr)

H2: What Is The Function of “ndim” Attribute in NumPy?

The “ndim” attribute in NumPy is used to determine an array’s number of dimensions (i.e., the rank). It is a property of the ndarray class and can be accessed by calling the ndim attribute on an array object.

For example, a one-dimensional array will have an ndim value of 1, a two-dimensional array will have an ndim value of 2, and so on.

import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([[1, 2, 3], [4, 5, 6]])

arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print(arr1.ndim) # 1

print(arr2.ndim) # 2

print(arr3.ndim) # 3

This attribute can be useful when working with arrays of different dimensions, as it allows you to determine the shape and size of the array. Additionally, it allows determining the number of loops required to iterate over the array. 

H2: How to Select Elements From a NumPy Array?

There are several ways to select elements from a NumPy array. Below are four of them: 

  1. Using indexing: You can select elements from a NumPy array by specifying the indices of the elements you want to select. For example, ‘arr[1]’ will select the second element of the array ‘arr.’
  2. Using slicing: You can select a range of elements from a NumPy array by specifying a start index, stop index and step size. For example, ‘arr[1:3:1]’ will select elements from the second to the third element with a step size of 1.
  3. Using Boolean indexing: You can select elements from a NumPy array by using a Boolean mask. For example, ‘arr[arr > 2]’ will select all elements from the array ‘arr’ that are greater than 2.
  4. =: You can select elements from a NumPy array by using an array of indices. For example, ‘arr[[1,2,3]]’ will select the 2nd, 3rd and 4th element of the array ‘arr.’

For better visualization, here is an example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# Using indexing

print(arr[1]) #2

# Using slicing

print(arr[1:3]) # [2 3]

# Using Boolean indexing

print(arr[arr > 2]) # [3 4 5]

# Using Fancy indexing

print(arr[[1,3]]) # [2 4]


You must note that the questions you get in your interview will differ following the position you are applying for. However, the questions and answers provided above are the most common ones that are best to know in any case. If you are wondering how to best prepare for your interview, consider taking an online course to assist you with your progress. There is a NumPy course on WildLearner that can provide you with easy-to-learn materials to master NumPy better. Check it out and give it a go. Meanwhile, good luck!