Check Loan Records for Proper Borrow-Return Order
Arrays & HashingAuto-graded · 21 test casesLast updated
Scan a time-ordered list of library events and verify each item's history strictly alternates borrow, return, starting with a borrow, using per-item state tracking.
A library records every loan activity in a single list, already sorted from earliest to latest. Each record is a three-element list [time, item_id, is_borrow]:
timeis the moment of the event. It is informational only.item_ididentifies the item involved (a string or an integer).is_borrowistruewhen the item was borrowed andfalsewhen it was given back.
Write a function that decides whether the log is consistent.
Look at the events of any single item in the order they appear. That item's history is valid when:
- its first event is a borrow,
- borrows and returns then strictly alternate (borrow, return, borrow, return, ...), and
- no return occurs while the item is not currently borrowed.
Events of different items may be interleaved arbitrarily and do not affect each other. An item may still be borrowed when the log ends, which is perfectly fine.
Return True if the history of every item is valid, otherwise False. An empty log is valid.
Process the events in the given order; do not re-sort them.
Constraints
0 <= len(logs) <= 10^5- Each record has exactly three entries:
[time, item_id, is_borrow]. item_idvalues are hashable (strings or integers).is_borrowis a boolean.- Records are already in chronological order.
Examples
Example 1
Input: check_loan_log([[1, 'a', True], [2, 'a', False], [3, 'a', True], [4, 'a', False]])
Output: True
Example 2
Input: check_loan_log([[1, 'a', False]])
Output: False
Example 3
Input: check_loan_log([[1, 'a', True], [2, 'a', True]])
Output: False
Hints
Hint 1
Consider the two states an item can be in: on the shelf or borrowed. Every item starts on the shelf.
Hint 2
Each event type is only legal from one state: a borrow needs the item on the shelf, a return needs it borrowed. Anything else makes the log invalid.
Hint 3
A set of currently borrowed item ids is enough. Check membership before adding or removing, and return False at the first violation.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each item is in one of two states: on the shelf or borrowed. Every item starts on the shelf. Strict alternation means each event must flip the item's state, and each event type is legal from exactly one state:
| Event | Required state | New state |
|---|---|---|
| borrow | on the shelf | borrowed |
| return | borrowed | on the shelf |
Since there are only two states, a set of currently borrowed item ids is sufficient. Being in the set means borrowed; being absent means on the shelf (including items never seen).
Walk through the events once, in the given order:
- Borrow: if the item is already in the set, it is being borrowed twice, so return
False. Otherwise add it. - Return: if the item is not in the set, it is being returned without being borrowed, so return
False. Otherwise remove it. - If all events pass, return
True.
Why it is correct
Invariant: after processing some prefix of events, an item is in the set exactly when its history so far is valid and ends with a borrow.
- Initially all histories are empty (valid, not ending in a borrow) and the set is empty.
- For an event on item
x, other items are unaffected. A borrow is valid exactly whenx's history is empty or ends with a return, i.e.xis not in the set; afterwards it ends with a borrow, so we add it. A return is valid exactly when the history ends with a borrow, i.e.xis in the set; afterwards it ends with a return, so we remove it.
So the first invalid event for any item is detected at the moment it occurs. If none occurs, every item's history is valid. Interleaving across items is irrelevant because the rule is per item. Items still borrowed at the end are allowed, so no final emptiness check is made. The timestamp is never needed because the input is already ordered.
Complexity
- Time: O(n) expected, with one set lookup and at most one insert or delete per event.
- Space: O(m), where m is the number of items borrowed at the same time (at most n).
Reference solution
from typing import List
def check_loan_log(events: List[List]) -> bool:
borrowed = set()
for _, item_id, is_borrow in events:
if is_borrow:
if item_id in borrowed:
return False
borrowed.add(item_id)
else:
if item_id not in borrowed:
return False
borrowed.remove(item_id)
return True