Ways to create NumPy arrays (np.array, zeros, ones, arange, linspace) and when to use each

Quick Overview

Asks for the main ways to create NumPy arrays, including np.array, zeros, ones, arange and linspace, and when each one fits. Tests practical NumPy fluency: dtype defaults, pre-allocation, and the difference between building a range by step size and by number of points.

Ways to create NumPy arrays (np.array, zeros, ones, arange, linspace) and when to use each

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Describe different ways to create NumPy arrays (for example `np.array`, `np.zeros`, `np.ones`, `np.arange`, `np.linspace`), and when you might choose one over another. ```hint Group them by what you start from Sort the constructors by their input: values you already have, a shape you know in advance, or a range defined either by a step size or by a number of points. ``` ### What a Strong Answer Covers - Converting existing Python data with `np.array`, including how the dtype is inferred - Pre-allocation with `zeros`, `ones`, `empty` and `full`, and when uninitialized memory is acceptable - How `arange` (step size, exclusive stop) differs from `linspace` (point count, inclusive endpoint) - Practical pitfalls such as floating-point steps and default dtypes - A clear rule for picking a constructor in a given situation ### Follow-up Questions - Why can `np.arange` with a fractional step return one more element than you expect, and what would you use instead? - What is the difference between `np.array` and `np.asarray`, and when does it matter for memory? - Why is calling `np.append` inside a loop slow, and what pattern replaces it? - How would you create an array with the same shape and dtype as an existing one, or an identity matrix?

Overview: Asks for the main ways to create NumPy arrays, including np.array, zeros, ones, arange and linspace, and when each one fits. Tests practical NumPy fluency: dtype defaults, pre-allocation, and the difference between building a range by step size and by number of points.

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

Describe different ways to create NumPy arrays (for example np.array, np.zeros, np.ones, np.arange, np.linspace), and when you might choose one over another.

What a Strong Answer Covers Guidance

  • Converting existing Python data with np.array , including how the dtype is inferred
  • Pre-allocation with zeros , ones , empty and full , and when uninitialized memory is acceptable
  • How arange (step size, exclusive stop) differs from linspace (point count, inclusive endpoint)
  • Practical pitfalls such as floating-point steps and default dtypes
  • A clear rule for picking a constructor in a given situation

Follow-up Questions Guidance

  • Why can np.arange with a fractional step return one more element than you expect, and what would you use instead?
  • What is the difference between np.array and np.asarray , and when does it matter for memory?
  • Why is calling np.append inside a loop slow, and what pattern replaces it?
  • How would you create an array with the same shape and dtype as an existing one, or an identity matrix?