Top 9 NumPy Interview Questions

Top 9 NumPy Interview Questions

5 mins read2.6K Views Comment
Updated on Oct 3, 2023 12:10 IST

In this article, we will discuss basic and intermediate NumPy Interview Questions in most simplified ways with example.



In this article, we will discuss basic and intermediate NumPy Interview Question.

If you are an aspiring data scientist, you are sure to deal with NumPy array-related questions during interviews given the importance of the library during data analytics.

Often, you will be asked to perform simple and straightforward array operations in Python using NumPy. This article will arm you with all the basics of the NumPy library to prepare for the most common questions that come up during Data Science interviews, through Python codes

Table of Content

Quick Intro to NumPy

NumPy (aka Numerical Python) is a third-party open-source Python library used for scientific computations. It offers powerful functions and tools for performing a variety of arithmetic and logical operations on numerical data.

Let’s discuss the common NumPy-related questions you might come across during your interviews.

Top 110+ Python Interview Questions and Answers
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due more

Basic NumPy Questions

Q1. What are the most common NumPy data types?

A1. NumPy arrays support a number of numerical data types, the most common amongst them are discussed below –

An N-dimensional array type is known as ndarray. All elements stored in a ndarray should be homogeneous i.e., have the same type.

A few of the most common data types are:

  • Boolean
  • Integer (signed and unsigned)
  • Float
  • Complex

Q2. How to create NumPy arrays?


  • One-dimensional array – consists of a single row.
import numpy as np
#1D Array
arr1 = np.array([1,2,3])
Copy code
  • Multi-dimensional array – consists of multiple rows and columns, like an Excel spreadsheet, wherein each column can be considered as a dimension.
#2D Array
X = ([[1.8, 2.5, 3.14],
[4, 5, 6]])
arr2 = np.array(X)
Copy code
#3D Array
Y = ([[1,2,3],
arr3 = np.array(Y)
Copy code

Q3. How do you identify the data type of an array?

A3. Through the following command:

print('data type arr1 ', arr1.dtype)
print('data type arr2 ', arr2.dtype)
print('data type arr3 ', arr3.dtype)
Copy code

Q4. List the advantages of NumPy over Python lists.

A4. Traditional Python lists have several limitations when compared to NumPy arrays –

Feature NumPy Arrays Python Lists
Data type Stores homogenous data i.e., data having the same types Stores heterogenous data i.e., elements can be of different data types
Flexibility Less flexible, because operations are performed element-wise
Allows more flexibility in adding and removing data
Arithmetic and matrix operations Many vectors and matrix operations are in-built Complex statistical and analytical libraries are not available
Loops Explicit loop is required to run through the elements Explicit loop is not required to run through the elements
Declaration for usage NumPy module needs to be imported for using NumPy arrays
No import is required to use Python lists

Q5. What is negative indexing in a NumPy array?

A5. In a NumPy array, the elements can be accessed in any order as shown:


As you can see, negative indexing refers to indexing from the end of the list starting at -1.

Q6. How do you count the number of times a value appears in a NumPy array of integers?

A6. For this, we will use the bincount() method that accepts only positive integers or Boolean expressions as its arguments.

arr = np.array([1, 7, 1, 1, 7, 6, 5, 1, 3, 3, 2, 0, 0])
Copy code

Q7. How do you create an array with all values as zeros or ones?

A7. For this, we will use the np.zeros() and np.ones() methods:

#Creating array of zeros
zeros_arr = np.zeros((4,4))
Copy code
#Creating array of zeros
zeros_arr = np.zeros((4,4))
Copy code

Q8. Print an array range between 1 to 35 and show 7 integer random numbers.

rand_arr = np.random.randint(1,35,7)
Copy code

Q9. Print the first, third, and last position of a NumPy array?

rand_arr = np.random.randint(1,35,7)
Copy code

Must Read: What is Python?

Intermediate NumPy Questions

Q1. How to change the data type of a NumPy array?

A1. You can use the astype() method for this:

arr = np.array([2.7, 3.14, 5.89, 7])
new_arr = arr.astype('i')
Copy code

Q2. How to reverse a 3×3 matrix?


  • First, let’s create a 3×3 matrix:
matrix = np.arange(30,39).reshape(3,3)
Copy code
  • Now, let’s flatten the array:
flat = matrix.flatten()
Copy code
  • Let’s reverse the flattened array:
flat = matrix.flatten()
Copy code
  • Lastly, let’s reshape the array back to a 3×3 matrix:
Copy code

Q3. How to add matrices using NumPy?


  • Let’s first create two matrices:
#Construct two matrices
A = ([[1,2,3],
B = ([[2,4,6],
  • Now, just perform simple addition between the two ndarrays:
arr_A = np.array(A)
arr_B = np.array(B)
result = arr_A + arr_B

Q4. How to multiply matrices using NumPy?


  • Now, let’s find the dot product of the two ndarrays we created in the above question:
#Dot product of two matrices
result =,arr_B)
  • The same result will be obtained when we use np.matmul() for matrix product:
result = np.matmul(arr_A,arr_B)
  • You can also use np.multiply() to return the element-wise matrix multiplication of two ndarrays:
result = np.multiply(arr_A,arr_B)

Q5. How to find the transpose of the matrix using NumPy?


#Construct a matrix= ([[38,13],
#Find the transpose of the matrix
transpose = np.transpose(X)

Q6. What is array slicing and how do you do it in NumPy?

A6. Slicing means extracting a portion of the given array to generate a new view of the same array without copying it. This is done by specifying the following:

  • start: specifies where to start slicing from aka the lower limit
  • stop: specifies where to stop slicing aka the upper limit
  • step: determines the increment between each index for slicing
#Slicing a 1D array= np.array([2, 3, 7, 11, 13, 17, 19, 23, 29, 31, 37]) 

Q7. How do you stack matrices?

A7. NumPy arrays/matrices can be stacked either horizontally or vertically through the following methods:

  • np.hstack() method adds the second ndarray to the columns of the first ndarray:
#Construct two matrices
A = ([[1,2,3],
B = ([[2,4,6],
arr_A = np.array(A)
arr_B = np.array(B)
#Stacking Horizontally
np.hstack((arr_A, arr_B))
  • np.vstack() method combines the second ndarray as new rows in the first ndarray:
#Stacking Vertically
np.vstack((arr_A, arr_B))

Q8. How to swap two rows/columns in a 2D NumPy array?

  • create an array first:
arr = np.arange(16).reshape(4,4)
  • Let’s swap the first and the second rows:
arr[[1,0,2,3], :]
  • Let’s swap the first and the second columns of the original array:
arr[:, [1,0,2,3]]

Q9. Create a new array consisting of common values from two given arrays.

We need to find the positions where the elements of the two arrays match:

#Create two 1D arrays= np.array([1,2,3,2,3,4,3,4,5,6])= np.array([1,7,9,2,7,4,3,6,4,6])
np.where(== b)

Split a given array into four equal-sized sub-arrays.

  • For this, let’s create an 8×3 integer array:
arr = np.arange(20,44).reshape(8,3)
  • Now, you can create 4 sub-arrays using the split() function:
sub_arr = np.split(arr, 4) 


Hope this article will be helpful for your Data Science interview preparations. You can also find a similar article on Pandas Interview Questions.

Top Trending Articles:

Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is a Data Scientist | What is Data Analyst

Getting started with Python Strings
Getting started with Python Strings
Python strings are immutable, i.e., you cannot modify the contents of the object. In this article, we will briefly discuss how to create, assign string to a variable, how more
Array Manipulation Using NumPy
Array Manipulation Using NumPy
NumPy is a foundation library for scientific computations in Python, literally standing for Numerical Python. It contains sophisticated functions and tools for integrating with other programming languages as well. more
Slicing in Python
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio