Bird
Raised Fist0
NumPydata~20 mins

outer() for outer operations in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Outer Product Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of numpy.outer with simple vectors
What is the output of the following code using numpy.outer?
NumPy
import numpy as np

x = np.array([1, 2])
y = np.array([3, 4])
result = np.outer(x, y)
print(result)
A
[[3 4]
 [6 8]]
B
[[3 6]
 [4 8]]
C
[[4 3]
 [8 6]]
D
[[1 2]
 [3 4]]
Attempts:
2 left
💡 Hint
Remember that np.outer multiplies each element of the first array by each element of the second array.
❓ data_output
intermediate
1:30remaining
Shape of the output from np.outer with 1D arrays
Given two 1D numpy arrays a with shape (4,) and b with shape (3,), what is the shape of np.outer(a, b)?
NumPy
import numpy as np

a = np.arange(4)
b = np.arange(3)
result = np.outer(a, b)
print(result.shape)
A(3, 3)
B(4, 3)
C(4, 4)
D(3, 4)
Attempts:
2 left
💡 Hint
The outer product shape is (len(a), len(b)).
❓ visualization
advanced
2:30remaining
Visualizing the outer product matrix
Which option shows the correct heatmap visualization of np.outer([1, 2, 3], [4, 5])?
NumPy
import numpy as np
import matplotlib.pyplot as plt

matrix = np.outer([1, 2, 3], [4, 5])
plt.imshow(matrix, cmap='viridis')
plt.colorbar()
plt.show()
AA 3x2 heatmap with values [[4, 5], [8, 10], [12, 15]] increasing top to bottom and left to right
BA 2x3 heatmap with values [[4, 8, 12], [5, 10, 15]] increasing left to right and top to bottom
CA 3x2 heatmap with values [[1, 2], [3, 4], [5, 6]] increasing top to bottom and left to right
DA 2x3 heatmap with values [[1, 3, 5], [2, 4, 6]] increasing left to right and top to bottom
Attempts:
2 left
💡 Hint
Check the shape and values of the outer product matrix before visualizing.
🧠 Conceptual
advanced
2:00remaining
Understanding the difference between np.outer and np.dot
Which statement correctly describes the difference between np.outer and np.dot when applied to 1D numpy arrays?
A<code>np.outer</code> and <code>np.dot</code> produce the same output for 1D arrays.
B<code>np.outer</code> performs element-wise multiplication, while <code>np.dot</code> performs matrix multiplication.
C<code>np.outer</code> produces a scalar, while <code>np.dot</code> produces a matrix of pairwise products.
D<code>np.outer</code> produces a matrix of all pairwise products, while <code>np.dot</code> produces a single scalar sum of element-wise products.
Attempts:
2 left
💡 Hint
Think about the shapes of the outputs and what each function computes.
🔧 Debug
expert
2:30remaining
Identify the error in using np.outer with 2D arrays
What error will the following code produce and why?
import numpy as np

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])
result = np.outer(x, y)
print(result.shape)
NumPy
import numpy as np

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])
result = np.outer(x, y)
print(result.shape)
AValueError due to shape mismatch between 2D arrays
BTypeError because np.outer only accepts 1D arrays
CNo error; output shape is (4, 4) because inputs are flattened before outer product
DAttributeError because np.outer is not defined for numpy arrays
Attempts:
2 left
💡 Hint
Check how np.outer handles multi-dimensional arrays internally.

Practice

(1/5)
1. What does the numpy.outer() function do with two input arrays?
easy
A. It concatenates the two arrays into one.
B. It adds the two arrays element-wise.
C. It returns the dot product of the two arrays.
D. It computes the outer product, combining every element of the first array with every element of the second array.

Solution

  1. Step 1: Understand the purpose of numpy.outer()

    The function takes two arrays and pairs each element of the first array with each element of the second array.
  2. Step 2: Identify the output type

    The result is a 2D array showing all combinations, which is the outer product.
  3. Final Answer:

    It computes the outer product, combining every element of the first array with every element of the second array. -> Option D
  4. Quick Check:

    Outer product = combine all pairs [OK]
Hint: Outer product pairs all elements from both arrays [OK]
Common Mistakes:
  • Confusing outer product with dot product
  • Thinking it adds arrays element-wise
  • Assuming it concatenates arrays
2. Which of the following is the correct syntax to compute the outer product of arrays a and b using numpy?
easy
A. numpy.outer(a, b)
B. numpy.outerproduct(a, b)
C. numpy.outer(a * b)
D. numpy.outer(a + b)

Solution

  1. Step 1: Recall the function name and parameters

    The correct function is numpy.outer() and it takes two arrays as separate arguments.
  2. Step 2: Check the options for correct syntax

    Only numpy.outer(a, b) matches the correct function call with two arguments.
  3. Final Answer:

    numpy.outer(a, b) -> Option A
  4. Quick Check:

    Correct function call = numpy.outer(a, b) [OK]
Hint: Use numpy.outer(array1, array2) with two arguments [OK]
Common Mistakes:
  • Using wrong function name like outerproduct
  • Passing one combined argument instead of two
  • Using addition or multiplication inside outer()
3. What is the output of the following code?
import numpy as np
x = np.array([1, 2])
y = np.array([3, 4])
result = np.outer(x, y)
print(result)
medium
A. [[3 6] [4 8]]
B. [[3 4] [6 8]]
C. [[4 6] [8 12]]
D. [[1 2] [3 4]]

Solution

  1. Step 1: Calculate outer product manually

    Multiply each element of x by each element of y:
    1*3=3, 1*4=4
    2*3=6, 2*4=8
  2. Step 2: Form the 2D array

    Arrange results as rows for each element of x:
    [[3, 4],
    [6, 8]]
  3. Final Answer:

    [[3 4] [6 8]] -> Option B
  4. Quick Check:

    Outer product matches [[3 4], [6 8]] [OK]
Hint: Multiply each element of first array by all in second [OK]
Common Mistakes:
  • Mixing row and column order
  • Confusing with dot product output
  • Swapping elements of arrays
4. What is the output of the following code?
import numpy as np
x = np.array([1, 2, 3])
y = 5
result = np.outer(x, y)
print(result)
medium
A. [[5] [10] [15]]
B. [[5 10 15]]
C. [[1 2 3] [5]]
D. Throws an error

Solution

  1. Step 1: np.outer() handles scalar second argument

    It treats y=5 as a length-1 array, producing a (3,1) shaped array.
  2. Step 2: Compute the products

    [[1*5], [2*5], [3*5]] = [[5], [10], [15]]
  3. Final Answer:

    [[5] [10] [15]] -> Option A
  4. Quick Check:

    Scalar second arg -> column vector [OK]
Hint: np.outer accepts scalars, treating as 1-element array [OK]
Common Mistakes:
  • Thinking scalars cause errors
  • Expecting row vector instead of column
  • Confusing with concatenation
5. You have two arrays:
a = np.array([1, 0, 2])
b = np.array([3, np.nan])

You want to create a 2D array where each element is the product of elements from a and b, but you want to ignore zeros in a (replace their entire row with zeros). Which code correctly uses numpy.outer() and post-processing to achieve this?
hard
A. result = np.outer(a, b) result[:, b == 0] = 0
B. result = np.outer(a, b) result[a != 0, :] = 0
C. result = np.outer(a, b) result[a == 0, :] = 0
D. result = np.outer(a, b) result[b == 0, :] = 0

Solution

  1. Step 1: Compute outer product

    Using np.outer(a, b) creates a 2D array with all products.
  2. Step 2: Replace rows where a is zero with zeros

    Use boolean indexing result[a == 0, :] = 0 to zero out rows corresponding to zero elements in a.
  3. Final Answer:

    result = np.outer(a, b) result[a == 0, :] = 0 -> Option C
  4. Quick Check:

    Zero rows where a is zero [OK]
Hint: Zero rows where a is zero using boolean indexing [OK]
Common Mistakes:
  • Zeroing columns instead of rows
  • Using wrong condition for zeroing
  • Confusing axis in indexing
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