Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / NumPy Array Operations
interview

Q1. Scenario: You have a numpy array of temperatures in Celsius: [0, 20, 30, 40]. Convert to Fahrenheit using vectorized operation. (F = C*9/5 + 32).
c = np.array([0,20,30,40]); f = c * 9/5 + 32. Output: [32., 68., 86., 104.]. No loop needed. Vectorized operations are applied element-wise, fast and concise.

Q2. Scenario: You have two arrays a = [10,20,30], b = [1,2,3]. Compute a/b, a**b, and np.exp(a).
a/b = [10,10,10]; a**b = [10^1=10, 20^2=400, 30^3=27000]; np.exp(a) = [e^10, e^20, e^30] very large. Universal functions (ufuncs) like np.exp, np.log, np.sin apply element-wise. Used for activation functions (sigmoid, tanh).

Q3. Scenario: Compute the sum, cumulative sum, mean, and standard deviation of an array [1,2,3,4,5] using NumPy methods.
arr = np.arange(1,6); s = arr.sum(); cs = arr.cumsum(); m = arr.mean(); std = arr.std(). Results: sum=15, cumsum=[1,3,6,10,15], mean=3, std≈1.414. These are descriptive statistics.

Q4. Scenario: You have a 2D array representing scores of 3 students in 4 subjects: [[85,90,88,92],[78,85,80,86],[92,95,90,94]]. Compute the mean score per student (row-wise) and per subject (column-wise).
scores = np.array(...); student_means = scores.mean(axis=1); subject_means = scores.mean(axis=0). axis=1 across columns, axis=0 across rows. Output: student_means ~ [88.75, 82.25, 92.75]; subject_means ~ [85,90,86,90.67].

Q5. Scenario: Clip values in an array to a range [0,1]. For example, arr = [-0.5, 0.2, 1.3, 0.7]. Use np.clip.
clipped = np.clip(arr, 0, 1) -> [0., 0.2, 1., 0.7]. Useful for bounding probabilities or gradients. Also can do arr[arr<0]=0; arr[arr>1]=1 but clip is easier.