Data Analyst Interview
Menu
Browse in your language. All mock interviews, preparation sessions and feedback are in English only.
Python · Learning guide

NumPy arrays: shapes, broadcasting and validation

Predict the output shape before running a numerical operation, then check the values on a small example.

Start the worked examples ↓

Add one adjustment per column

Here rows are stores and columns are products. The adjustment has one value for each product. Broadcasting applies it to each row without requiring a manually repeated input array.

import numpy as np
sales = np.array([[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]])
column_adjustment = np.array([1.0, 2.0, 3.0])
adjusted = sales + column_adjustment
assert adjusted.shape == (2, 3)
assert np.allclose(adjusted, [[11, 22, 33], [41, 52, 63]])
print(sales.sum(axis=0))  # [50. 70. 90.]
print(sales.sum(axis=1))  # [ 60. 150.]

The result has two rows and three columns. Summing axis 0 leaves one total per product; summing axis 1 leaves one per store.

Check shape compatibility

Compare dimensions from the right: matching sizes or a size of one can broadcast. To add one value per store to this (2, 3) array, reshape two values to (2, 1). A plain (2,) array does not match the trailing dimension of three.

store_adjustment = np.array([5.0, 10.0]).reshape(2, 1)
assert np.allclose(sales + store_adjustment, [[15, 25, 35], [50, 60, 70]])

Treat dtype and missingness deliberately

Fixed-width integer operations can overflow. Floating-point arithmetic is approximate, so use an appropriate tolerance for numeric checks. mean and nanmean answer different questions when NaN is present; ignoring missing observations is an assumption, not a repair.

Basic slicing commonly returns a view, while advanced indexing returns a copy. If you need an independent result before modification, make the copy explicit and check memory implications.

Explore outliers without deleting them automatically

import numpy as np
prices = np.array([120, 150, 130, 800, 145, 160, 155])
q1, q3 = np.percentile(prices, [25, 75])
iqr = q3 - q1
flagged = prices[(prices < q1 - 1.5 * iqr) | (prices > q3 + 1.5 * iqr)]
assert flagged.tolist() == [800]
print(flagged)

The $800 value is flagged by this rule, not proven incorrect. Investigate its context. Record the percentile method when reproducibility across tools matters.

Performance questions to ask

Array operations can avoid Python-level loops, but there is no universal 50× or 100× speedup. Measure the relevant workload, include allocation costs and inspect memory use. For random simulations, prefer an explicit generator such as np.random.default_rng(42), and record the library version.

Continue learning

Reference: NumPy broadcasting · NumPy indexing. Check behaviour for your installed version.

These are educational exercises with fictional data, not a verified employer question bank. Amounts in examples are USD unless otherwise stated.

Practise your explanation

Use the self-practice guide to review your approach. Mock-interview booking and payment are currently inactive. All planned preparation sessions and feedback are in English.

View preparation options
✉ WhatsApp