Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Table: library_books
+------------------+---------+ | Column Name | Type | +------------------+---------+ | book_id | int | | title | varchar | | author | varchar | | genre | varchar | | publication_year | int | | total_copies | int | +------------------+---------+ book_id is the unique identifier for this table. Each row contains information about a book in the library, including the total number of copies owned by the library.
Table: borrowing_records
+---------------+---------+ | Column Name | Type | +---------------+---------+ | record_id | int | | book_id | int | | borrower_name | varchar | | borrow_date | date | | return_date | date | +---------------+---------+ record_id is the unique identifier for this table. Each row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.
Write a solution to find all books that are currently borrowed (not returned) and have zero copies available in the library.
return_dateReturn the result table ordered by current borrowers in descending order, then by book title in ascending order.
The result format is in the following example.
Example:
Input:
library_books table:
+---------+------------------------+------------------+----------+------------------+--------------+ | book_id | title | author | genre | publication_year | total_copies | +---------+------------------------+------------------+----------+------------------+--------------+ | 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 | | 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 | | 3 | 1984 | George Orwell | Dystopian| 1949 | 1 | | 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 | | 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 | | 6 | Brave New World | Aldous Huxley | Dystopian| 1932 | 4 | +---------+------------------------+------------------+----------+------------------+--------------+
borrowing_records table:
+-----------+---------+---------------+-------------+-------------+ | record_id | book_id | borrower_name | borrow_date | return_date | +-----------+---------+---------------+-------------+-------------+ | 1 | 1 | Alice Smith | 2024-01-15 | NULL | | 2 | 1 | Bob Johnson | 2024-01-20 | NULL | | 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 | | 4 | 3 | David Brown | 2024-02-01 | NULL | | 5 | 4 | Emma Wilson | 2024-01-05 | NULL | | 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 | | 7 | 1 | Grace Miller | 2024-02-05 | NULL | | 8 | 6 | Henry Taylor | 2024-01-12 | NULL | | 9 | 2 | Ivan Clark | 2024-02-12 | NULL | | 10 | 2 | Jane Adams | 2024-02-15 | NULL | +-----------+---------+---------------+-------------+-------------+
Output:
+---------+------------------+---------------+-----------+------------------+-------------------+ | book_id | title | author | genre | publication_year | current_borrowers | +---------+------------------+---------------+-----------+------------------+-------------------+ | 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 | | 3 | 1984 | George Orwell | Dystopian | 1949 | 1 | +---------+------------------+---------------+-----------+------------------+-------------------+
Explanation:
Output table is ordered by current_borrowers in descending order, then by book_title in ascending order.
Problem summary: Table: library_books +------------------+---------+ | Column Name | Type | +------------------+---------+ | book_id | int | | title | varchar | | author | varchar | | genre | varchar | | publication_year | int | | total_copies | int | +------------------+---------+ book_id is the unique identifier for this table. Each row contains information about a book in the library, including the total number of copies owned by the library. Table: borrowing_records +---------------+---------+ | Column Name | Type | +---------------+---------+ | record_id | int | | book_id | int | | borrower_name | varchar | | borrow_date | date | | return_date | date | +---------------+---------+ record_id is the unique identifier for this table. Each row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet. Write a solution to find all books that
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"library_books":["book_id","title","author","genre","publication_year","total_copies"],"borrowing_records":["record_id","book_id","borrower_name","borrow_date","return_date"]},"rows":{"library_books":[[1,"The Great Gatsby","F. Scott","Fiction",1925,3],[2,"To Kill a Mockingbird","Harper Lee","Fiction",1960,3],[3,"1984","George Orwell","Dystopian",1949,1],[4,"Pride and Prejudice","Jane Austen","Romance",1813,2],[5,"The Catcher in the Rye","J.D. Salinger","Fiction",1951,1],[6,"Brave New World","Aldous Huxley","Dystopian",1932,4]],"borrowing_records":[[1,1,"Alice Smith","2024-01-15",null],[2,1,"Bob Johnson","2024-01-20",null],[3,2,"Carol White","2024-01-10","2024-01-25"],[4,3,"David Brown","2024-02-01",null],[5,4,"Emma Wilson","2024-01-05",null],[6,5,"Frank Davis","2024-01-18","2024-02-10"],[7,1,"Grace Miller","2024-02-05",null],[8,6,"Henry Taylor","2024-01-12",null],[9,2,"Ivan Clark","2024-02-12",null],[10,2,"Jane Adams","2024-02-15",null]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3570: Find Books with No Available Copies
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #3570: Find Books with No Available Copies
// import pandas as pd
//
//
// def find_books_with_no_available_copies(
// library_books: pd.DataFrame, borrowing_records: pd.DataFrame
// ) -> pd.DataFrame:
// current_borrowers = (
// borrowing_records[borrowing_records["return_date"].isna()]
// .groupby("book_id")
// .size()
// .rename("current_borrowers")
// .reset_index()
// )
//
// merged = library_books.merge(current_borrowers, on="book_id", how="inner")
// fully_borrowed = merged[merged["current_borrowers"] == merged["total_copies"]]
// fully_borrowed = fully_borrowed.sort_values(
// by=["current_borrowers", "title"], ascending=[False, True]
// )
//
// cols = [
// "book_id",
// "title",
// "author",
// "genre",
// "publication_year",
// "current_borrowers",
// ]
// return fully_borrowed[cols].reset_index(drop=True)
// Accepted solution for LeetCode #3570: Find Books with No Available Copies
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #3570: Find Books with No Available Copies
// import pandas as pd
//
//
// def find_books_with_no_available_copies(
// library_books: pd.DataFrame, borrowing_records: pd.DataFrame
// ) -> pd.DataFrame:
// current_borrowers = (
// borrowing_records[borrowing_records["return_date"].isna()]
// .groupby("book_id")
// .size()
// .rename("current_borrowers")
// .reset_index()
// )
//
// merged = library_books.merge(current_borrowers, on="book_id", how="inner")
// fully_borrowed = merged[merged["current_borrowers"] == merged["total_copies"]]
// fully_borrowed = fully_borrowed.sort_values(
// by=["current_borrowers", "title"], ascending=[False, True]
// )
//
// cols = [
// "book_id",
// "title",
// "author",
// "genre",
// "publication_year",
// "current_borrowers",
// ]
// return fully_borrowed[cols].reset_index(drop=True)
# Accepted solution for LeetCode #3570: Find Books with No Available Copies
import pandas as pd
def find_books_with_no_available_copies(
library_books: pd.DataFrame, borrowing_records: pd.DataFrame
) -> pd.DataFrame:
current_borrowers = (
borrowing_records[borrowing_records["return_date"].isna()]
.groupby("book_id")
.size()
.rename("current_borrowers")
.reset_index()
)
merged = library_books.merge(current_borrowers, on="book_id", how="inner")
fully_borrowed = merged[merged["current_borrowers"] == merged["total_copies"]]
fully_borrowed = fully_borrowed.sort_values(
by=["current_borrowers", "title"], ascending=[False, True]
)
cols = [
"book_id",
"title",
"author",
"genre",
"publication_year",
"current_borrowers",
]
return fully_borrowed[cols].reset_index(drop=True)
// Accepted solution for LeetCode #3570: Find Books with No Available Copies
// Rust example auto-generated from py reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (py):
// # Accepted solution for LeetCode #3570: Find Books with No Available Copies
// import pandas as pd
//
//
// def find_books_with_no_available_copies(
// library_books: pd.DataFrame, borrowing_records: pd.DataFrame
// ) -> pd.DataFrame:
// current_borrowers = (
// borrowing_records[borrowing_records["return_date"].isna()]
// .groupby("book_id")
// .size()
// .rename("current_borrowers")
// .reset_index()
// )
//
// merged = library_books.merge(current_borrowers, on="book_id", how="inner")
// fully_borrowed = merged[merged["current_borrowers"] == merged["total_copies"]]
// fully_borrowed = fully_borrowed.sort_values(
// by=["current_borrowers", "title"], ascending=[False, True]
// )
//
// cols = [
// "book_id",
// "title",
// "author",
// "genre",
// "publication_year",
// "current_borrowers",
// ]
// return fully_borrowed[cols].reset_index(drop=True)
// Accepted solution for LeetCode #3570: Find Books with No Available Copies
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #3570: Find Books with No Available Copies
// import pandas as pd
//
//
// def find_books_with_no_available_copies(
// library_books: pd.DataFrame, borrowing_records: pd.DataFrame
// ) -> pd.DataFrame:
// current_borrowers = (
// borrowing_records[borrowing_records["return_date"].isna()]
// .groupby("book_id")
// .size()
// .rename("current_borrowers")
// .reset_index()
// )
//
// merged = library_books.merge(current_borrowers, on="book_id", how="inner")
// fully_borrowed = merged[merged["current_borrowers"] == merged["total_copies"]]
// fully_borrowed = fully_borrowed.sort_values(
// by=["current_borrowers", "title"], ascending=[False, True]
// )
//
// cols = [
// "book_id",
// "title",
// "author",
// "genre",
// "publication_year",
// "current_borrowers",
// ]
// return fully_borrowed[cols].reset_index(drop=True)
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.