KatInterview

Coupon Usage Caps: Check Remaining Redemptions per User

Arrays & HashingAuto-graded · 16 test cases
EasyAuto-gradedArrays & Hashinghash-mapcountingaggregation

Last updated

Count past coupon redemptions per (user, coupon) pair from a log, then answer many read-only queries on whether each user is still under the coupon's cap.

Coupon Usage Caps

A checkout service enforces a cap on how many times each customer may use each coupon. You are given the cap for every known coupon, a history of redemptions that have already happened, and a batch of eligibility questions.

Task

Implement check_coupon_eligibility(coupon_caps, redemption_history, queries).

  • coupon_caps: a dictionary mapping a coupon identifier (string) to an integer, the maximum number of times a single user may redeem that coupon.
  • redemption_history: a list of dictionaries, each with the keys user_id (string), promo_id (string) and timestamp (non-negative integer). Each entry records one completed redemption of coupon promo_id by user user_id.
  • queries: a list of dictionaries, each with the keys user_id and promo_id.

Return a list of booleans with one entry per query, in the same order as queries. An entry is True if the number of history entries for that exact (user_id, promo_id) pair is strictly less than the cap for that coupon, and False otherwise.

Rules

  • Every history entry counts toward usage, regardless of its timestamp. Timestamps do not affect the answer, and the history is not guaranteed to be sorted.
  • Duplicate history entries (even with identical user, coupon and timestamp) each count separately.
  • A query is a read-only check. Answering it does not add a redemption, so identical queries always get identical answers.
  • If a user has no history entries for a coupon, their usage is 0.
  • If a coupon is not present in coupon_caps, its cap is 0, so the answer is always False, even if the history contains entries for it.
  • A cap of 0 (or less) means nobody is ever eligible.
  • If queries is empty, return an empty list.

Constraints

  • redemption_history and queries each contain up to 10^5 entries.
  • timestamp is a non-negative integer.
  • A single user may have a very large number of history entries.
  • Your solution should comfortably run within time limits for the largest inputs.

Examples

Example 1

Input: check_coupon_eligibility({'SAVE10': 2, 'WELCOME': 1}, [{'user_id': 'u1', 'promo_id': 'SAVE10', 'timestamp': 100}, {'user_id': 'u1', 'promo_id': 'SAVE10', 'timestamp': 150}, {'user_id': 'u2', 'promo_id': 'WELCOME', 'timestamp': 200}], [{'user_id': 'u1', 'promo_id': 'SAVE10'}, {'user_id': 'u1', 'promo_id': 'WELCOME'}, {'user_id': 'u2', 'promo_id': 'WELCOME'}])

Output: [False, True, False]

Hints

Hint 1

Re-scanning the whole history for each query would take about 10^10 steps in the worst case. Think about preprocessing the history once.

Hint 2

Group usage by the pair (user_id, promo_id). A tuple works well as a dictionary key, and each history entry adds one to its pair's count.

Hint 3

When answering a query, use a default of 0 for a pair never seen and a default of 0 for a coupon missing from the caps, then compare with a strict less-than. Do not modify counts while answering.