Loan Count Gap Between Newest Member and Their Sponsor
SQL & DataAuto-graded · 13 test casesLast updated
Tests table-style aggregation in Python: select the row with the highest key, count occurrences per key, and handle null or missing sponsor references with a default of zero.
A library tracks which members currently hold its book copies, and which member introduced each member.
You are given two tables as lists of rows:
copies: each row is[copy_id, held_by_member_id]. The second value is the member currently holding that copy, orNoneif nobody holds it.members: each row is[member_id, sponsor_member_id]. The second value is the member who introduced this member, orNoneif there is no sponsor.
Find the member with the largest member_id in members. Return a list of exactly three values:
- that member's
member_id, - that member's
sponsor_member_id(which may beNone), - the gap: the number of copies held by that member, minus the number of copies held by their sponsor.
Rules:
- A copy with
held_by_member_idequal toNonecounts for nobody. - If the sponsor is
None, or the sponsor holds no copies, the sponsor's count is0. - A sponsor id does not need to appear in
members; their count is still taken fromcopies. - The gap may be negative or zero.
- Each row in
copiescounts as one copy.
Return the result as [member_id, sponsor_member_id, gap].
Constraints
memberscontains at least one row, andmember_idvalues are unique.copiesmay be empty.- All ids are integers, except that
held_by_member_idandsponsor_member_idmay beNone. - The result is always a list of length 3, in the order given above.
Examples
Example 1
Input: loan_gap_for_newest_member([[1, 10], [2, 10], [3, 20], [4, 30]], [[10, None], [20, 10], [30, 20]])
Output: [30, 20, 0]
Hints
Hint 1
Splitting the work into independent parts helps: pick the target member, count holdings per member, then look up two counts.
Hint 2
Build a dictionary of how many copies each member holds, skipping rows where the holder is None.
Hint 3
Use a default of 0 when looking up a count (a missing member or a None sponsor), so the subtraction never involves a missing value.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat the task as three independent steps.
- Pick the target member. Scan
membersand take the row with the largestmember_id. That row gives both the member id and the sponsor id. - Count holdings. Walk through
copiesonce and build a dictionary (aCounter) mapping each member id to the number of copies they hold. Rows whose holder isNoneare skipped, because those copies belong to nobody. - Look up and subtract. Read the target's count, defaulting to
0. If the sponsor isNone, their count is0; otherwise read it from the dictionary, again defaulting to0. Return[member_id, sponsor_id, own_count - sponsor_count].
Why it is correct
- The counts come only from
copies, so a sponsor who is missing frommembersor who holds nothing simply gets0. - Skipping
Noneholders ensures unheld copies are never attributed to anyone. - Using
.get(key, 0)means a missing key never produces an error or a missing value in the subtraction. - Counting once up front avoids re-scanning
copiesfor each lookup. - A negative gap is natural and needs no special handling, and a self-sponsor yields a gap of
0.
Complexity
Let C be the number of copies and M the number of members.
- Time: O(M + C), one pass over each list.
- Space: O(K), where K is the number of distinct holders (K ≤ C).
Reference solution
from collections import Counter
from typing import List, Optional
def loan_gap_for_newest_member(copies: List[List[Optional[int]]], members: List[List[Optional[int]]]) -> List[Optional[int]]:
newest = max(members, key=lambda row: row[0])
member_id, sponsor_id = newest
counts = Counter(holder for _, holder in copies if holder is not None)
own_count = counts.get(member_id, 0)
sponsor_count = counts.get(sponsor_id, 0) if sponsor_id is not None else 0
return [member_id, sponsor_id, own_count - sponsor_count]