Card Authorization Report with Time-Activated Block Rules
Arrays & HashingAuto-graded · 19 test casesLast updated
Sort card authorization events by time and apply permanent, time-activated block rules to label each as APPROVE or REJECT, testing stable sorting, two-stream merging and hashing.
You are building a small payment-screening step. You receive a batch of card authorization attempts and a list of blocking rules. Each rule switches on at a specific moment and stays on for the rest of the timeline. Produce a chronological report that labels every attempt as accepted or declined.
Input
auth_events: a list of events, each of the form[timestamp_seconds, unique_id, amount, card_number, merchant].block_rules: a list of rules, each of the form[start_time, field, value].
The events may be given in any order, and so may the rules.
Rule semantics
- A rule with
start_time = tis active for every event whosetimestamp_seconds >= t. If a rule and an event share the same timestamp, the rule is already active for that event. - Rules never expire. Once active, a rule stays active for all later events.
- A rule matches an event when the event's value for the rule's
fieldis equal to the rule'svalue. - The valid field names are
timestamp_seconds,unique_id,amount,card_numberandmerchant. They correspond to the five positions of an event, in that order. - A rule whose
fieldis not one of those names is ignored. - An event is declined if at least one active rule matches it. Several matching rules still produce a single decline.
- A rule that starts after an event has no effect on that event.
Output
Return a list of strings, one per event, ordered by timestamp_seconds ascending. Events with equal timestamps keep the relative order in which they appear in auth_events.
Each string has the form:
"<timestamp_seconds> <unique_id> <amount> <STATUS>"
where the values are inserted using their normal string form and STATUS is REJECT if the event is declined, otherwise APPROVE.
If there are no events, return an empty list.
Constraints
0 <= len(auth_events) <= 10^50 <= len(block_rules) <= 10^5- Timestamps and rule start times are integers.
unique_id,card_numberandmerchantare strings.amountis a number.- Rule values are compared to event values using ordinary equality.
- The solution should run in roughly
O((n + m) log(n + m))time.
Examples
Example 1
Input: review_card_authorizations([[170, 'tx3', 30, 'card-b', 'store-a'], [100, 'tx1', 10, 'card-a', 'store-a'], [120, 'tx2', 20, 'card-b', 'store-b']], [[110, 'card_number', 'card-b'], [160, 'merchant', 'store-a']])
Output: ['100 tx1 10 APPROVE', '120 tx2 20 REJECT', '170 tx3 30 REJECT']
Example 2
Input: review_card_authorizations([[25, 'auth-2', 15, 'card-y', 'shop-b'], [10, 'auth-1', 5, 'card-x', 'shop-a']], [])
Output: ['10 auth-1 5 APPROVE', '25 auth-2 15 APPROVE']
Example 3
Input: review_card_authorizations([[50, 'auth-b', 12, 'card-z', 'shop'], [50, 'auth-a', 8, 'card-y', 'shop']], [[50, 'merchant', 'shop']])
Output: ['50 auth-b 12 REJECT', '50 auth-a 8 REJECT']
Hints
Hint 1
Sort the events by timestamp with a stable sort, so ties keep their original input order. Sort the rules by start time as well.
Hint 2
Walk through the sorted events once while keeping a pointer into the sorted rules. Before judging an event, activate every rule whose start time is less than or equal to the event's timestamp.
Hint 3
Keep, for each field name, a set of values blocked so far. An event is declined if any of its field values is in the matching set. Since rules never expire, nothing is ever removed.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a merge of two time-ordered streams.
- Sort the events by timestamp using a stable sort. Pairing each event with its original index guarantees that equal timestamps keep their input order.
- Sort the rules by start time.
- Walk through the sorted events once, with a pointer into the sorted rules. Before deciding an event, advance the pointer over every rule whose start time is at most the event's timestamp, and record each rule's value in a set kept per field (ignoring unknown fields).
- An event is declined if, for any of the five fields, its value is in that field's blocked set. Otherwise it is approved. Append the formatted line to the report.
Why it is correct
When an event with timestamp t is judged, the blocked sets contain exactly the rules with start time <= t. All of them have been added, and no later rule has. Events are visited in non-decreasing timestamp order, so the set of active rules only grows and nothing needs to be removed. This matches the rule that a rule is active from its start time onward, including events at the same instant. An event is declined exactly when some active rule's (field, value) pair matches it, which is the specification. The sets make duplicate rules and multiple matches harmless. Ignoring unknown fields matches the stated behavior.
Complexity
Let n be the number of events and m the number of rules.
- Time:
O(n log n + m log m)for sorting, plusO(n + m)for the merge. Each event does a constant number of set lookups, because there are five fields. - Space:
O(n + m)for the sorted copies, the blocked sets and the output.
A naive approach that compares each event with every earlier rule costs O(n * m). Hashing the blocked values avoids that.
Reference solution
from typing import List
def review_card_authorizations(auth_events: List[List[object]], block_rules: List[List[object]]) -> List[str]:
field_position = {
"timestamp_seconds": 0,
"unique_id": 1,
"amount": 2,
"card_number": 3,
"merchant": 4,
}
# Stable ordering by timestamp (index breaks ties to keep input order).
ordered_events = sorted(
enumerate(auth_events), key=lambda item: (item[1][0], item[0])
)
ordered_rules = sorted(block_rules, key=lambda rule: rule[0])
blocked = {}
rule_ptr = 0
report = []
for _, event in ordered_events:
ts = event[0]
# Activate every rule that has started by this event's timestamp.
while rule_ptr < len(ordered_rules) and ordered_rules[rule_ptr][0] <= ts:
_, field, value = ordered_rules[rule_ptr]
if field in field_position:
blocked.setdefault(field, set()).add(value)
rule_ptr += 1
declined = any(
event[pos] in blocked.get(field, ())
for field, pos in field_position.items()
)
status = "REJECT" if declined else "APPROVE"
report.append(f"{event[0]} {event[1]} {event[2]} {status}")
return report