
You are given a small library system with the following relational schema and several Python data-processing tasks. Answer the SQL questions and implement the described Python functions.
Assume the following tables:
Table: Books
book_id
INT, primary key
title
VARCHAR
condition
ENUM('good', 'damaged', 'lost') — current physical condition of the book copy
copies
INT — number of physical copies the library owns for this title
lifetime_value
DECIMAL — total revenue (e.g., rental fees) generated by this title over its lifetime
Table: Loans
loan_id
INT, primary key
book_id
INT, foreign key to
Books(book_id)
member_id
INT, foreign key to
Members(member_id)
loaned_at
DATETIME
returned_at
DATETIME NULL —
NULL
means the book has
not
yet been returned
Table: Members
member_id
INT, primary key
invited_by
INT NULL, foreign key to
Members(member_id)
— the member who invited this member, if any
reserved_copies
INT — number of book copies this member currently has reserved
Write SQL queries for the following:
condition = 'good'
and
the book has
not
yet been returned (i.e., the corresponding
Loans.returned_at
is
NULL
).
book_id
and
title
) that have:
copies > 10
), and
lifetime_value
among such books.
lifetime_value
descending, and if needed, break ties by
book_id
ascending. Limit the result to 3 rows.
invited_by
is not
NULL
), consider the pair:
Members.member_id = invited_by
Members.member_id
(the one who was invited)
reserved_copies
value. For every inviter–invitee pair, compute the absolute difference:
[ |\text{inviter.reserved_copies} - \text{invitee.reserved_copies}| ]
Write a query that returns
a single row with a single column
giving the
maximum
such absolute difference over all valid inviter–invitee pairs.
Implement the following functions in Python. Aim for efficient, readable solutions.
You are given a list of non-negative integers scores, where scores[i] is the number of points a student earns by reading the i-th book in a sequence. The student may choose to read any subset of books, but cannot read two adjacent books in the sequence (they need rest between adjacent books).
Write a function:
Constraints:
0 <= len(scores) <= 10**5
0 <= scores[i] <= 10**4
The function should return the maximum possible total score.
You are given a list of log entries representing operations on resources. Each entry is a tuple (action, resource_id) where:
action
is a string: either
'OPEN'
or
'CLOSE'
resource_id
is a string identifying the resource
A log is considered valid if all of the following hold:
Write a function:
Return False if the log does not make sense according to the above rules; otherwise return True.
Constraints:
0 <= len(logs) <= 10**5
You are given a list of office objects and a set of office IDs that are closed.
Assume offices are represented as:
Each Office dictionary has:
"id"
:
int
— office identifier
"employees"
:
List[Dict[str, object]]
— a list of employee objects assigned to that office (you do
not
need to inspect the employee fields; just count how many there are).
You are also given a set closed_office_ids: Set[int] containing the IDs of offices that are closed.
Write a function:
The function should:
id
is in
closed_office_ids
.
len(office["employees"])
.
office_id
(int) to that employee count (int) for
only
the open offices.
You may assume:
offices
.
0 <= len(offices) <= 10**4
.
You are given two non-empty strings a and b. Create a new string by alternating characters from a and b, starting with a. When one string runs out of characters, append the remaining characters from the other string.
For example:
a = "abc"
,
b = "XYZ"
→ result:
"aXbYcZ"
a = "ab"
,
b = "WXYZ"
→ result:
"aWbXYZ"
Write a function:
Assume:
1 <= len(a), len(b) <= 10**5
.
The function should run in O(len(a) + len(b)) time.