Design Composable Event Recommendations
StubHubSoftware Engineer · OnsiteLast updated
This problem tests your ability to design a flexible, object-oriented recommendation engine that allows for composable filtering and ranking logic using the strategy or decorator pattern.
Problem Statement
You are building a recommendation engine for a ticket marketplace. Users have specific preferences, and events must be filtered and ranked based on these rules. You need to implement a system that evaluates an Event against a collection of Filter and Ranker strategies.
An Event is represented as follows:
class Event:
def __init__(self, id, category, price, popularity_score, start_time):
self.id = id
self.category = category
self.price = price
self.popularity_score = popularity_score
self.start_time = start_time
Tasks:
- Define an interface or abstract base class for
Filter(e.g.,CategoryFilter,PriceRangeFilter). - Define an interface for
Ranker(e.g.,PopularityRanker,PriceRanker). - Implement a
RecommendationEngineclass that takes a list ofEventobjects, applies all filters, and then sorts the remaining events based on a weighted combination of active rankers.
Example:
Input: A list of 3 events. Filter: CategoryFilter('Sports'). Ranker: PopularityRanker.
- Event A: {'id': 1, 'category': 'Sports', 'price': 50, 'popularity_score': 0.8}
- Event B: {'id': 2, 'category': 'Music', 'price': 30, 'popularity_score': 0.9}
- Event C: {'id': 3, 'category': 'Sports', 'price': 100, 'popularity_score': 0.7}
Output: [Event A, Event C] (filtered by 'Sports' and sorted by popularity).
Constraints:
- Use clean, extensible code (follow the Open/Closed Principle).
- Handle the case where no events match the filters.
- Ensure the ranking logic can handle multiple rankers simultaneously (e.g., a weighted score of popularity and price).
Hints
Hint 1
Consider using the Strategy Pattern to encapsulate the filtering and ranking algorithms as interchangeable objects.
Hint 2
For the ranking system, define a common scoring method that returns a float, allowing you to sum weighted results from multiple rankers.
Hint 3
Maintain a pipeline architecture where the engine processes a list of filters sequentially before passing the result to the ranker ensemble.
Follow-up questions
- How would you optimize the system if the number of events grew to millions and filtering had to happen in sub-100ms?
- How would you introduce 'diversity' into the top 10 recommendations to ensure the user doesn't just see the same category?
- If rankers have different scales (e.g., price is 0-500, popularity is 0-1), how would you normalize the scores before weighting them?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.