Skip to content

Commit b900a04

Browse files
authored
Added task 3465
1 parent 4b8aaf7 commit b900a04

File tree

3 files changed

+142
-0
lines changed
  • src
    • main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers
    • test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers

3 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3465\. Find Products with Valid Serial Numbers
2+
3+
Easy
4+
5+
Table: `products`
6+
7+
+--------------+------------+
8+
| Column Name | Type |
9+
+--------------+------------+
10+
| product_id | int |
11+
| product_name | varchar |
12+
| description | varchar |
13+
+--------------+------------+
14+
(product_id) is the unique key for this table.
15+
Each row in the table represents a product with its unique ID, name, and description.
16+
17+
Write a solution to find all products whose description **contains a valid serial number** pattern. A valid serial number follows these rules:
18+
19+
* It starts with the letters **SN** (case-sensitive).
20+
* Followed by exactly `4` digits.
21+
* It must have a hyphen (-) **followed by exactly** `4` digits.
22+
* The serial number must be within the description (it may not necessarily start at the beginning).
23+
24+
Return _the result table ordered by_ `product_id` _in **ascending** order_.
25+
26+
The result format is in the following example.
27+
28+
**Example:**
29+
30+
**Input:**
31+
32+
products table:
33+
34+
+------------+--------------+------------------------------------------------------+
35+
| product_id | product_name | description |
36+
+------------+--------------+------------------------------------------------------+
37+
| 1 | Widget A | This is a sample product with SN1234-5678 |
38+
| 2 | Widget B | A product with serial SN9876-1234 in the description |
39+
| 3 | Widget C | Product SN1234-56789 is available now |
40+
| 4 | Widget D | No serial number here |
41+
| 5 | Widget E | Check out SN4321-8765 in this description |
42+
+------------+--------------+------------------------------------------------------+
43+
44+
**Output:**
45+
46+
+------------+--------------+------------------------------------------------------+
47+
| product_id | product_name | description |
48+
+------------+--------------+------------------------------------------------------+
49+
| 1 | Widget A | This is a sample product with SN1234-5678 |
50+
| 2 | Widget B | A product with serial SN9876-1234 in the description |
51+
| 5 | Widget E | Check out SN4321-8765 in this description |
52+
+------------+--------------+------------------------------------------------------+
53+
54+
**Explanation:**
55+
56+
* **Product 1:** Valid serial number SN1234-5678
57+
* **Product 2:** Valid serial number SN9876-1234
58+
* **Product 3:** Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)
59+
* **Product 4:** No serial number in the description
60+
* **Product 5:** Valid serial number SN4321-8765
61+
62+
The result table is ordered by product\_id in ascending order.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2025_02_25_Time_716_ms_(100.00%)_Space_0.0_MB_(100.00%)
3+
SELECT * FROM products WHERE description REGEXP 'SN[0-9]{4}-[0-9]{4}$'
4+
OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+' ORDER BY product_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package g3401_3500.s3465_find_products_with_valid_serial_numbers;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.sql.Connection;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
" CREATE TABLE products ("
24+
+ " product_id INT,"
25+
+ " product_name VARCHAR(50),"
26+
+ " description VARCHAR(100)"
27+
+ ");"
28+
+ "insert into products (product_id, product_name, description) values "
29+
+ "(1, 'Widget A', 'This is a sample product with SN1234-5678');"
30+
+ "insert into products (product_id, product_name, description) values "
31+
+ "(2, 'Widget B', 'A product with serial SN9876-1234 in the description');"
32+
+ "insert into products (product_id, product_name, description) values "
33+
+ "(3, 'Widget C', 'Product SN1234-56789 is available now');"
34+
+ "insert into products (product_id, product_name, description) values "
35+
+ "(4, 'Widget D', 'No serial number here');"
36+
+ "insert into products (product_id, product_name, description) values "
37+
+ "(5, 'Widget E', 'Check out SN4321-8765 in this description');")
38+
class MysqlTest {
39+
@Test
40+
void testScript(@EmbeddedDatabase DataSource dataSource)
41+
throws SQLException, FileNotFoundException {
42+
try (final Connection connection = dataSource.getConnection()) {
43+
try (final Statement statement = connection.createStatement();
44+
final ResultSet resultSet =
45+
statement.executeQuery(
46+
new BufferedReader(
47+
new FileReader(
48+
"src/main/java/g3401_3500/"
49+
+ "s3465_find_products_with_valid_serial_numbers/"
50+
+ "script.sql"))
51+
.lines()
52+
.collect(Collectors.joining("\n"))
53+
.replaceAll("#.*?\\r?\\n", ""))) {
54+
assertThat(resultSet.next(), equalTo(true));
55+
assertThat(resultSet.getNString(1), equalTo("1"));
56+
assertThat(resultSet.getNString(2), equalTo("Widget A"));
57+
assertThat(
58+
resultSet.getNString(3),
59+
equalTo("This is a sample product with SN1234-5678"));
60+
assertThat(resultSet.next(), equalTo(true));
61+
assertThat(resultSet.getNString(1), equalTo("2"));
62+
assertThat(resultSet.getNString(2), equalTo("Widget B"));
63+
assertThat(
64+
resultSet.getNString(3),
65+
equalTo("A product with serial SN9876-1234 in the description"));
66+
assertThat(resultSet.next(), equalTo(true));
67+
assertThat(resultSet.getNString(1), equalTo("5"));
68+
assertThat(resultSet.getNString(2), equalTo("Widget E"));
69+
assertThat(
70+
resultSet.getNString(3),
71+
equalTo("Check out SN4321-8765 in this description"));
72+
assertThat(resultSet.next(), equalTo(false));
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)