← 返回 instacart 的题目列表Idempotent Library Notifications for Due Loans and FIFO Holds
类型:online_judge
Problem: Idempotent Notifications for Loans and FIFO Holds
Patrons can borrow items. Implement a notification job that sends notifications in these cases:
24 hours before a borrowed item is due, send REMINDER24;
When the item is due or overdue, send DUE;
Patrons can hold an item. If the item currently has available copies, notify the earliest active holds in FIFO order with HOLD_READY. If there are N available copies, at most the first N active holds are eligible.
The job must be idempotent: if a notification key already exists in sent_notifications, it must not be emitted again.
Input Format
now
m
item_id total_copies
... m lines
l
loan_id patron_id item_id due_time returned
... l lines
h
hold_id patron_id item_id created_time active
... h lines
s
notification_key
... s lines
Notes:
Time values are integer hours;
returned: 0 means the loan is still active, 1 means returned;
active: 1 means the hold is still active, 0 means canceled or completed;
notification_key is one of:
REMINDER24:<loan_id>
DUE:<loan_id>
HOLD_READY:<hold_id>
Rules
Only loans with returned = 0 can generate due-related notifications;
If now >= due_time - 24 and REMINDER24:<loan_id> has not been sent, emit REMINDER24;
If now >= due_time and DUE:<loan_id> has not been sent, emit DUE;
Available copies for an item: total_copies - number of active unreturned loans;
For each item, sort active holds by (created_time, hold_id); if availability is N, only the first N holds are eligible for HOLD_READY;
If HOLD_READY:<hold_id> has already been sent, do not emit it again, but it still occupies its FIFO position.
Output Format
Print the number of notifications, followed by one notification per line:
TYPE patron_id item_id reference_id
where:
For REMINDER24 / DUE, reference_id is loan_id;
For HOLD_READY, reference_id is hold_id.
Output order:
Loan notifications sorted by loan_id lexicographically; for the same loan, REMINDER24 before DUE;
Hold-ready notifications sorted by item_id lexicographically, then FIFO order.
Example
Input
100
1
bookA 2
1
loan1 p1 bookA 130 0
2
hold1 p2 bookA 10 1
hold2 p3 bookA 20 1
0
Output
2
REMINDER24 p1 bookA loan1
HOLD_READY p2 bookA hold1