Skip to content

Added task 3642 #2030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

#
# Copyright © 2015-2021 the original authors.
# Copyright © 2015 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
3642\. Find Books with Polarized Opinions

Easy

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 polarized opinions if it has `at least one rating ≥ 4` and `at least one rating ≤ 2`
* Only consider books that have **at least** `5` **reading sessions**
* Calculate the **rating spread** as (`highest_rating - lowest_rating`)
* Calculate the **polarization score** as the number of extreme ratings (`ratings ≤ 2 or ≥ 4`) divided by total sessions
* **Only include** books where `polarization 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 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 Great Gatsby (book\_id = 1):**
* Has 5 reading sessions (meets minimum requirement)
* Ratings: 5, 1, 4, 2, 5
* Has ratings ≥ 4: 5, 4, 5 (3 sessions)
* Has ratings ≤ 2: 1, 2 (2 sessions)
* Rating spread: 5 - 1 = 4
* Extreme ratings (≤2 or ≥4): All 5 sessions (5, 1, 4, 2, 5)
* Polarization score: 5/5 = 1.00 (≥ 0.6, qualifies)
* **1984 (book\_id = 3):**
* Has 6 reading sessions (meets minimum requirement)
* Ratings: 2, 1, 2, 1, 4, 5
* Has ratings ≥ 4: 4, 5 (2 sessions)
* Has ratings ≤ 2: 2, 1, 2, 1 (4 sessions)
* Rating spread: 5 - 1 = 4
* Extreme ratings (≤2 or ≥4): All 6 sessions (2, 1, 2, 1, 4, 5)
* Polarization score: 6/6 = 1.00 (≥ 0.6, qualifies)
* **Books not included:**
* To Kill a Mockingbird (book\_id = 2): All ratings are 4-5, no low ratings (≤2)
* Pride and Prejudice (book\_id = 4): Only 2 sessions (< 5 minimum)
* The Catcher in the Rye (book\_id = 5): Only 2 sessions (< 5 minimum)

The result table is ordered by polarization score in descending order, then by book title in descending order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Write your MySQL query statement below
# #Easy #Database #2025_08_10_Time_490_ms_(100.00%)_Space_0.0_MB_(100.00%)
WITH book_stats AS (
SELECT
book_id,
COUNT(*) AS total_sessions,
SUM(CASE WHEN session_rating <> 3 THEN 1 ELSE 0 END) AS extreme_ratings,
MAX(session_rating) AS max_rating,
MIN(session_rating) AS min_rating,
SUM(CASE WHEN session_rating > 3 THEN 1 ELSE 0 END) AS high_ratings,
SUM(CASE WHEN session_rating <= 2 THEN 1 ELSE 0 END) AS low_ratings
FROM reading_sessions
GROUP BY book_id
)
SELECT
bs.book_id,
b.title,
b.author,
b.genre,
b.pages,
(bs.max_rating - bs.min_rating) AS rating_spread,
ROUND(bs.extreme_ratings * 1.0 / bs.total_sessions, 2) AS polarization_score
FROM book_stats bs
JOIN books b USING (book_id)
WHERE
bs.total_sessions >= 5
AND bs.high_ratings > 0
AND bs.low_ratings > 0
AND (bs.extreme_ratings * 1.0 / bs.total_sessions) >= 0.6
ORDER BY polarization_score DESC, b.title DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package g3601_3700.s3642_find_books_with_polarized_opinions;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
import org.zapodot.junit.db.common.CompatibilityMode;

@EmbeddedDatabaseTest(
compatibilityMode = CompatibilityMode.MySQL,
initialSqls =
"CREATE TABLE books ("
+ " book_id INT PRIMARY KEY,"
+ " title VARCHAR(255),"
+ " author VARCHAR(255),"
+ " genre VARCHAR(50),"
+ " pages INT"
+ ");"
+ "INSERT INTO books (book_id, title, author, genre, pages) VALUES"
+ "(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);"
+ "CREATE TABLE reading_sessions ("
+ " session_id INT PRIMARY KEY,"
+ " book_id INT,"
+ " reader_name VARCHAR(100),"
+ " pages_read INT,"
+ " session_rating INT,"
+ " FOREIGN KEY (book_id) REFERENCES books(book_id)"
+ ");"
+ "INSERT INTO reading_sessions (session_id, book_id, "
+ "reader_name, pages_read, session_rating) VALUES"
+ "(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);")
class MysqlTest {
@Test
void testScript(@EmbeddedDatabase DataSource dataSource)
throws SQLException, FileNotFoundException {
try (final Connection connection = dataSource.getConnection()) {
try (final Statement statement = connection.createStatement();
final ResultSet resultSet =
statement.executeQuery(
new BufferedReader(
new FileReader(
"src/main/java/g3601_3700/"
+ "s3642_find_books_with_"
+ "polarized_opinions/"
+ "script.sql"))
.lines()
.collect(Collectors.joining("\n"))
.replaceAll("#.*?\\r?\\n", ""))) {
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("1"));
assertThat(resultSet.getNString(2), equalTo("The Great Gatsby"));
assertThat(resultSet.getNString(3), equalTo("F. Scott"));
assertThat(resultSet.getNString(4), equalTo("Fiction"));
assertThat(resultSet.getNString(5), equalTo("180"));
assertThat(resultSet.getNString(6), equalTo("4"));
assertThat(resultSet.getNString(7), equalTo("1.00"));
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("3"));
assertThat(resultSet.getNString(2), equalTo("1984"));
assertThat(resultSet.getNString(3), equalTo("George Orwell"));
assertThat(resultSet.getNString(4), equalTo("Dystopian"));
assertThat(resultSet.getNString(5), equalTo("328"));
assertThat(resultSet.getNString(6), equalTo("4"));
assertThat(resultSet.getNString(7), equalTo("1.00"));
assertThat(resultSet.next(), equalTo(false));
}
}
}
}
Loading