Explain how NumPy combines and splits arrays: concatenate, stack, vstack, hstack, split, vsplit, hsplit

Quick Overview

Asks how NumPy's concatenate, stack, vstack and hstack join arrays and how split, vsplit and hsplit divide them. Tests whether you can predict the shape of each result, tell an existing axis from a new one, and handle 1-D inputs and uneven splits.

Explain how NumPy combines and splits arrays: concatenate, stack, vstack, hstack, split, vsplit, hsplit

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the different methods for combining (`concatenate`, `stack`, `vstack`, `hstack`) and splitting (`split`, `vsplit`, `hsplit`) NumPy arrays. These functions come up whenever you assemble feature matrices or batch data, and mixing them up is a common source of shape bugs. ```hint Track the shapes For each function, write down the input shapes and the output shape, and check whether the number of dimensions changes. ``` ### What a Strong Answer Covers - The difference between joining along an existing axis and joining along a new axis, and how `ndim` changes in each case. - The shape requirements each combining function puts on its inputs. - What `vstack`, `hstack` and `hsplit` do with 1-D inputs compared with 2-D inputs. - How `split` takes either a number of sections or a list of cut indices, what happens when the count does not divide the axis, and where `np.array_split` fits. - A small example with concrete input and output shapes. ### Follow-up Questions - How would you build a 2-D array whose columns are three 1-D arrays of length n? - Why is calling `np.concatenate` inside a loop to grow an array slow, and what would you do instead? - Do the pieces returned by `np.split` share memory with the original array, and what happens if you write into one of them? - What is the inverse of `np.stack`?

Overview: Asks how NumPy's concatenate, stack, vstack and hstack join arrays and how split, vsplit and hsplit divide them. Tests whether you can predict the shape of each result, tell an existing axis from a new one, and handle 1-D inputs and uneven splits.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the different methods for combining (concatenate, stack, vstack, hstack) and splitting (split, vsplit, hsplit) NumPy arrays.

These functions come up whenever you assemble feature matrices or batch data, and mixing them up is a common source of shape bugs.

What a Strong Answer Covers Guidance

  • The difference between joining along an existing axis and joining along a new axis, and how ndim changes in each case.
  • The shape requirements each combining function puts on its inputs.
  • What vstack , hstack and hsplit do with 1-D inputs compared with 2-D inputs.
  • How split takes either a number of sections or a list of cut indices, what happens when the count does not divide the axis, and where np.array_split fits.
  • A small example with concrete input and output shapes.

Follow-up Questions Guidance

  • How would you build a 2-D array whose columns are three 1-D arrays of length n?
  • Why is calling np.concatenate inside a loop to grow an array slow, and what would you do instead?
  • Do the pieces returned by np.split share memory with the original array, and what happens if you write into one of them?
  • What is the inverse of np.stack ?