Data Science

Data Cleaning Interview Questions and Answers

Common data cleaning interview questions and answers — handling missing values, outliers, duplicates, inconsistent formats, and feature scaling — with practical pandas examples.

Meritshot Team10 min read
Data CleaningInterview QuestionsData SciencePandasPython
Back to Blog

Data Cleaning Interview Questions and Answers

Ask any working data scientist how they spend their week and you will hear the same answer: most of it goes into cleaning data, not building models. That is exactly why data cleaning shows up in almost every data analyst and data scientist interview in India — from startups in Bengaluru to analytics teams at banks and consulting firms. Interviewers want to know that you can take a messy, real-world dataset and make it trustworthy.

This guide collects the most common data cleaning interview questions, each with a clear, correct answer and practical pandas examples you can actually use on the job.

Fundamentals

What is data cleaning and why does it matter?

Data cleaning (also called data cleansing or data wrangling) is the process of detecting and correcting errors, inconsistencies, and inaccuracies in a dataset so that downstream analysis and models can be trusted. It covers missing values, duplicates, outliers, wrong data types, inconsistent formatting, and structural problems.

It matters because of a simple principle: garbage in, garbage out. Even the best machine learning algorithm produces misleading results when trained on dirty data. Clean data improves model accuracy, makes business reports reliable, and prevents costly decisions based on wrong numbers.

What are the main steps in a typical data cleaning workflow?

A practical workflow usually follows these steps:

  1. Inspect the data — understand shape, data types, and summary statistics.
  2. Handle missing values — delete or impute.
  3. Remove duplicates — drop exact or near-duplicate rows.
  4. Fix data types and formats — dates, numbers stored as text, inconsistent casing.
  5. Detect and treat outliers — using statistical rules.
  6. Standardize categorical values — merge "Del" and "Delhi" into one label.
  7. Validate — confirm the cleaned data meets expected rules.
import pandas as pd

df = pd.read_csv("customers.csv")
df.info()            # data types and non-null counts
df.describe()        # summary statistics for numeric columns
df.isnull().sum()    # missing values per column

Handling Missing Values

How do you detect missing values in pandas?

Use isnull() (or its alias isna()) combined with sum() to count missing entries per column, and mean() to get the proportion missing.

df.isnull().sum()              # count of NaNs per column
df.isnull().mean() * 100       # percentage missing per column

When should you delete rows versus impute missing values?

This is a classic judgement question. General guidance:

  • Delete rows (dropna()) when only a small fraction of rows have missing values and the loss will not bias your data.
  • Delete a column when the vast majority of its values are missing and it carries little signal.
  • Impute when the data is valuable, the missing share is meaningful, or dropping would shrink the dataset too much.

The key is to first understand why the data is missing. Values missing completely at random are safer to drop than values missing for a systematic reason, which can introduce bias.

What imputation strategies do you know?

StrategyWhen to use
MeanNumeric data with a roughly symmetric distribution and no strong outliers
MedianNumeric data that is skewed or has outliers
ModeCategorical data (fill with the most frequent category)
Forward / backward fillTime series, where nearby values are good estimates
KNN imputationWhen missing values relate to patterns across multiple features
Constant / flagWhen "missing" is itself meaningful (fill with 0 or "Unknown")
df["age"].fillna(df["age"].median(), inplace=True)       # median for skewed numeric
df["city"].fillna(df["city"].mode()[0], inplace=True)    # mode for categorical
df["sales"].fillna(method="ffill", inplace=True)         # forward fill for time series

When is median preferred over mean for imputation?

Prefer the median when the column is skewed or contains outliers. The mean is pulled toward extreme values — a few very high salaries, for instance, inflate the average — while the median stays near the centre of the distribution and gives a more representative fill value.

What is KNN imputation?

KNN (K-Nearest Neighbours) imputation fills a missing value by looking at the k most similar rows (based on the other features) and averaging their values for that column. It is more sophisticated than mean or median because it uses relationships between features, but it is slower on large datasets and sensitive to feature scaling.

from sklearn.impute import KNNImputer

imputer = KNNImputer(n_neighbors=5)
df_numeric = pd.DataFrame(imputer.fit_transform(df_numeric),
                          columns=df_numeric.columns)

Duplicates

How do you find and remove duplicate records?

Use duplicated() to flag duplicates and drop_duplicates() to remove them. You can check duplicates across all columns or a subset that defines a unique record.

df.duplicated().sum()                          # count exact duplicate rows
df = df.drop_duplicates()                       # drop exact duplicates
df = df.drop_duplicates(subset=["email"],       # dedupe on a key column
                        keep="first")

Watch out for near-duplicates — records that are logically the same but differ in spacing or case, such as "Rahul Sharma " and "rahul sharma". Standardize the text first, then deduplicate.

Outliers

What is an outlier, and how do you detect one?

An outlier is a data point that lies far from the rest of the distribution. Outliers can be genuine (a real high-value transaction) or errors (age recorded as 999). Two standard detection methods:

  • IQR method — compute the interquartile range (Q3 − Q1) and flag values below Q1 − 1.5 × IQR or above Q3 + 1.5 × IQR.
  • Z-score method — flag values whose z-score (distance from the mean in standard deviations) exceeds a threshold, commonly 3.
Q1 = df["income"].quantile(0.25)
Q3 = df["income"].quantile(0.75)
IQR = Q3 - Q1
lower, upper = Q1 - 1.5 * IQR, Q3 + 1.5 * IQR
outliers = df[(df["income"] < lower) | (df["income"] > upper)]

When would you use z-score instead of IQR?

Use the z-score method when the data is approximately normally distributed, because it assumes a symmetric bell curve. Use the IQR method when the data is skewed or non-normal, since it is based on quartiles and does not assume any distribution. IQR is generally the safer default for messy real-world data.

How do you treat outliers once detected?

There is no single correct answer — the right choice depends on context:

  • Remove them if they are clearly data-entry errors.
  • Cap / winsorize them to a sensible boundary instead of deleting.
  • Transform the column (for example, a log transform) to compress extreme values.
  • Keep them if they are legitimate and important, such as fraud cases.

Always investigate before deleting — an outlier is sometimes the most valuable row in the dataset.

Formats, Types, and Categories

How do you fix inconsistent formats and wrong data types?

Common problems are numbers stored as text, dates in mixed formats, and inconsistent casing. Convert types with astype(), parse dates with pd.to_datetime(), and standardize strings with str methods.

df["price"] = df["price"].astype(float)
df["signup_date"] = pd.to_datetime(df["signup_date"], errors="coerce")
df["city"] = df["city"].str.strip().str.title()   # "  delhi " -> "Delhi"

errors="coerce" turns unparseable values into NaT/NaN instead of raising an error, so you can handle them consistently as missing data.

What is the difference between label encoding and one-hot encoding?

Both convert categorical variables into numbers so models can use them.

  • Label encoding assigns each category an integer (Red = 0, Green = 1, Blue = 2). It is compact but implies an artificial order, so it suits ordinal data (Low, Medium, High) and tree-based models.
  • One-hot encoding creates a separate binary column per category. It avoids implying order, which makes it the right choice for nominal data used with linear models, but it increases dimensionality.
# One-hot encoding
df = pd.get_dummies(df, columns=["city"], drop_first=True)

# Label encoding
from sklearn.preprocessing import LabelEncoder
df["size"] = LabelEncoder().fit_transform(df["size"])

How do you handle high-cardinality categorical features?

A high-cardinality feature has many unique values — pincodes, product IDs, or user names. One-hot encoding these creates thousands of columns. Practical approaches:

  • Group rare categories into a single "Other" bucket.
  • Use frequency or target encoding, replacing each category with a statistic.
  • Keep only the top-N most frequent categories and bucket the rest.

Scaling and Imbalance

What is the difference between normalization and standardization?

Both put numeric features on a comparable scale, which helps distance-based and gradient-based algorithms.

AspectNormalization (Min-Max)Standardization (Z-score)
Formula(x − min) / (max − min)(x − mean) / std
Output rangeUsually 0 to 1Centred at 0, unit variance
Sensitive to outliersYesLess so
Good forKNN, neural networksLinear/logistic regression, SVM, PCA
from sklearn.preprocessing import MinMaxScaler, StandardScaler

df[cols] = MinMaxScaler().fit_transform(df[cols])       # normalization
df[cols] = StandardScaler().fit_transform(df[cols])     # standardization

How do you handle imbalanced data?

When one class heavily outnumbers another (common in fraud or churn problems), models tend to ignore the minority class. Techniques include:

  • Oversampling the minority class (for example, SMOTE, which generates synthetic samples).
  • Undersampling the majority class.
  • Using class weights so the model penalizes minority errors more.
  • Evaluating with precision, recall, and F1 instead of plain accuracy, which is misleading on imbalanced data.

Text Cleaning and Validation

What are the common steps in cleaning text data?

Text cleaning prepares raw strings for NLP. Typical steps:

  1. Lowercasing for consistency.
  2. Removing punctuation and special characters.
  3. Removing stopwords (common words like "the", "is").
  4. Tokenization — splitting text into words.
  5. Stemming or lemmatization — reducing words to their base form.
import re
text = "Great Product!! Loved it."
text = text.lower()
text = re.sub(r"[^a-z\s]", "", text)   # remove punctuation and digits

What is data validation and why does it matter?

Data validation is the final check that cleaned data obeys the rules your analysis expects: ages fall in a plausible range, emails match a valid pattern, categorical columns contain only allowed values, and totals reconcile. Validation catches problems that slipped through cleaning and prevents them from silently corrupting reports or model training.

assert df["age"].between(0, 120).all()          # sanity check on range
assert df["email"].str.contains("@").all()      # basic format check

Wrapping Up

Data cleaning is not the glamorous part of data science, but it is the part interviewers probe hardest — because it is what you will actually do most days. If you can explain when to impute versus drop, why median beats mean on skewed data, and how IQR differs from z-score, you will stand out from candidates who only memorized model names.

The best preparation is hands-on practice on real, messy datasets. Download a few from Kaggle, break them further, and clean them end to end. To go deeper into the full pipeline from cleaning to modelling, explore our AI and Data Science course or browse more tutorials on the Meritshot blog. Clean data is the quiet foundation every good model stands on — master it, and the rest of data science gets a lot easier.

Recommended