Bird
Raised Fist0
NumPydata~20 mins

np.vstack() and np.hstack() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stacking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.vstack() with 1D arrays
What is the output of this code?
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.vstack((arr1, arr2))
print(result)
NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.vstack((arr1, arr2))
print(result)
A
[[1 4]
 [2 5]
 [3 6]]
B
[[1 2 3]
 [4 5 6]]
C[1 2 3 4 5 6]
D
[[1 2 3]
 [4 5 6]
 [1 2 3]]
Attempts:
2 left
💡 Hint
np.vstack stacks arrays vertically, adding rows.
❓ Predict Output
intermediate
2:00remaining
Output of np.hstack() with 2D arrays
What is the output of this code?
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.hstack((arr1, arr2))
print(result)
NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.hstack((arr1, arr2))
print(result)
A[[1 2 3 4 5 6 7 8]]
B
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
C
[[1 5]
 [2 6]
 [3 7]
 [4 8]]
D
[[1 2 5 6]
 [3 4 7 8]]
Attempts:
2 left
💡 Hint
np.hstack stacks arrays horizontally, adding columns.
❓ data_output
advanced
2:00remaining
Shape after stacking arrays with np.vstack and np.hstack
Given these arrays:
import numpy as np
arr1 = np.array([[1, 2, 3]])
arr2 = np.array([[4, 5, 6]])

What is the shape of the result after applying np.vstack((arr1, arr2)) and np.hstack((arr1, arr2)) respectively?
NumPy
import numpy as np
arr1 = np.array([[1, 2, 3]])
arr2 = np.array([[4, 5, 6]])
vstack_result = np.vstack((arr1, arr2))
hstack_result = np.hstack((arr1, arr2))
print(vstack_result.shape, hstack_result.shape)
A(2, 3) and (1, 6)
B(2, 6) and (1, 3)
C(3, 2) and (6, 1)
D(1, 6) and (2, 3)
Attempts:
2 left
💡 Hint
vstack adds rows, hstack adds columns.
🔧 Debug
advanced
2:00remaining
Error when stacking arrays with incompatible shapes
What error does this code raise?
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[4, 5, 6]])
result = np.vstack((arr1, arr2))
NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[4, 5, 6]])
result = np.vstack((arr1, arr2))
ATypeError: unsupported operand type(s) for +: 'int' and 'list'
BNo error, runs successfully
CValueError: all the input arrays must have same number of dimensions
DIndexError: index out of range
Attempts:
2 left
💡 Hint
np.vstack requires arrays to have the same number of dimensions.
🚀 Application
expert
3:00remaining
Combining arrays for data analysis
You have two numpy arrays representing data from two sensors:
sensor1 = np.array([[10, 20], [30, 40]])
sensor2 = np.array([[50, 60], [70, 80]])

You want to create a single array where sensor2 data is stacked below sensor1 data, and then another array where sensor2 data is stacked to the right of sensor1 data.
Which code correctly creates these two arrays?
NumPy
import numpy as np
sensor1 = np.array([[10, 20], [30, 40]])
sensor2 = np.array([[50, 60], [70, 80]])
A
vertical_stack = np.concatenate((sensor1, sensor2), axis=0)
horizontal_stack = np.concatenate((sensor1, sensor2), axis=1)
B
vertical_stack = np.hstack((sensor1, sensor2))
horizontal_stack = np.vstack((sensor1, sensor2))
C
vertical_stack = np.vstack((sensor1, sensor2))
horizontal_stack = np.hstack((sensor1, sensor2))
D
vertical_stack = np.concatenate((sensor1, sensor2), axis=1)
horizontal_stack = np.concatenate((sensor1, sensor2), axis=0)
Attempts:
2 left
💡 Hint
axis=0 stacks rows (vertical), axis=1 stacks columns (horizontal).

Practice

(1/5)
1. What does np.vstack() do with arrays?
easy
A. Stacks arrays vertically, adding rows
B. Stacks arrays horizontally, adding columns
C. Flattens arrays into one dimension
D. Splits arrays into smaller parts

Solution

  1. Step 1: Understand vertical stacking

    np.vstack() stacks arrays by adding rows one below another.
  2. Step 2: Compare with horizontal stacking

    np.hstack() stacks arrays side by side by adding columns, so it is different.
  3. Final Answer:

    Stacks arrays vertically, adding rows -> Option A
  4. Quick Check:

    np.vstack() = vertical stack [OK]
Hint: Remember 'v' in vstack means vertical stacking [OK]
Common Mistakes:
  • Confusing vertical and horizontal stacking
  • Thinking vstack flattens arrays
  • Assuming vstack splits arrays
2. Which of the following is the correct syntax to horizontally stack two arrays a and b using NumPy?
easy
A. np.hstack(a, b)
B. np.hstack[a, b]
C. np.hstack((a, b))
D. np.hstack{a, b}

Solution

  1. Step 1: Check function call syntax

    NumPy functions use parentheses and tuples for multiple arrays, so np.hstack((a, b)) is correct.
  2. Step 2: Identify incorrect syntax

    Square brackets, missing parentheses, or curly braces are invalid for function calls in Python.
  3. Final Answer:

    np.hstack((a, b)) -> Option C
  4. Quick Check:

    Correct function call uses parentheses and tuple [OK]
Hint: Use double parentheses for multiple arrays in hstack [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Passing arrays without tuple parentheses
  • Using curly braces which are invalid
3. What is the output of the following code?
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])
result = np.vstack((x, y))
print(result)
medium
A. [[1 2 5 6] [3 4 7 8]]
B. [[1 2 3 4] [5 6 7 8]]
C. [[1 2] [5 6] [3 4] [7 8]]
D. [[1 2] [3 4] [5 6] [7 8]]

Solution

  1. Step 1: Understand np.vstack behavior

    It stacks arrays vertically, so rows of y come below rows of x.
  2. Step 2: Combine arrays row-wise

    Result has 4 rows: first two from x, next two from y, each with 2 columns.
  3. Final Answer:

    [[1 2] [3 4] [5 6] [7 8]] -> Option D
  4. Quick Check:

    Vertical stack adds rows below [OK]
Hint: vstack adds rows; hstack adds columns [OK]
Common Mistakes:
  • Confusing vstack with hstack output
  • Expecting horizontal concatenation
  • Misreading array shapes
4. What error will occur when running this code?
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[4, 5, 6], [7, 8, 9]])
result = np.hstack((a, b))
print(result)
medium
A. No error, outputs concatenated array
B. TypeError because inputs are not lists
C. SyntaxError due to wrong function call
D. ValueError due to incompatible shapes

Solution

  1. Step 1: Check shapes of arrays

    a is shape (3,), b is shape (2,3). They differ in first dimension.
  2. Step 2: Understand hstack requirements

    np.hstack() stacks arrays along their second axis if possible. Here, a is 1D and b is 2D, but NumPy broadcasts a to shape (1,3) and stacks along axis 1, resulting in shape (3,3).
  3. Step 3: Result

    No error occurs; arrays are concatenated.
  4. Final Answer:

    No error, outputs concatenated array -> Option A
  5. Quick Check:

    NumPy broadcasts 1D array and concatenates [OK]
Hint: NumPy can broadcast 1D arrays when stacking [OK]
Common Mistakes:
  • Assuming hstack requires same number of rows
  • Confusing ValueError with TypeError
  • Ignoring NumPy broadcasting rules
5. Given two arrays:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6, 7], [8, 9, 10]])
Which method correctly combines them into a single array with shape (2, 5) by stacking horizontally?
hard
A. np.vstack((a, b))
B. np.hstack((a, b))
C. np.hstack((a.T, b.T)).T
D. np.vstack((a.T, b.T)).T

Solution

  1. Step 1: Check shapes of arrays

    a shape is (2, 2), b shape is (2, 3). Both have 2 rows, so horizontal stacking is possible.
  2. Step 2: Apply np.hstack

    np.hstack((a, b)) concatenates columns, resulting in shape (2, 5).
  3. Step 3: Verify other options

    np.vstack((a, b)) would fail due to different column counts; transposes complicate shape and are unnecessary here.
  4. Final Answer:

    np.hstack((a, b)) -> Option B
  5. Quick Check:

    Same rows, hstack adds columns [OK]
Hint: For same rows, use hstack to add columns [OK]
Common Mistakes:
  • Using vstack with different column counts
  • Overcomplicating with transpose
  • Mixing up row and column dimensions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard