Predicting Rising Products and Serving Them in a Personalized Feed
Machine LearningModel answerLast updated
Design a daily forecasting pipeline that spots products about to surge in sales, plus a sub-100ms personalized homepage feed that surfaces them, including cold-start handling.
Spotting Tomorrow's Hot Products
An online marketplace wants a homepage module that highlights products just before they take off. The goal is to flag items whose sales will accelerate strongly over the coming week, then show them to the shoppers most likely to care.
What the system must do
- Forecast. Detect products with an upward trajectory before they reach peak volume. "Hot" means high sales velocity during the next 7 days.
- Recommend. Present the flagged products on the homepage, tailored to different user segments.
- Refresh cadence. The forecasting pipeline reruns once per day. The serving path must respond in under 100 ms.
Scale and constraints
- About 1 million feed requests per day.
- About 500,000 active SKUs in the catalog.
- Results must reflect each user's past behavior and category preferences.
What to cover in your answer
- A high-level architecture spanning data ingestion, model training, and online serving.
- A data model for item features and user-item interactions.
- How you would handle brand-new products that have no sales history.
- The trade-offs between precomputing scores in batch and scoring in real time.
State your assumptions (label definition, user counts, peak traffic) and explain why you made each design choice.
Hints
Hint 1
Separate the problem into two parts: a batch forecaster that scores every SKU once a day, and a lightweight online ranker that personalizes a small pool of trending items per request.
Hint 2
Raw sales volume only finds items that are already popular. Think about features that capture momentum, such as windowed counts, day-over-day change, acceleration, and category-relative comparisons.
Hint 3
For new items, combine content-based priors (text, image, brand, price), shrinkage toward category averages, and a small exploration budget in the feed so the system can learn quickly.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Overview
Treat this as two systems connected by a shared score and feature store:
- Trend forecaster (offline, daily). Scores all 500K SKUs for the likelihood of a strong next-7-day sales surge.
- Personalized ranker (online, low latency). Takes the top trending pool and orders it for each user.
Decoupling them lets the expensive modeling run in batch while serving stays a fast lookup plus a small ranking step.
1. Requirements and scale
- Define the label. For example, positive if units sold in days t+1..t+7 land in the top 1% of the item's category. Alternatively regress on
log(sales_next7 / sales_prev7). Per-category normalization prevents high-volume categories from dominating. - Traffic. 1M requests/day is about 12 QPS on average, perhaps 120 QPS at peak. Throughput is easy; latency is the real constraint.
- Batch size. Scoring 500K SKUs daily with a gradient-boosted model takes minutes on one machine.
- Assumptions. About 5M monthly users and about 50M events/day. Target p99 under 100 ms, with ranking itself under about 30 ms.
2. API
GET /v1/feed/trending?user_id=U&limit=20 returns items with score, a short reason string, model version, and a request_id. The request ID lets impressions and clicks be joined back for training and experiments.
3. Data model
| Store | Key | Contents |
|---|---|---|
| Raw events (Kafka to data lake, date-partitioned) | event_id | user, sku, type (view/cart/purchase), timestamp, session, price |
| Daily item aggregates (warehouse) | (sku, date) | views, carts, purchases, revenue, conversion, stock, price |
| Item features (offline + online store) | sku | 1/3/7/14/28-day counts, velocity, acceleration, 3d/28d ratio, category z-score, search growth, wishlist adds, content embedding, age in days |
| Trend scores (KV, versioned) | sku | probability of surge, predicted units, category, uncertainty |
| Trending candidates (KV) | category | top-N SKUs by score |
| User profile (online KV) | user | category affinity, embedding, last ~50 items, price band |
Velocity and acceleration are what capture "rising but not yet peaked"; raw volume alone finds items that have already peaked.
4. Architecture
Clickstream and orders flow into Kafka, then a lake. Spark jobs build daily features into the offline store, while Flink maintains streaming counters in the online store. A weekly training job fits the trend model, and a daily batch job scores all SKUs and writes scores plus per-category candidate lists to the KV store. At request time the feed service:
- Fetches the user profile.
- Generates about 300 candidates from the user's top categories plus an ANN lookup of the user embedding against the trending pool.
- Ranks them with a small model.
- Applies diversity and business rules such as stock availability.
Trend model. Start with LightGBM on windowed tabular features. Each training row is (item, date t) with the label from t+1..t+7. Use time-based splits to avoid leakage. Sequence models can come later; GBDT is a strong baseline.
Ranker. A small GBDT or MLP predicts click or purchase probability using the user-item embedding similarity, category affinity, surge probability, price fit, and recency. Final score is roughly P(purchase) × f(p_surge), with the weighting tuned online. Anonymous users get a geo-level or global trending list diversified across categories.
Latency budget. KV multi-get about 5 ms, ANN over about 10K items about 5 ms, scoring 300 candidates about 10 ms: roughly 30 ms total. A precomputed per-segment list serves as a fallback if the ranker times out.
5. Cold start for new items
New products have no history yet are often the future winners. Layer several tactics:
- Content priors. Embed title, description, and images; combine with brand, price, category, and seller track record. Use early trajectories of similar items as features.
- Shrinkage. Smooth sparse signals toward category priors, e.g.
(purchases + α·prior_cr) / (views + α), so 2 sales from 3 views does not look like a breakout. - Early signals. Hours since launch, first-day views relative to comparable launches, wishlist adds, search-term growth.
- Exploration. Reserve 5-10% of feed slots for new items, chosen with Thompson sampling on click-through, which also generates training data.
- Intraday boost. Streaming counters can promote a promising new item into the pool before the next daily run.
For new users, show geo-popular and trending items, then shift to session-based affinity after a few clicks.
6. Batch vs. real-time scoring
| Daily batch | Real-time | |
|---|---|---|
| Freshness | Up to 24h stale; misses intraday spikes | Minutes |
| Cost/complexity | Cheap, easy to debug and backfill | Streaming infra, online/offline feature consistency |
| Latency risk | KV lookup only | Model inference on the critical path |
Recommendation: batch for the trend score (the 7-day horizon does not need minute-level freshness), real-time for the personalization step, plus a streaming boost for new or spiking items. This hybrid keeps the hot path cheap while capturing most of the freshness benefit.
7. Evaluation and monitoring
- Offline: precision@K and recall@K for surge detection per category, PR-AUC, calibration, and a backtest across several weeks.
- Online: A/B test on CTR, add-to-cart, conversion, and revenue per session. Watch diversity and the share of exposure given to new items.
- Monitoring: feature drift, score distribution shifts, stale batch runs, and a fallback to yesterday's scores if the job fails.
Common mistakes
- Ranking by raw sales volume, which recommends items that are already popular.
- Leaking future data into training via random rather than time-based splits.
- Ignoring feedback loops: showing items makes them sell more. Log impressions and use exploration or propensity correction.
- Putting heavy model inference on the request path when batch lookup would do.
- Forgetting stock levels, so the feed promotes sold-out items.
- No plan for new items or anonymous users.