NumPy / Pandas / scikit-learn interview questions

25 questions from the NumPy / Pandas / scikit-learn course, each with the correct answer and an explanation. Try to answer before you open the answer.

NumPy Arrays

1. What do all elements of a NumPy array share?

  1. A Python object type per element
  2. A single value
  3. A single dtype
  4. Nothing in particular
Show answer

Answer: A single dtype

One dtype enables fast, compact storage.

Lesson: Creating and Shaping Arrays

2. Can a (2, 3) array be added to a (3,) array?

  1. Yes, the (3,) array is broadcast across each row
  2. No, shapes must be identical
  3. Only if both are float
  4. Only with a loop
Show answer

Answer: Yes, the (3,) array is broadcast across each row

Trailing dimensions match.

Lesson: Vectorised Maths and Broadcasting

3. Which operation returns a view sharing memory with the original?

  1. np.copy(a)
  2. A boolean mask
  3. Fancy indexing with a list
  4. A basic slice such as a[1:4]
Show answer

Answer: A basic slice such as a[1:4]

Slices are views; masks and lists copy.

Lesson: Indexing, Masks, Views and Copies

Computing With NumPy

4. What does scores.mean(axis=0) return for a students-by-exams matrix?

  1. The mean of each student (row)
  2. The mean of each exam (column)
  3. A single overall mean
  4. The median
Show answer

Answer: The mean of each exam (column)

axis=0 collapses rows.

Lesson: Aggregation and Random Numbers

6. Why can a looped sum and np.dot differ slightly?

  1. Loops round to integers
  2. np.dot is wrong
  3. Python ints are inexact
  4. They add the numbers in a different order, and floating-point addition is not exactly associative
Show answer

Answer: They add the numbers in a different order, and floating-point addition is not exactly associative

Compare floats with a tolerance.

Lesson: Vectorisation Instead of Loops

pandas Fundamentals

7. What does each DataFrame column have?

  1. Its own dtype
  2. The same dtype as all other columns
  3. No dtype
  4. A separate index per row
Show answer

Answer: Its own dtype

Columns are typed Series.

Lesson: Series and DataFrames

8. Which selects rows by integer position?

  1. query
  2. loc
  3. iloc
  4. at with labels
Show answer

Answer: iloc

i for integer position.

Lesson: Selecting Rows and Columns

9. What does an empty numeric field in a CSV become after read_csv?

  1. An empty string in a float column
  2. 0
  3. NaN
  4. An error
Show answer

Answer: NaN

Missing values become NaN.

Lesson: Reading and Writing Data

Cleaning Data With pandas

10. What does df.dropna() do by default?

  1. Drops every row that has any missing value
  2. Fills missing values with 0
  3. Drops columns only
  4. Nothing unless inplace=True
Show answer

Answer: Drops every row that has any missing value

Often too aggressive; use subset.

Lesson: Handling Missing Values

11. What does errors="coerce" do in pd.to_numeric?

  1. Converts to strings
  2. Raises an error for bad values
  3. Deletes the column
  4. Turns unparseable values into NaN
Show answer

Answer: Turns unparseable values into NaN

Bad values become missing.

Lesson: Types, Strings and Categories

12. In pandas 3, how do you set column b to 0 where a > 1 in df?

  1. df[df["a"] > 1]["b"] = 0
  2. df.loc[df["a"] > 1, "b"] = 0
  3. subset = df[df["a"] > 1]; subset["b"] = 0
  4. df.b.iloc = 0
Show answer

Answer: df.loc[df["a"] > 1, "b"] = 0

One loc call on the original.

Lesson: Copy-on-Write in pandas 3

Analysing Data With pandas

13. Which groupby method returns results aligned to the original rows?

  1. agg
  2. transform
  3. size
  4. describe
Show answer

Answer: transform

Same length as the input.

Lesson: Grouping and Aggregating

14. Which join keeps every order even if the customer is unknown?

  1. concat with axis=1
  2. An inner join
  3. A right join from orders
  4. A left join from orders
Show answer

Answer: A left join from orders

Left keeps all left rows.

Lesson: Merging and Concatenating

15. What happens to duplicate (month, city) pairs in pivot_table?

  1. They are combined with the aggregation function
  2. They raise an error
  3. Only the first is kept
  4. They become new columns
Show answer

Answer: They are combined with the aggregation function

pivot_table aggregates; pivot would fail.

Lesson: Pivot Tables and Reshaping

16. What does resample("W") do by default?

  1. Shifts dates by a week
  2. Keeps one row per weekday
  3. Aggregates into weeks ending on Sunday
  4. Removes weekends
Show answer

Answer: Aggregates into weeks ending on Sunday

Weekly bins, W-SUN.

Lesson: Working With Time Series

Machine Learning With scikit-learn

17. Why use stratify=y in train_test_split?

  1. To remove outliers
  2. To make training faster
  3. To keep class proportions the same in train and test sets
  4. To scale features
Show answer

Answer: To keep class proportions the same in train and test sets

Stratification preserves class balance.

Lesson: The Estimator API

18. Which metric measures how many actual positives the model found?

  1. Recall
  2. Precision
  3. Accuracy
  4. Support
Show answer

Answer: Recall

Recall = found positives / all positives.

Lesson: Classification Metrics

19. What does an R² of 0 mean?

  1. The data has no variance
  2. The model is perfect
  3. The model predicts zero
  4. The model is no better than predicting the mean
Show answer

Answer: The model is no better than predicting the mean

R² compares to the mean baseline.

Lesson: Regression and Baselines

Pipelines and Model Selection

20. What does handle_unknown="ignore" do in OneHotEncoder?

  1. Drops the whole row
  2. Encodes unseen categories as all zeros instead of raising an error
  3. Adds a new column at predict time
  4. Raises a warning and stops
Show answer

Answer: Encodes unseen categories as all zeros instead of raising an error

Robust to new categories.

Lesson: Pipelines and ColumnTransformer

21. Which splitter suits data ordered in time?

  1. Random KFold with shuffling
  2. TimeSeriesSplit
  3. StratifiedKFold
  4. LeaveOneOut on shuffled data
Show answer

Answer: TimeSeriesSplit

Never train on the future.

Lesson: Cross-Validation

22. How do you name the C parameter of an SVC step called "svc" in a pipeline grid?

  1. pipeline_C
  2. C
  3. svc.C
  4. svc__C
Show answer

Answer: svc__C

step__parameter.

Lesson: Hyperparameter Search

Avoiding Pitfalls and Shipping Models

23. How do you prevent leakage from feature selection or scaling?

  1. Put them in a Pipeline so they are fitted only on training folds
  2. Apply them to all data before splitting
  3. Skip cross-validation
  4. Use a larger test set only
Show answer

Answer: Put them in a Pipeline so they are fitted only on training folds

Fit preprocessing on training data only.

Lesson: Data Leakage

24. Why should you never load a pickled model from an untrusted source?

  1. It deletes training data
  2. It is always too large
  3. Unpickling can execute arbitrary code
  4. It changes the model's accuracy
Show answer

Answer: Unpickling can execute arbitrary code

Pickle is not a safe format for untrusted input.

Lesson: Saving and Loading Models

25. Which item belongs on a data science checklist?

  1. Scale data before splitting
  2. Tune hyperparameters on the test set
  3. Compare the model to a dummy baseline
  4. Ignore missing values
Show answer

Answer: Compare the model to a dummy baseline

A baseline shows whether the model adds value.

Lesson: A Data Science Workflow Checklist

← Back to the course