Asked and Answered: 10 NumPy Interview Questions to Master

Written by Coursera Staff • Updated on

Are you considering a career that requires proficiency in NumPy? Data scientists, Python developers, business analysts, and other professionals benefit from having at least a working knowledge of NumPy, and other programming tools.

[Feature image] A woman interviews a candidate in the office meeting room.

Whether you’re already preparing for your interview or still deciding whether you want to learn NumPy, these 10 NumPy interview questions can help prepare you for your next steps. 

1. Briefly explain NumPy and why you might choose it.

Numerical Python, or 'NumPy,' offers high-end, powerful performance. It's a versatile, open-source package that you use to process arrays and perform broadcasting functions. It's flexible, functional, and fast. You can use NumPy to perform various mathematical operations, including trigonometry, algebraic and statistical calculations, deep learning, financial analysis, and scientific computing.

2. Why do data scientists and developers prefer NumPy over MATLAB or IDL?

The most straightforward answer is that NumPy is free, open-source, and uses Python. These qualities make it versatile for general programming while capable of complex scientific calculations and mathematical operations. Additionally, NumPy offers superior accessibility and performance. It includes multidimensional arrays and popular libraries such as SciPy and Scikit-Learn for statistics and machine learning. It also has PyTorch and TensorFlow for data manipulation.

3. Why would you use NumPy instead of Python’s lists?

One of NumPy’s most significant benefits over Python lists is its speed. NumPy provides multiple options for creating arrays and fast methods for manipulating data inside those arrays. NumPy is more compact, speedy, and consumes less memory. 

NumPy has static arrays with a fixed size. It performs advanced calculations using vectorised operations, which Python Lists doesn't offer. Additionally, because NumPy enables you to specify data types, you can optimise code further than with Python lists.

4. What’s the difference between vectorisation and broadcasting?

Vectorisation allows you to perform operations on arrays using integrated precompiled functions without loops. You will use operators such as ‘-’ or ‘+’ and the np.vectorise() function to increase program speed and utilise functions like np.dot. It’s performed within a single array.

Broadcasting allows you to perform arithmetic operations using the np.broadcast() function on different shapes or dimensions of arrays by broadcasting the dimensions of one to the other to make them the same shape. You can also use an element-by-element approach on arrays of the same shape.

NumPy offers many features, including its ability to work with different databases and the tools that allow you to integrate with C++ and C. Other helpful features include:

  • Interoperable with various hardware platforms and array libraries

  • Contains N-dimensional array objects and provides multidimensional arrays

  • Performs high-level mathematical functions, including linear algebra and statistics

  • Supports financial and scientific computations

  • Works with a variety of databases 

  • Offers broadcast functions

  • Vectorised coding in NumPy makes it possible to develop concise, readable codes that are easier to generate and contain fewer errors, even with lengthy coding or looping

6. Why would you use the shape property?

Each array in NumPy contains a set number of elements, which determine its shape. For example, if you have a two-dimensional NumPy array that contains three rows and four columns, its shape would be (3, 4)—the number of rows and columns. You can determine an array's current shape using the shape function— np.shape(). You can also use it to reshape the array and get the number of elements in the various dimensions within an array. 

7. How would you use NumPy’s random seed function?

The random seed function creates pseudo-random numbers in NumPy. These numbers look like they are generated randomly but are created using algorithms. Because a computer generates the numbers, you can create and recreate the same set repeatedly using the same input to generate the same output. This repeatability facilitates sharing code. 

Your input is the ‘seed’ the algorithm uses to create pseudo-random numbers. You use this function with other NumPy functions, including numpy.random.randint, which lets you create random integers, or numpy.random.choice, which enables you to create random samples from the input. 

8. Explain the difference between view and copy.

In NumPy, a copy is a new array that ‘owns’ its data. The original is unaffected by any changes you make to the copy, and the copy remains unaffected by changes to the original. You make a copy by duplicating metadata and the data buffer, a process that's relatively memory-consuming and slower than the view method.

To make a view, you use ndarray.view. In doing so, you can access the array by changing specific metadata without altering the data buffer. It provides a new way to look at the data using this new array, called a 'view.' Because the data buffer stays the same, any changes to the original are reflected in the view, and any changes made affect the original. 

9. What are the uses of vstack() and hstack() functions?

NumPy libraries have central data structures called arrays. These grids of values contain raw data and information to help locate or interpret elements. Both Vstack() and hstack() functions help merge arrays. Vstack() merges arrays vertically, while hstack() combines arrays horizontally. 

  • Syntax of vstack(): numpy.vstack(tup)

  • Syntax of hstack(): numpy.hstack(tup)

10. What is the SWIG method, and how could you use it?

SWIG stands for Simple Wrapper and Interface Generator. It's a tool that can create interfaces to multiple scripting languages and wrap large C libraries. Benefits include its support for various scripting languages, C++ support, and its ability to parse header files quickly. On the downside, it requires generating large code between Python and C and can be challenging because of its performance issues and the need for APIs.

You can use SWIG for a layered approach to building a Python extension module. The low-level wrappers would be created using C, while you can use Python code to create high-level features. In a case like this, SWIG offers flexibility and the ability to enhance extension modules using the language that will best accomplish various tasks.

Next steps 

Now that you’re more familiar with some of the possible NumPy interview questions (and their answers), are you ready to apply for jobs? If you need more of a refresher to ensure your skills are sharp, consider completing an online course like Data Analysis with Python, offered by IBM on Coursera. 

Suppose you’d like to prepare more for the interview process. In that case, you can pick up additional tips by completing a Guided Project like Accomplishment STAR Techniques for Job Interviews on the Coursera Project Network. 

Consider taking an online course, such as Advanced Interviewing Techniques, by the University of Maryland on Coursera.

Keep reading

Updated on
Written by:

Editorial Team

Coursera’s editorial team is comprised of highly experienced professional editors, writers, and fact...

This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.