Track Click-to-Conversion Matching with Time Windows
Data Structure DesignAuto-graded · 20 test casesLast updated
Tests event stream processing with time-based lookback windows, requiring efficient per-user state management and timestamp-based expiration logic.
You are building a system to match user clicks with conversion events. The system processes a chronological stream of events, where each event is either a CLICK or a CONVERSION for a particular user at a specific timestamp.
Your task is to match conversions to clicks according to these rules:
- When a
CONVERSIONevent arrives for a user, it should match the most recentCLICKevent for that same user that occurred within a lookback window - The lookback window extends from
conversion_timestamp - lookback_window_mstoconversion_timestamp(inclusive on both ends) - Each
CLICKcan only be matched once; after a click is consumed by a conversion, it becomes unavailable - If no valid click exists within the window, the conversion remains unmatched
Implement the function match_conversion_events that processes a sequence of events and returns a result for each event:
- For
CLICKevents: return-1 - For
CONVERSIONevents: return theuser_idif a matching click was found and consumed, otherwise return-1
Input:
lookback_window_ms: an integer representing the maximum time difference (in milliseconds) for a valid matchoperations: a list of event types, each either"CLICK"or"CONVERSION"arguments: a list where each element is[user_id, timestamp]corresponding to each operation
Output:
- A list of integers, one result per event in order
Constraints:
- Events arrive in non-decreasing timestamp order
1 ≤ lookback_window_ms ≤ 10^91 ≤ number of events ≤ 10^61 ≤ user_id ≤ 10^90 ≤ timestamp ≤ 10^15- All inputs are valid integers
Examples
Example 1
Input: match_conversion_events(1000, ['CLICK', 'CONVERSION', 'CONVERSION'], [[1, 1000], [1, 1500], [1, 2500]])
Output: [-1, 1, -1]
Example 2
Input: match_conversion_events(500, ['CLICK', 'CLICK', 'CONVERSION', 'CONVERSION'], [[7, 10], [7, 20], [7, 20], [7, 20]])
Output: [-1, -1, 7, 7]
Hints
Hint 1
Maintain separate state for each user to track their click history
Hint 2
Since events arrive in timestamp order, older clicks that fall outside the window can be safely discarded
Hint 3
A deque allows efficient removal from both ends: prune expired clicks from the front, consume the latest click from the back
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to maintain a separate queue of click timestamps for each user. Since events arrive in chronological order, each user's queue naturally stays sorted by timestamp.
For each event, we process it in three steps:
-
Prune expired clicks: Remove all clicks from the front of the user's queue that fall before
timestamp - lookback_window_ms. These clicks can never match any future conversion since time only moves forward. -
Handle the event:
- If it's a
CLICK: append the timestamp to the user's queue and return-1 - If it's a
CONVERSION: check if any valid clicks remain. If so, remove the most recent one (from the back of the queue) and return theuser_id. Otherwise return-1
- If it's a
This works because:
- The queue maintains clicks in chronological order
- After pruning, all remaining clicks are within the valid window
- The back of the queue holds the most recent valid click, which is exactly what we need to match
- Removing a click when it's consumed ensures single-use semantics
Why It Works
The algorithm correctly handles all edge cases:
- Multiple conversions can consume multiple clicks in newest-to-oldest order
- A click at exactly
timestamp - lookback_window_msis valid (inclusive boundary) - Once consumed, a click cannot be reused
- Users without clicks correctly return
-1for conversions
Complexity
Time Complexity: O(N) amortized across all N events. Each click is added once and removed at most once (either by expiration or consumption). Pruning work is distributed across events.
Space Complexity: O(U + C) where U is the number of distinct users with active clicks and C is the total number of unexpired, unconsumed clicks stored.
Solution
from collections import defaultdict, deque
from typing import List
def match_conversion_events(lookback_window_ms: int, operations: List[str], arguments: List[List[int]]) -> List[int]:
user_clicks = defaultdict(deque)
results = []
for event_type, (user_id, timestamp) in zip(operations, arguments):
click_queue = user_clicks[user_id]
earliest_valid = timestamp - lookback_window_ms
# Remove clicks that are too old to be valid
while click_queue and click_queue[0] < earliest_valid:
click_queue.popleft()
if event_type == "CLICK":
click_queue.append(timestamp)
results.append(-1)
else: # CONVERSION
if click_queue:
click_queue.pop()
results.append(user_id)
else:
results.append(-1)
return results