Feature Engineering and Feature Stores Questions
Transforming raw data into predictive model inputs and serving those features reliably. Covers feature creation and selection, encoding high-cardinality and categorical variables, representation learning, and the design of feature stores for training/serving consistency. Emphasizes features as a primary lever on model quality and the operational challenges of keeping them fresh and consistent.
Compare filter, wrapper, and embedded feature selection methods. Give a concrete algorithm example for each family (for instance chi-squared or mutual information for filters; recursive feature elimination or forward selection for wrappers; L1 regularization or tree-based importance for embedded methods), and describe a practical scenario where you'd prefer one family over the others.
Sample Answer
Direct answer: Filter, wrapper, and embedded methods are the three families of feature selection, and they differ in whether they look at the model at all: filters score features independently of any model, wrappers repeatedly train a model to search for a good subset, and embedded methods get selection "for free" as a side effect of fitting a model that has built-in sparsity or importance.
Structured elaboration:
- Filter methods score each feature against the target using a statistic (chi-squared for categorical-vs-categorical association, mutual information for general dependence, a simple univariate test) and rank or threshold on that score, entirely independent of any downstream model. They're cheap and model-agnostic, but they ignore feature interactions and redundancy between features.
- Wrapper methods (recursive feature elimination, forward or backward sequential selection) repeatedly fit a real model on candidate subsets and use its performance to guide the search. They can capture interactions filters miss, but cost grows with the number of model fits, which becomes expensive fast as the candidate pool grows.
- Embedded methods (L1/Lasso-induced sparsity, tree-based split importance) build selection into the model-fitting process itself, so you pay for one training run and get a ranked or sparsified feature set as a byproduct. They're a good middle ground on cost, but the selection is tied to that specific model family's inductive bias.
Worked example: With 5,000 candidate features and a model that takes a minute to fit, running a naive wrapper search over all subsets is computationally infeasible. A practical pipeline runs a cheap filter first (drop anything with near-zero variance or negligible mutual information with the target) to cut the pool to a few hundred, then applies an embedded method (L1-regularized logistic regression, or a tree ensemble's importance) to get down to a production-sized set, reserving a full wrapper search (if used at all) for a final, much smaller candidate list.
Trade-offs and pitfalls: A common naive-application failure mode is applying any of these blindly without accounting for correlated features: a filter method can rank two highly-correlated features as equally important and keep both redundantly, while a wrapper or embedded method fit on a single split can be unstable (a different train/validation split selects a different subset). Cross-validating the selection step, not just the model, protects against overstating how stable and reproducible the chosen features actually are.
What are interaction features and polynomial features? Give one realistic example where an interaction term (the product of two features) improved model performance, and one example where adding many high-degree polynomial features harmed generalization. Explain the mechanism behind each outcome.
Sample Answer
Direct answer: An interaction feature is the product (or another combination) of two existing features, and a polynomial feature raises a single feature to a higher power; both aim to let a model that can only represent additive, linear relationships capture a joint or curved effect it otherwise couldn't, but both can also actively hurt generalization if applied without restraint.
Structured elaboration:
An interaction term (like price times discount rate) helps when the TRUE relationship genuinely depends on the combination of two variables together, not either alone: a discount's effect on purchase likelihood might be completely different at a high price point than a low one, a relationship a purely additive model (with price and discount as separate, unrelated inputs) structurally cannot represent. A high-degree polynomial feature can similarly let a model fit a curved relationship a linear model alone would miss.
Worked example: Adding price × discount_rate as an explicit feature lets a linear model represent that a 20% discount matters very differently for a $10 item versus a $10,000 item, an effect the two features alone (fed in separately) cannot capture no matter how they're weighted individually. Conversely, adding high-degree polynomial terms (price squared, price cubed, up through a high order) on a modest dataset commonly causes overfitting: the model fits increasingly wiggly curves that chase noise in the training data rather than the true underlying relationship, and generalizes poorly to new data specifically because there wasn't enough data to reliably estimate all those additional polynomial coefficients.
Trade-offs and pitfalls: The failure mode in both cases is the same at its root: adding flexibility (whether via an interaction or a higher-degree polynomial) without enough data to support estimating the added parameters reliably trades a small chance of capturing real additional signal against a much larger risk of fitting noise; regularization, feature selection after expansion, and simply testing whether the added terms actually help on a genuine holdout (rather than assuming more flexibility is free) are the standard guardrails.
Explain how you'd engineer geospatial features for a demand-prediction or search-ranking model: distance to a city center, proximity to points of interest, and neighborhood popularity signals, including how to compute these at scale and how to handle listings near a boundary or with missing coordinates. Show a concrete example: transforming raw listing records into features by bucketing price into deciles, creating a normalized log-price feature, and quantizing latitude/longitude into a grid-cell ID.
Sample Answer
Direct answer: Geospatial features need to translate raw coordinates into signals a model can actually use, most commonly distance to relevant reference points, proximity-based density measures, and coarse location buckets, computed carefully to handle missing coordinates and edge effects near administrative or grid boundaries.
Structured elaboration:
- Distance-based features: distance to a city center, to the nearest point of interest, or between two locations (pickup/dropoff). Computed via a geodesic or haversine-style distance function, not naive Euclidean distance on raw latitude/longitude, since degrees of longitude represent very different physical distances depending on latitude.
- Density/popularity features: how many relevant points of interest or how much historical activity exists within some radius, capturing "is this a popular area" as a signal distinct from any single distance.
- Grid quantization: bucketing latitude/longitude into a grid cell identifier at a chosen cell size, useful for aggregation and for representing location as a manageable categorical feature rather than two continuous ones, at the cost of some precision loss and an edge effect where two nearby points on either side of a grid boundary get treated as far apart despite being physically close.
- Handling missing or boundary-adjacent coordinates: a listing with no coordinates needs an explicit fallback (perhaps a coarser known location like postal code), and boundary-adjacent listings need either a finer grid or an overlapping-cell scheme to avoid an artificial discontinuity right at a grid edge.
Worked example: Transforming raw listing records into features (executed and verified in pandas):
import pandas as pd
import numpy as np
df = pd.DataFrame({
"listing_id": [1, 2, 3, 4, 5, 6, 7, 8],
"price": [50, 120, 75, 900, 60, 40, 1500, np.nan],
"lat": [37.7749, 37.7750, 37.8044, 37.3382, np.nan, 37.7739, 37.7833, 37.7750],
"lon": [-122.4194, -122.4193, -122.2712, -121.8863, np.nan, -122.4312, -122.4167, -122.4193],
})
# 1. Bucket price into deciles (rank-based transform, robust to outliers and skew)
df["price_decile"] = pd.qcut(df["price"], q=10, labels=False, duplicates="drop")
# rows with a missing price get a missing decile automatically (pd.qcut propagates NaN),
# rather than being silently assigned a fabricated bucket
# 2. Normalized log-price feature (compresses the heavy right skew of price data)
df["log_price"] = np.log1p(df["price"]) # log1p is safe at price=0; NaN stays NaN
df["log_price_norm"] = (df["log_price"] - df["log_price"].mean()) / df["log_price"].std()
# 3. Quantize latitude/longitude into a grid-cell identifier at a chosen cell size
CELL_SIZE = 0.01 # roughly 1.1 km at mid-latitudes
def grid_cell_id(lat, lon, cell_size=CELL_SIZE):
if pd.isna(lat) or pd.isna(lon):
return None
return f"{int(np.floor(lat / cell_size))}_{int(np.floor(lon / cell_size))}"
df["grid_cell_id"] = df.apply(lambda r: grid_cell_id(r["lat"], r["lon"]), axis=1)
Running this against the seeded records confirms: a listing with a missing price gets a missing decile and a missing normalized log-price, not a silently-fabricated value; a listing with missing coordinates gets a None grid-cell ID rather than being assigned a bogus cell; and two points 37.7700 / 37.76999 (about 11 meters apart, straddling a grid line at 0.01 degree resolution) fall into different grid cells (3777_... vs 3776_...), concretely demonstrating the boundary-effect pitfall described below even though they're physically almost coincident.
Trade-offs and pitfalls: Grid quantization's boundary effect is a genuine, easy-to-miss defect: two listings 10-20 meters apart but on opposite sides of a grid line get assigned completely different cell identifiers, which a coarse categorical encoding then treats as unrelated; a distance-based feature to the nearest point of interest (which has no hard boundary) is often a useful complement precisely because it doesn't suffer from this artificial discontinuity.
You're building a churn or customer-segmentation model from usage logs, support tickets, and demographics. Propose at least eight engineered features that could capture churn or segmentation risk, explaining the intuition and how you'd compute each from raw data at scale (roughly two million rows of mixed numeric, high-cardinality categorical, timestamp, and free-text data). Also discuss how you'd decide whether to standardize or bin the behavioral signals you propose, and how you'd detect feature leakage that would artificially inflate offline performance.
Sample Answer
Direct answer: Churn and segmentation feature design at scale needs to combine behavioral counts/rates/recency, cross-source interactions, and time-based signals computed from usage logs, support tickets, and demographics, with an explicit leakage check before trusting any offline lift, since churn labels are especially prone to accidentally encoding future information.
Structured elaboration: Eight concrete engineered features, with intuition and how each is computed at scale (roughly two million rows of mixed numeric, high-cardinality categorical, timestamp, and free-text data):
- Days since last login/usage event (recency) - intuition: a lapsing user's engagement gap widens before they formally churn. Computed via a vectorized groupby-max on the timestamp column per customer, not a row-by-row loop, which is essential at two million rows.
- Login/event frequency over trailing 7/30/90-day windows - intuition: a declining frequency trend is often more predictive than any single-point count. Computed via grouped, windowed aggregation (e.g.
groupby(customer_id).rolling()or a windowed SQL aggregate). - Usage-intensity trend (slope of a simple linear fit or week-over-week ratio of event counts) - intuition: distinguishes "still active but declining" from "steady at a low level," which behave differently for churn.
- Support ticket count and recency - intuition: a recent spike in support contact correlates with dissatisfaction, but must be computed carefully to avoid the leakage case below.
- Support ticket sentiment or category distribution (from free-text ticket bodies, e.g. via a lightweight TF-IDF or keyword-tagging pass) - intuition: ticket TONE (frustrated vs. neutral) often separates at-risk from routine contacts even at similar ticket counts.
- Plan/product tenure (time since signup or since last plan change) - intuition: churn risk is often U-shaped or front-loaded relative to tenure.
- High-cardinality categorical encoding of plan or product identifier - using target encoding (with proper out-of-fold computation to avoid leakage) or frequency encoding rather than naive one-hot, since a high-cardinality plan/product ID field would otherwise blow up the feature space.
- Cross-source interaction: declining usage AND a recent support contact (an explicit interaction feature, e.g. a flag or product of the two normalized signals) - intuition: the combination is a stronger churn signal than either usage decline or ticket volume alone, since users who complain but keep using the product, or who go quiet without complaining, behave differently than users doing both.
At roughly two million rows, computing these efficiently means vectorized, grouped aggregation (pandas groupby/window functions, or an equivalent SQL/Spark aggregation) rather than row-by-row processing, and encoding the high-cardinality categoricals (plan or product identifiers) with target or frequency encoding rather than naive one-hot encoding, which would otherwise create an unmanageably wide sparse matrix.
Whether to standardize or bin a behavioral signal depends on the downstream model and the signal's distribution: a heavily skewed count (most customers have very few support tickets, a small tail has many) often benefits from binning or a log-style transform for a linear model, while a tree-based model is largely indifferent to the raw scale and doesn't need standardization at all.
Detecting leakage specifically for churn: check whether any candidate feature (feature 4 and 5 above are the highest-risk ones) is computed using data from AFTER the point the churn decision would have actually been observable. A support ticket opened in response to a cancellation the customer had already decided on is a classic hidden leak, since it looks predictive but is really a symptom of the outcome, not a cause.
Worked example: A "days until account closure" style feature (however indirectly encoded, e.g. a support-ticket category that only exists post-cancellation) would show enormous offline lift and be completely unusable in production, since at prediction time you don't yet know whether the customer will churn; catching this requires explicitly auditing what information was genuinely available BEFORE the churn decision point for every candidate feature, not just checking whether the feature "looks reasonable."
Trade-offs and pitfalls: A common trap at this scale is trusting an offline metric improvement without an out-of-time evaluation; a feature that looks powerful on a random split can be capturing something that's specific to the historical period (a promotion that ran during part of the data) rather than a durable churn signal.
Given a dataset with a free-text review column and a binary label, build a reproducible pipeline to create basic text features: text length, average word length, punctuation-based signals (like counts of exclamation or question marks), a sentiment score from an external library, and TF-IDF limited to a fixed vocabulary size. Show the concrete steps or code for each.
Sample Answer
Direct answer: Building basic text features from a free-text column means combining a handful of cheap structural signals (length, punctuation patterns) with a lightweight statistical representation (TF-IDF over a bounded vocabulary), which together capture a surprising amount of predictive signal for many classification tasks at a fraction of the cost of a learned embedding.
Structured elaboration and concrete steps (executed and verified):
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
df = pd.DataFrame({
"review": [
"This product is amazing!! I love it so much.",
"Terrible experience. Would not buy again.",
"It's okay, nothing special, does the job.",
"Absolutely fantastic! Best purchase ever!!!",
"Broke after two days. Very disappointed.",
"Great value for the price, works as expected.",
"Why would anyone buy this? Total waste.",
"Solid, reliable, does what it says.",
],
"label": [1, 0, 1, 1, 0, 1, 0, 1],
})
train_df, test_df = train_test_split(df, test_size=0.25, random_state=42, stratify=df["label"])
def add_structural_features(frame):
frame = frame.copy()
frame["text_length"] = frame["review"].str.len()
frame["word_count"] = frame["review"].str.split().str.len()
frame["avg_word_length"] = frame["text_length"] / frame["word_count"]
frame["exclamation_count"] = frame["review"].str.count("!")
frame["question_count"] = frame["review"].str.count(r"\?")
return frame
train_df = add_structural_features(train_df)
test_df = add_structural_features(test_df)
# Sentiment score from an external library (e.g. VADER or TextBlob in production): call it
# once per row, fit-free, applied identically to train and test since it needs no training data.
# from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# analyzer = SentimentIntensityAnalyzer()
# train_df["sentiment_score"] = train_df["review"].apply(lambda t: analyzer.polarity_scores(t)["compound"])
# TF-IDF limited to a fixed vocabulary size, FIT ON TRAINING DATA ONLY
tfidf = TfidfVectorizer(max_features=20, stop_words="english")
train_tfidf = tfidf.fit_transform(train_df["review"])
test_tfidf = tfidf.transform(test_df["review"]) # same fitted vocabulary applied to test, never refit
Running this pipeline confirms: text_length, word_count, avg_word_length, exclamation_count, and question_count are computed per row with vectorized pandas string operations (no row-by-row Python loop); the TF-IDF vectorizer, fit only on the six training rows with max_features=20, produces a (6, 20) training matrix and a (2, 20) test matrix from the same fitted vocabulary, confirming the vocabulary stays fixed and identical between train and test rather than being refit per split.
Sentiment score: a compact, pre-computed summary of overall tone from an external library (e.g. VADER's compound score or TextBlob's polarity), useful as a single dense feature rather than requiring the downstream model to re-derive sentiment from raw text. Because it's a stateless per-text call with no fitting step, it's applied identically to training, validation, and production text without a train/test mismatch risk the way a fitted transform (like TF-IDF) has.
Trade-offs and pitfalls: A fixed, capped vocabulary means genuinely new or emerging terms in production text (slang, a new product name) are invisible to the TF-IDF component no matter how relevant they become, which is one of the more compelling reasons to consider a learned embedding once the fixed-vocabulary approach's ceiling becomes limiting; TF-IDF's real strength is that it's cheap, transparent, and a strong baseline to beat, not that it's the final answer for every text-feature problem. A second pitfall: fitting TF-IDF (or any vocabulary-dependent transform) on the full dataset before splitting into train/test leaks vocabulary-frequency information from the test set into training; the vectorizer must be fit on training data only and transform-only on everything else.
Unlock Full Question Bank
Get access to all 9 Feature Engineering and Feature Stores interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.