Skip to content

Commit 93ff703

Browse files
authored
Added task 3421
1 parent 15e30b8 commit 93ff703

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
3421\. Find Students Who Improved
2+
3+
Medium
4+
5+
Table: `Scores`
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| student_id | int |
11+
| subject | varchar |
12+
| score | int |
13+
| exam_date | varchar |
14+
+-------------+---------+
15+
(student_id, subject, exam_date) is the primary key for this table.
16+
Each row contains information about a student's score in a specific subject on a particular exam date. score is between 0 and 100 (inclusive).
17+
18+
Write a solution to find the **students who have shown improvement**. A student is considered to have shown improvement if they meet **both** of these conditions:
19+
20+
* Have taken exams in the **same subject** on at least two different dates
21+
* Their **latest score** in that subject is **higher** than their **first score**
22+
23+
Return _the result table_ _ordered by_ `student_id,` `subject` _in **ascending** order_.
24+
25+
The result format is in the following example.
26+
27+
**Example:**
28+
29+
**Input:**
30+
31+
Scores table:
32+
33+
+------------+----------+-------+------------+
34+
| student_id | subject | score | exam_date |
35+
+------------+----------+-------+------------+
36+
| 101 | Math | 70 | 15-01-2023 |
37+
| 101 | Math | 85 | 15-02-2023 |
38+
| 101 | Physics | 65 | 15-01-2023 |
39+
| 101 | Physics | 60 | 15-02-2023 |
40+
| 102 | Math | 80 | 15-01-2023 |
41+
| 102 | Math | 85 | 15-02-2023 |
42+
| 103 | Math | 90 | 15-01-2023 |
43+
| 104 | Physics | 75 | 15-01-2023 |
44+
| 104 | Physics | 85 | 15-02-2023 |
45+
+------------+----------+-------+------------+
46+
47+
**Output:**
48+
49+
+------------+----------+-------------+--------------+
50+
| student_id | subject | first_score | latest_score |
51+
+------------+----------+-------------+--------------+
52+
| 101 | Math | 70 | 85 |
53+
| 102 | Math | 80 | 85 |
54+
| 104 | Physics | 75 | 85 |
55+
+------------+----------+-------------+--------------+
56+
57+
**Explanation:**
58+
59+
* Student 101 in Math: Improved from 70 to 85
60+
* Student 101 in Physics: No improvement (dropped from 65 to 60)
61+
* Student 102 in Math: Improved from 80 to 85
62+
* Student 103 in Math: Only one exam, not eligible
63+
* Student 104 in Physics: Improved from 75 to 85
64+
65+
Result table is ordered by student\_id, subject.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2025_01_17_Time_466_ms_(74.56%)_Space_0B_(100.00%)
3+
4+
WITH Ranked AS (
5+
SELECT
6+
student_id,
7+
subject,
8+
FIRST_VALUE(score) OVER(PARTITION BY student_id,subject ORDER BY exam_date) AS first_score,
9+
FIRST_VALUE(score) OVER(PARTITION BY student_id,subject ORDER BY exam_date DESC) AS latest_score
10+
FROM Scores
11+
)
12+
13+
SELECT * FROM Ranked
14+
WHERE first_score<latest_score
15+
GROUP BY student_id,subject
16+
ORDER BY student_id,subject
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package g3401_3500.s3421_find_students_who_improved
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
(
20+
" CREATE TABLE Scores (" +
21+
" student_id INT," +
22+
" subject VARCHAR(50)," +
23+
" score INT," +
24+
" exam_date VARCHAR(10)" +
25+
");" +
26+
"insert into Scores (student_id, subject, score, exam_date) values " +
27+
"('101', 'Math', '70', '15-01-2023');" +
28+
"insert into Scores (student_id, subject, score, exam_date) values " +
29+
"('101', 'Math', '85', '15-02-2023');" +
30+
"insert into Scores (student_id, subject, score, exam_date) values " +
31+
"('101', 'Physics', '65', '15-01-2023');" +
32+
"insert into Scores (student_id, subject, score, exam_date) values " +
33+
"('101', 'Physics', '60', '15-02-2023');" +
34+
"insert into Scores (student_id, subject, score, exam_date) values " +
35+
"('102', 'Math', '80', '15-01-2023');" +
36+
"insert into Scores (student_id, subject, score, exam_date) values " +
37+
"('102', 'Math', '85', '15-02-2023');" +
38+
"insert into Scores (student_id, subject, score, exam_date) values " +
39+
"('103', 'Math', '90', '15-01-2023');" +
40+
"insert into Scores (student_id, subject, score, exam_date) values " +
41+
"('104', 'Physics', '75', '15-01-2023');" +
42+
"insert into Scores (student_id, subject, score, exam_date) values " +
43+
"('104', 'Physics', '85', '15-02-2023');"
44+
),
45+
],
46+
)
47+
internal class MysqlTest {
48+
@Test
49+
@Throws(SQLException::class, FileNotFoundException::class)
50+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
51+
dataSource.connection.use { connection ->
52+
connection.createStatement().use { statement ->
53+
statement.executeQuery(
54+
BufferedReader(
55+
FileReader(
56+
"src/main/kotlin/g3401_3500/" +
57+
"s3421_find_students_who_improved/script.sql",
58+
),
59+
)
60+
.lines()
61+
.collect(Collectors.joining("\n"))
62+
.replace("#.*?\\r?\\n".toRegex(), ""),
63+
).use { resultSet ->
64+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
65+
assertThat<String>(resultSet.getNString(1), equalTo<String>("101"))
66+
assertThat<String>(resultSet.getNString(2), equalTo<String>("Math"))
67+
assertThat<String>(resultSet.getNString(3), equalTo<String>("70"))
68+
assertThat<String>(resultSet.getNString(4), equalTo<String>("85"))
69+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
70+
assertThat<String>(resultSet.getNString(1), equalTo<String>("102"))
71+
assertThat<String>(resultSet.getNString(2), equalTo<String>("Math"))
72+
assertThat<String>(resultSet.getNString(3), equalTo<String>("80"))
73+
assertThat<String>(resultSet.getNString(4), equalTo<String>("85"))
74+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
75+
assertThat<String>(resultSet.getNString(1), equalTo<String>("104"))
76+
assertThat<String>(resultSet.getNString(2), equalTo<String>("Physics"))
77+
assertThat<String>(resultSet.getNString(3), equalTo<String>("75"))
78+
assertThat<String>(resultSet.getNString(4), equalTo<String>("85"))
79+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false))
80+
}
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)