Chapter 3: NumPy Exercises
NumPy Exercises Set written in the style of a real teacher who really cares that you understand — not just copy-paste.
I divided it into difficulty levels so you can choose where you want to start or how deep you want to go.
Each question includes:
- clear task
- expected output shape / type when relevant
- small hint (try first without reading it!)
- full solution + detailed explanation + learning point
You should try every question yourself first — even if only for 2–5 minutes. The real learning happens when you struggle a little.
Level 1 – Warm-up & Fundamentals (very important to master)
Q1. Create five different NumPy arrays that all contain exactly the numbers 5, 8, 11, 14, 17, 20.
Use five different creation methods.
Q2. Create a 7×7 array filled with zeros. Then set every element on the border to 9 and every element on the main diagonal to 5.
Do it without loops and without hard-coded indices.
Q3.
|
0 1 2 3 4 5 6 |
arr = np.random.randint(-50, 150, size=(8, 12)) |
Without typing any numbers manually, print:
- shape
- number of dimensions
- total number of elements
- memory used by one element (in bytes)
- total memory used by the array (in bytes)
- data type
Q4. Create a 1D array containing 30 evenly spaced numbers between -4π and +4π (inclusive). Then compute sin and cos of every value and plot both curves on the same figure.
Use only NumPy functions — no matplotlib loops.
Q5. Create a 10×10 multiplication table (1×1 up to 10×10) using broadcasting only. Do not use np.meshgrid, np.outer, np.indices, or loops.
Level 2 – Indexing, Slicing, Masking (must master)
Q6.
|
0 1 2 3 4 5 6 |
a = np.arange(120).reshape(8, 15) |
Extract and print (show the sub-arrays):
a) rows 2, 5, 7 (in that order) b) columns 3, 7, 11, 14 c) the 4×5 submatrix from row 3–6 and column 5–9 (inclusive) d) the checkerboard pattern starting from position [0,0] (every second element) e) all elements that are divisible by 13
Q7. Given the same array a:
Replace all values divisible by 7 with -777 and all values greater than 100 with 999 in one single code cell (you may use multiple lines).
Q8. Create a boolean mask that selects all rows in a where at least two elements are greater than 90.
Then create a new array containing only those rows.
Q9.
|
0 1 2 3 4 5 6 |
data = np.random.randn(2000, 6) |
Keep only the rows where all values are between -2.5 and +2.5 (inclusive).
Then keep only the rows where at least one value is outside [-2, +2].
Show both resulting shapes.
Q10. Given a 1D array of 1000 random floats between -10 and 10:
Replace all values inside (-1, +1) with 0 and all values outside [-5, +5] with the nearest boundary (-5 or +5).
Do it in one expression (you may nest np.where).
Level 3 – Arithmetic, Broadcasting, Reductions (very common in practice)
Q11. Create a 15×15 array where element [i,j] = (i+1) × (j+1) using broadcasting only (no loops, no meshgrid, no outer).
Q12.
|
0 1 2 3 4 5 6 |
X = np.random.randn(5000, 25) |
Normalize each column to have:
- mean = 0
- standard deviation = 1
Do it in one line.
Then normalize each row the same way (one line).
Q13. Create a 100×100 array where:
element [i,j] = sin(2π × (i+1)/50) + cos(2π × (j+1)/30)
Do it without meshgrid and without loops.
Q14.
|
0 1 2 3 4 5 6 |
sales = np.random.randint(50, 500, size=(12, 8)) # 12 months × 8 products |
Compute:
- total sales per month (row sums)
- total sales per product (column sums)
- cumulative sales per month (cumulative sum along rows)
- percentage contribution of each product in each month (normalize columns per row)
Q15. Given a 1D array of 10000 random numbers:
Find how many values are:
- greater than their own row mean (wait — it’s 1D, so mean of whole array)
- within ±1 standard deviation from the mean
- outliers (more than 3 std away from mean)
Do it without loops.
Level 4 – Medium-Hard / Interview-style
Q16. Create a function that takes a 2D array and returns a boolean mask of the same shape where:
True = the element is a local maximum (strictly greater than all 4 direct neighbors: up, down, left, right) False = otherwise
Handle borders correctly (border elements have fewer neighbors).
Do not use loops — use array comparisons + padding or shifting.
Q17. Given a 1D array arr of length N:
Create a 2D view (not copy) of shape (N-k+1, k) where each row is a sliding window of k consecutive elements.
Example: k=4, arr=[10,20,30,40,50,60]
→ result shape (N-3, 4):
[[10 20 30 40] [20 30 40 50] [30 40 50 60]]
Do it without copying data (use strides if possible).
Q18.
|
0 1 2 3 4 5 6 |
img = np.random.randint(0, 256, (200, 300, 3), dtype=np.uint8) |
Increase contrast by multiplying each channel by 1.4 and subtracting 40, then clip to [0,255].
Do it in one expression and keep uint8 dtype.
Level 5 – Expert / Real-interview questions
Q19. Create a 100×100 array where:
element [i,j] = GCD of (i+1) and (j+1)
Do it without explicit double loop (use broadcasting + np.gcd).
Q20. Given a 1D array a of length N (N ≈ 1_000_000):
Compute the moving average over windows of size 50 (stride 1). Result should have length N−49.
Do it efficiently (no slow Python loops).
Q21. Given a 2D array X (samples × features):
For each row, replace the k largest values with 0, where k = 3.
Do it vectorized (no per-row loops).
Q22. Implement a vectorized moving maximum over a window of size w.
Given 1D array of length N, produce array of length N−w+1 where each position is the max of the next w elements.
Do not use scipy.ndimage or pandas rolling.
(You can use strides + np.maximum.reduce)
How to use this quiz
-
Pick a level or a few questions
-
Try to solve them without looking at solutions
-
When you’re ready, say for example:
- “Show solutions for Q1–Q10”
- “I want detailed solution only for Q16–Q22”
- “Give hints for Q19–Q22 first”
- “Make even harder questions”
- “Explain broadcasting in Q11 and Q13 more”
I’m here — tell me how you want to proceed! We can go question by question, or do whole sections. 😊
