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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: books
+-------------+---------+ | Column Name | Type | +-------------+---------+ | book_id | int | | title | varchar | | author | varchar | | genre | varchar | | pages | int | +-------------+---------+ book_id is the unique ID for this table. Each row contains information about a book including its genre and page count.
Table: reading_sessions
+----------------+---------+ | Column Name | Type | +----------------+---------+ | session_id | int | | book_id | int | | reader_name | varchar | | pages_read | int | | session_rating | int | +----------------+---------+ session_id is the unique ID for this table. Each row represents a reading session where someone read a portion of a book. session_rating is on a scale of 1-5.
Write a solution to find books that have polarized opinions - books that receive both very high ratings and very low ratings from different readers.
at least one rating ≥ 4 and at least one rating ≤ 25 reading sessionshighest_rating - lowest_rating)ratings ≤ 2 or ≥ 4) divided by total sessionspolarization score ≥ 0.6 (at least 60% extreme ratings)Return the result table ordered by polarization score in descending order, then by title in descending order.
The polarization score should be rounded to 2 decimal places.
The result format is in the following example.
Example:
Input:
books table:
+---------+------------------------+---------------+----------+-------+ | book_id | title | author | genre | pages | +---------+------------------------+---------------+----------+-------+ | 1 | The Great Gatsby | F. Scott | Fiction | 180 | | 2 | To Kill a Mockingbird | Harper Lee | Fiction | 281 | | 3 | 1984 | George Orwell | Dystopian| 328 | | 4 | Pride and Prejudice | Jane Austen | Romance | 432 | | 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 277 | +---------+------------------------+---------------+----------+-------+
reading_sessions table:
+------------+---------+-------------+------------+----------------+ | session_id | book_id | reader_name | pages_read | session_rating | +------------+---------+-------------+------------+----------------+ | 1 | 1 | Alice | 50 | 5 | | 2 | 1 | Bob | 60 | 1 | | 3 | 1 | Carol | 40 | 4 | | 4 | 1 | David | 30 | 2 | | 5 | 1 | Emma | 45 | 5 | | 6 | 2 | Frank | 80 | 4 | | 7 | 2 | Grace | 70 | 4 | | 8 | 2 | Henry | 90 | 5 | | 9 | 2 | Ivy | 60 | 4 | | 10 | 2 | Jack | 75 | 4 | | 11 | 3 | Kate | 100 | 2 | | 12 | 3 | Liam | 120 | 1 | | 13 | 3 | Mia | 80 | 2 | | 14 | 3 | Noah | 90 | 1 | | 15 | 3 | Olivia | 110 | 4 | | 16 | 3 | Paul | 95 | 5 | | 17 | 4 | Quinn | 150 | 3 | | 18 | 4 | Ruby | 140 | 3 | | 19 | 5 | Sam | 80 | 1 | | 20 | 5 | Tara | 70 | 2 | +------------+---------+-------------+------------+----------------+
Output:
+---------+------------------+---------------+-----------+-------+---------------+--------------------+ | book_id | title | author | genre | pages | rating_spread | polarization_score | +---------+------------------+---------------+-----------+-------+---------------+--------------------+ | 1 | The Great Gatsby | F. Scott | Fiction | 180 | 4 | 1.00 | | 3 | 1984 | George Orwell | Dystopian | 328 | 4 | 1.00 | +---------+------------------+---------------+-----------+-------+---------------+--------------------+
Explanation:
The result table is ordered by polarization score in descending order, then by book title in descending order.
Problem summary: Table: books +-------------+---------+ | Column Name | Type | +-------------+---------+ | book_id | int | | title | varchar | | author | varchar | | genre | varchar | | pages | int | +-------------+---------+ book_id is the unique ID for this table. Each row contains information about a book including its genre and page count. Table: reading_sessions +----------------+---------+ | Column Name | Type | +----------------+---------+ | session_id | int | | book_id | int | | reader_name | varchar | | pages_read | int | | session_rating | int | +----------------+---------+ session_id is the unique ID for this table. Each row represents a reading session where someone read a portion of a book. session_rating is on a scale of 1-5. Write a solution to find books that have polarized opinions - books that receive both very high ratings and very low ratings from different readers. A book has
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"books":["book_id","title","author","genre","pages"],"reading_sessions":["session_id","book_id","reader_name","pages_read","session_rating"]},"rows":{"books":[[1,"The Great Gatsby","F. Scott","Fiction",180],[2,"To Kill a Mockingbird","Harper Lee","Fiction",281],[3,"1984","George Orwell","Dystopian",328],[4,"Pride and Prejudice","Jane Austen","Romance",432],[5,"The Catcher in the Rye","J.D. Salinger","Fiction",277]],"reading_sessions":[[1,1,"Alice",50,5],[2,1,"Bob",60,1],[3,1,"Carol",40,4],[4,1,"David",30,2],[5,1,"Emma",45,5],[6,2,"Frank",80,4],[7,2,"Grace",70,4],[8,2,"Henry",90,5],[9,2,"Ivy",60,4],[10,2,"Jack",75,4],[11,3,"Kate",100,2],[12,3,"Liam",120,1],[13,3,"Mia",80,2],[14,3,"Noah",90,1],[15,3,"Olivia",110,4],[16,3,"Paul",95,5],[17,4,"Quinn",150,3],[18,4,"Ruby",140,3],[19,5,"Sam",80,1],[20,5,"Tara",70,2]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// import pandas as pd
// from decimal import Decimal, ROUND_HALF_UP
//
//
// def find_polarized_books(
// books: pd.DataFrame, reading_sessions: pd.DataFrame
// ) -> pd.DataFrame:
// df = books.merge(reading_sessions, on="book_id")
// agg_df = (
// df.groupby(["book_id", "title", "author", "genre", "pages"])
// .agg(
// max_rating=("session_rating", "max"),
// min_rating=("session_rating", "min"),
// rating_spread=("session_rating", lambda x: x.max() - x.min()),
// count_sessions=("session_rating", "count"),
// low_or_high_count=("session_rating", lambda x: ((x <= 2) | (x >= 4)).sum()),
// )
// .reset_index()
// )
//
// agg_df["polarization_score"] = agg_df.apply(
// lambda r: float(
// Decimal(r["low_or_high_count"] / r["count_sessions"]).quantize(
// Decimal("0.01"), rounding=ROUND_HALF_UP
// )
// ),
// axis=1,
// )
//
// result = agg_df[
// (agg_df["count_sessions"] >= 5)
// & (agg_df["max_rating"] >= 4)
// & (agg_df["min_rating"] <= 2)
// & (agg_df["polarization_score"] >= 0.6)
// ]
//
// return result.sort_values(
// by=["polarization_score", "title"], ascending=[False, False]
// )[
// [
// "book_id",
// "title",
// "author",
// "genre",
// "pages",
// "rating_spread",
// "polarization_score",
// ]
// ]
// Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// import pandas as pd
// from decimal import Decimal, ROUND_HALF_UP
//
//
// def find_polarized_books(
// books: pd.DataFrame, reading_sessions: pd.DataFrame
// ) -> pd.DataFrame:
// df = books.merge(reading_sessions, on="book_id")
// agg_df = (
// df.groupby(["book_id", "title", "author", "genre", "pages"])
// .agg(
// max_rating=("session_rating", "max"),
// min_rating=("session_rating", "min"),
// rating_spread=("session_rating", lambda x: x.max() - x.min()),
// count_sessions=("session_rating", "count"),
// low_or_high_count=("session_rating", lambda x: ((x <= 2) | (x >= 4)).sum()),
// )
// .reset_index()
// )
//
// agg_df["polarization_score"] = agg_df.apply(
// lambda r: float(
// Decimal(r["low_or_high_count"] / r["count_sessions"]).quantize(
// Decimal("0.01"), rounding=ROUND_HALF_UP
// )
// ),
// axis=1,
// )
//
// result = agg_df[
// (agg_df["count_sessions"] >= 5)
// & (agg_df["max_rating"] >= 4)
// & (agg_df["min_rating"] <= 2)
// & (agg_df["polarization_score"] >= 0.6)
// ]
//
// return result.sort_values(
// by=["polarization_score", "title"], ascending=[False, False]
// )[
// [
// "book_id",
// "title",
// "author",
// "genre",
// "pages",
// "rating_spread",
// "polarization_score",
// ]
// ]
# Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
import pandas as pd
from decimal import Decimal, ROUND_HALF_UP
def find_polarized_books(
books: pd.DataFrame, reading_sessions: pd.DataFrame
) -> pd.DataFrame:
df = books.merge(reading_sessions, on="book_id")
agg_df = (
df.groupby(["book_id", "title", "author", "genre", "pages"])
.agg(
max_rating=("session_rating", "max"),
min_rating=("session_rating", "min"),
rating_spread=("session_rating", lambda x: x.max() - x.min()),
count_sessions=("session_rating", "count"),
low_or_high_count=("session_rating", lambda x: ((x <= 2) | (x >= 4)).sum()),
)
.reset_index()
)
agg_df["polarization_score"] = agg_df.apply(
lambda r: float(
Decimal(r["low_or_high_count"] / r["count_sessions"]).quantize(
Decimal("0.01"), rounding=ROUND_HALF_UP
)
),
axis=1,
)
result = agg_df[
(agg_df["count_sessions"] >= 5)
& (agg_df["max_rating"] >= 4)
& (agg_df["min_rating"] <= 2)
& (agg_df["polarization_score"] >= 0.6)
]
return result.sort_values(
by=["polarization_score", "title"], ascending=[False, False]
)[
[
"book_id",
"title",
"author",
"genre",
"pages",
"rating_spread",
"polarization_score",
]
]
// Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// 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 #3642: Find Books with Polarized Opinions
// import pandas as pd
// from decimal import Decimal, ROUND_HALF_UP
//
//
// def find_polarized_books(
// books: pd.DataFrame, reading_sessions: pd.DataFrame
// ) -> pd.DataFrame:
// df = books.merge(reading_sessions, on="book_id")
// agg_df = (
// df.groupby(["book_id", "title", "author", "genre", "pages"])
// .agg(
// max_rating=("session_rating", "max"),
// min_rating=("session_rating", "min"),
// rating_spread=("session_rating", lambda x: x.max() - x.min()),
// count_sessions=("session_rating", "count"),
// low_or_high_count=("session_rating", lambda x: ((x <= 2) | (x >= 4)).sum()),
// )
// .reset_index()
// )
//
// agg_df["polarization_score"] = agg_df.apply(
// lambda r: float(
// Decimal(r["low_or_high_count"] / r["count_sessions"]).quantize(
// Decimal("0.01"), rounding=ROUND_HALF_UP
// )
// ),
// axis=1,
// )
//
// result = agg_df[
// (agg_df["count_sessions"] >= 5)
// & (agg_df["max_rating"] >= 4)
// & (agg_df["min_rating"] <= 2)
// & (agg_df["polarization_score"] >= 0.6)
// ]
//
// return result.sort_values(
// by=["polarization_score", "title"], ascending=[False, False]
// )[
// [
// "book_id",
// "title",
// "author",
// "genre",
// "pages",
// "rating_spread",
// "polarization_score",
// ]
// ]
// Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #3642: Find Books with Polarized Opinions
// import pandas as pd
// from decimal import Decimal, ROUND_HALF_UP
//
//
// def find_polarized_books(
// books: pd.DataFrame, reading_sessions: pd.DataFrame
// ) -> pd.DataFrame:
// df = books.merge(reading_sessions, on="book_id")
// agg_df = (
// df.groupby(["book_id", "title", "author", "genre", "pages"])
// .agg(
// max_rating=("session_rating", "max"),
// min_rating=("session_rating", "min"),
// rating_spread=("session_rating", lambda x: x.max() - x.min()),
// count_sessions=("session_rating", "count"),
// low_or_high_count=("session_rating", lambda x: ((x <= 2) | (x >= 4)).sum()),
// )
// .reset_index()
// )
//
// agg_df["polarization_score"] = agg_df.apply(
// lambda r: float(
// Decimal(r["low_or_high_count"] / r["count_sessions"]).quantize(
// Decimal("0.01"), rounding=ROUND_HALF_UP
// )
// ),
// axis=1,
// )
//
// result = agg_df[
// (agg_df["count_sessions"] >= 5)
// & (agg_df["max_rating"] >= 4)
// & (agg_df["min_rating"] <= 2)
// & (agg_df["polarization_score"] >= 0.6)
// ]
//
// return result.sort_values(
// by=["polarization_score", "title"], ascending=[False, False]
// )[
// [
// "book_id",
// "title",
// "author",
// "genre",
// "pages",
// "rating_spread",
// "polarization_score",
// ]
// ]
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.