KatInterview

Card Authorization Report with Time-Activated Block Rules

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashinghashingsortingsimulation

Last 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 = t is active for every event whose timestamp_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 field is equal to the rule's value.
  • The valid field names are timestamp_seconds, unique_id, amount, card_number and merchant. They correspond to the five positions of an event, in that order.
  • A rule whose field is 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^5
  • 0 <= len(block_rules) <= 10^5
  • Timestamps and rule start times are integers.
  • unique_id, card_number and merchant are strings. amount is 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.