KatInterview

Pluggable Scoring Pipeline for Ranking Concert and Show Listings

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designstrategy-patternranking

Last updated

Design an object-oriented ranking engine where scoring and filtering rules can be chained, weighted, reordered and isolated from failures, with a backfill tier when too few results come back.

Build a Pluggable Ranking Pipeline for Live Events

You are building the recommendation component of a ticket-selling site. Given a user and a list of candidate events, it must return the best count events, ranked by a combined relevance score.

The ranking logic has to be modular. Individual rules must be easy to switch on or off, swap out, or reorder without touching the engine. Each request may carry different constraints, such as a location, a spending limit or a preferred category. Many requests will also return too few events from the main rules, so the engine needs a second tier of rules that tops up the list.

What to build

  1. A RecommendationRule abstraction exposing apply(user_id, event_list), which returns a mapping from event to score.
  2. A RecommendationEngine that stores rules, decides the order they run in, combines their scores, and handles the backfill tier.
  3. At least two concrete rules:
    • PopularityRule: higher score for events with more total ticket sales.
    • ProximityRule: higher score for events closer to the user's current position.

Skeleton

class Event:
    def __init__(self, id, category, price, popularity, lat, lon):
        self.id = id
        # ... remaining attributes

class RecommendationEngine:
    def __init__(self):
        # set up rule storage and fallback handling
        pass

    def add_rule(self, rule, priority):
        pass

    def get_recommendations(self, user_id, events, count):
        # return the top `count` events using the prioritized rules
        pass

You may extend add_rule with optional parameters (for example a weight and a flag marking a rule as fallback), as long as the two-argument call still works.

Example

Ten events are supplied, the user is at (40.7, -74.0), and count = 3. The engine runs ProximityRule with weight 0.7 and PopularityRule with weight 0.3. It returns three Event objects ordered by the weighted combination of the two scores.

Constraints

  • A single request may contain thousands of events.
  • Rules must be independent of each other and interchangeable.
  • If one rule fails (for example, a dependent service times out), the engine must carry on with the remaining rules instead of failing the request.

What to cover in your answer

Describe the class design, how scores from different rules become comparable, how priority and weights work, how the fallback tier is triggered, and how failures are contained. Give complexity and code, and mention edge cases.

Hints

Hint 1

Rules measure very different things (ticket counts, kilometers). Think about how to map every rule's output into the same range before mixing them with weights.

Hint 2

Let a rule express filtering simply by leaving an event out of its returned mapping. Then budget, category and radius limits are just more rules, and priority becomes the run order.

Hint 3

Wrap each rule call in a try/except. If a rule fails, skip it and exclude its weight from the denominator so scores from different requests stay comparable. Run the fallback tier only on events not already selected.

Hint 4

For top-k selection over thousands of events, a heap avoids a full sort. Break ties by input position so results are deterministic.