01 / 04

How do you create a NumPy array?

np.array() function is used to create an array in numpy.

Heres how you can create an array:
Difficulty: 2/10
Topics: array creation methods, dtype specification, memory layout

Scenario Questions

0-2 years experience
  1. 1

    How would you create a 3x4 matrix filled with 7.0 as float32?

  2. 2

    You have a Python list [1,2,3] and a tuple (4,5,6). What's the simplest way to make a 2x3 array from them?

  3. 3

    What happens if you do np.array([[1,2], [3,4,5]])? What dtype do you get and why?

2-5 years experience
  1. 1

    You're reading a CSV with mixed numeric columns and some missing values marked as 'NA'. How do you create a clean float64 array without the 'NA' strings polluting the dtype?

  2. 2

    A teammate's code creates a large array via np.array(list_of_lists) and it's slow. You notice the input is already a list of numpy arrays. How would you fix this?

  3. 3

    Debugging: your array shows dtype=object but you expected float64. The input data came from a JSON load. Walk me through finding the root cause.

5-8 years experience
  1. 1

    You need to create a 50GB array on a machine with 64GB RAM for a one-time computation. The data comes from a memory-mapped file. Which creation method do you use and what parameters matter?

  2. 2

    Design a helper that efficiently builds a large array from a stream of variable-length batches (e.g., from a DataLoader). You can't know the final size upfront. What's your strategy?

  3. 3

    Your team sees frequent OOM errors when creating arrays from pandas DataFrames via .values. What's happening under the hood and how do you create the array zero-copy when possible?

8+ years experience
  1. 1

    You're defining array creation standards for a data platform used by 50 engineers across ML, analytics, and backend teams. What conventions do you enforce around dtype, memory order, and copy semantics?

  2. 2

    The org is migrating a critical pipeline from pandas to NumPy for latency reasons. The pipeline does heavy columnar transformations. How do you architect the array creation layer to be both performant and maintainable?

  3. 3

    A legacy system passes you raw C-contiguous buffers via FFI. You need to wrap them as NumPy arrays without copies, but the buffers may be freed externally. How do you design a safe ownership model?

Follow-up Questions

  • What happens if you pass a list of lists with different inner lengths?
  • When would you choose np.empty over np.zeros?
  • How do you create an array without copying data from a bytes buffer?