Implement a Composable Event Recommendation Engine
StubHubSoftware Engineer · OnsiteLast updated
This problem tests your ability to design a flexible, object-oriented recommendation pipeline using the Strategy and Composite patterns to handle multiple scoring rules and fallback logic.
Problem Statement
Design an event recommendation engine for a ticketing platform that calculates a final 'relevance score' for events. The system must support composable ranking rules that can be enabled, disabled, or reordered dynamically.
Each recommendation request may include different constraints (e.g., location, budget, or event category). Implement a system that allows chaining these rules together and supports a 'fallback' mechanism: if the primary rules yield too few events, the system should execute secondary rules to backfill the results.
Requirements
- Define a
RecommendationRuleinterface with a methodapply(user_id, event_list) -> scored_events. - Implement a
RecommendationEngineclass that manages a collection of rules and fallback logic. - Implement at least two concrete rules:
PopularityRule: Scores events based on global ticket sales.ProximityRule: Scores events based on distance from the user's current location.
Interface
class Event:
def __init__(self, id, category, price, popularity, lat, lon):
self.id = id
# ... other attributes
class RecommendationEngine:
def __init__(self):
# Initialize rules and fallback strategies
pass
def add_rule(self, rule, priority):
pass
def get_recommendations(self, user_id, events, count):
# Return top N events based on prioritized rules
pass
Example
- Input: A list of 10 events, a user located at (40.7, -74.0), and a
countof 3. - Logic: Apply
ProximityRule(weight 0.7) andPopularityRule(weight 0.3). - Output: A list of 3
Eventobjects sorted by the weighted sum of scores.
Constraints
- The engine must handle thousands of events per request.
- Rules must be independent and interchangeable.
- If a specific rule fails (e.g., service timeout), the system should continue to the next available rule.
Hints
Hint 1
Use the Strategy pattern to encapsulate each ranking algorithm within its own class.
Hint 2
Consider using a weighted scoring model where each rule returns a dictionary of score updates, which the engine then aggregates.
Hint 3
Implement the 'fallback' as a secondary list of rules that is only triggered if the size of the filtered results falls below the requested 'count'.
Follow-up questions
- How would you modify this system to support A/B testing different sets of rules for different user segments?
- If calculating scores becomes computationally expensive, how would you cache or optimize the rule execution?
- How would you incorporate a 'diversity' rule to ensure the final list doesn't contain only events from a single category?
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.