Home Uncategorized NumPy – Arrays, Broadcasting, Linear Algebra, Mathematical Operations:...
Uncategorized

NumPy – Arrays, Broadcasting, Linear Algebra, Mathematical Operations: 20 Coding Interview Questions

NumPy (Numerical Python) is the foundation for numerical computing in Python. Arrays, broadcasting, and vectorized operations enable 100-1000x performance over loops. Mastering NumPy is essential for data science, machine learning, and scientific computing.

Arrays: The Core Data Structure

Array Basics: NumPy arrays are homogeneous (single type), typed collections stored contiguously in memory. Faster than Python lists (which store pointers). np.array() creates from lists, np.zeros(), np.ones(), np.arange(), np.linspace() for specific patterns. Shape: (rows, cols). dtype specifies type: int32, float64, bool. Multidimensional: 1D (vector), 2D (matrix), 3D+ (tensors).

import numpy as np
arr = np.array([1, 2, 3])
matrix = np.zeros((3, 4))
range_arr = np.arange(0, 10, 2)
linspace = np.linspace(0, 1, 5)
print(arr.shape, arr.dtype)

Indexing & Slicing: arr[0] first element, arr[-1] last. Slicing arr[1:3] returns elements 1-2 (exclusive end). Multidimensional: matrix[0, 1] element at row 0, col 1. Boolean indexing: arr[arr > 5] returns elements > 5. Fancy indexing: arr[[0, 2, 4]] returns specific indices. Slicing creates views (efficient), not copies.

arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])
print(arr[arr > 25])
matrix = np.arange(12).reshape(3, 4)
print(matrix[1, 2])

Broadcasting: Element-Wise Operations: Operations on arrays of different shapes automatically broadcast. Smaller shapes stretched to match. Critical rule: dimensions compared right-to-left. Incompatible dimensions raise error. Broadcasting avoids explicit loops, 100x faster. Examples: scalar + array, (3, 4) + (3, 1), (3,) + (1, 4).

a = np.array([1, 2, 3])
b = np.array([[1], [2], [3]])
result = a + b
matrix = np.arange(12).reshape(3, 4)
matrix * 2
matrix / matrix.sum()

Universal Functions (ufuncs): Element-wise operations. np.sqrt(), np.exp(), np.log(), np.sin(), np.abs(), np.round(). np.add(), np.multiply(), np.maximum(). Return new arrays, don’t modify originals (unless out parameter). Vectorized: 100-1000x faster than Python loops. Single operation across all elements.

arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))
print(np.log(arr))
print(np.maximum(arr, 8))
result = np.zeros_like(arr)
np.add(arr, 10, out=result)

Aggregation & Reduction: np.sum(), np.mean(), np.std(), np.min(), np.max() collapse to single value. axis parameter: axis=0 (columns), axis=1 (rows). keepdims=True preserves dimensions for broadcasting. Efficient: single pass, no loops.

matrix = np.arange(12).reshape(3, 4)
print(matrix.sum())
print(matrix.sum(axis=0))
print(matrix.mean(axis=1, keepdims=True))
print(matrix.std())

Linear Algebra & Matrix Operations: np.dot(), np.matmul() for matrix multiplication. @ operator (Python 3.5+) equivalent to matmul. np.linalg.inv() matrix inverse, np.linalg.det() determinant, np.linalg.eig() eigenvalues. np.transpose() swap axes. Reshape: changing dimensions without data copy (if memory-contiguous).

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
product = np.dot(A, B)
product2 = A @ B
print(np.linalg.det(A))
print(np.linalg.inv(A))

5 Complete Solved Coding Problems

Question 1: Broadcasting with Normalization

Matrix (1000, 50). Normalize each column: (x – mean) / std. Use broadcasting without loops. Output same shape.

import numpy as np
matrix = np.random.randn(1000, 50)
mean = matrix.mean(axis=0, keepdims=True)
std = matrix.std(axis=0, keepdims=True)
normalized = (matrix – mean) / std
print(normalized.shape)

Explanation: keepdims=True maintains (1, 50) shape. Broadcasting: (1000, 50) – (1, 50) broadcasts (1, 50) to (1000, 50). Efficient: no loops, C-level operations. Result: zero-mean, unit-variance columns.

Google – ML Engineer

Question 2: Vectorized Distance Calculation

Two arrays: X (1000, 3), Y (100, 3). Compute pairwise Euclidean distance. Output shape (1000, 100). No loops.

X = np.random.randn(1000, 3)
Y = np.random.randn(100, 3)
sq_X = (X ** 2).sum(axis=1, keepdims=True)
sq_Y = (Y ** 2).sum(axis=1, keepdims=True).T
xy = np.dot(X, Y.T)
distances = np.sqrt(sq_X + sq_Y – 2 * xy)
print(distances.shape)

Explanation: Broadcasting trick: ||x-y||² = ||x||² + ||y||² – 2x·y. sq_X: (1000, 1), sq_Y: (1, 100) after transpose. Broadcast to (1000, 100). Vectorized: 1000x faster than double loop.

Meta – ML Systems

Question 3: Matrix Multiplication with Linear Algebra

Two matrices A (50, 100), B (100, 75). Multiply A @ B. Find determinant of A.T @ A. Verify: (A @ B).shape.

A = np.random.randn(50, 100)
B = np.random.randn(100, 75)
product = A @ B
print(product.shape)
ATA = A.T @ A
det = np.linalg.det(ATA)
print(f”Determinant: {det}”)

Explanation: @ operator (matrix multiplication). (50, 100) @ (100, 75) = (50, 75). A.T @ A: (100, 50) @ (50, 100) = (100, 100) square. Determinant scalar. Linear algebra operations via np.linalg module.

Amazon – Data Scientist

Question 4: Element-Wise Operations & Aggregation

Array 1M elements. Apply sqrt, log, exp element-wise. Calculate overall mean, per-row mean (if 2D). No loops.

arr = np.abs(np.random.randn(1000000))
sqrt_arr = np.sqrt(arr)
log_arr = np.log(sqrt_arr + 1)
exp_arr = np.exp(log_arr)
mean_overall = exp_arr.mean()
matrix = arr.reshape(1000, 1000)
mean_rows = matrix.mean(axis=1)
print(mean_overall, mean_rows.shape)

Explanation: ufuncs apply element-wise in C. np.abs() ensures positive. log(x+1) avoids log(0). Aggregation axis parameter. Reshape (1000000,) to (1000, 1000). Pure vectorized: 100-1000x faster than loops.

Apple – ML Engineer

Question 5: Boolean Indexing & Conditional Operations

Array of floats. Find elements > 0.5, set to 1. Elements <= 0.5, set to 0. Use where(). No loops.

arr = np.random.rand(1000)
result = np.where(arr > 0.5, 1, 0)
print(result)
binary = np.where(arr > 0.5, 1, 0).astype(bool)
count_true = binary.sum()

Explanation: np.where(condition, true_val, false_val) element-wise conditional. Vectorized: evaluates all conditions in parallel. Result: same shape as input. astype() converts dtype. Binary operations efficient: no Python loops.

Microsoft – ML Engineer

15 Practice Questions (Test Your Understanding)

Question 6: Create array (100,). Reshape to (10, 10), (20, 5). Verify shape changes. Do reshapes share memory?

Question 7: Random matrix (1000, 100). Find max value per column using axis. Find row with largest sum.

Question 8: Two arrays: A (50, 50), B (50, 50). Compute A.T @ A, A @ B, A @ B.T. Output shapes?

Question 9: Array (1000,). Calculate rolling mean (window=10) without explicit loops. Hint: stride tricks or cumsum.

Question 10: Matrix (100, 100). Compute row-wise and column-wise normalization using broadcasting.

Question 11: Three arrays: X (1000, 10), Y (1000, 5), Z (1000, 3). Concatenate along axis=1. Output shape?

Question 12: Inverse of 3×3 matrix using np.linalg.inv(). Verify: result @ original ≈ identity matrix.

Question 13: Eigenvalue decomposition: A @ v = λ * v. Use np.linalg.eig(). Verify for 2×2 matrix.

Question 14: Compare speed: loop-based dot product vs np.dot() for 1M element vectors.

Question 15: Broadcasting fails: (3, 4) + (4, 5) incompatible. Why? Which dimension rules?

Question 16: Stochastic gradient: array (1000, 50). Compute gradients per sample with mean reduction. Use broadcasting.

Question 17: Array with NaN values. Count NaNs, replace with column mean. Compute mean, std skipping NaN.

Question 18: Softmax: exp(x) / sum(exp(x)) for matrix (100, 10) per row. Use broadcasting, avoid overflow.

Question 19: Batch matrix multiplication: (32, 50, 100) @ (32, 100, 75). Output shape? Use np.matmul().

Question 20: Generate 1M random samples from N(0,1). Compute percentiles (25, 50, 75). Visualize with histogram.

Master NumPy

Get our comprehensive Data Analytics: Ace All The Interview Concepts course.

Scroll to Top