Skip to content

Commit 4ebde8b

Browse files
authored
Added task 3611
1 parent a786bd6 commit 4ebde8b

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
3611\. Find Overbooked Employees
2+
3+
Medium
4+
5+
Table: `employees`
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| employee_id | int |
11+
| employee_name | varchar |
12+
| department | varchar |
13+
+---------------+---------+
14+
employee_id is the unique identifier for this table.
15+
Each row contains information about an employee and their department.
16+
17+
Table: `meetings`
18+
19+
+---------------+---------+
20+
| Column Name | Type |
21+
+---------------+---------+
22+
| meeting_id | int |
23+
| employee_id | int |
24+
| meeting_date | date |
25+
| meeting_type | varchar |
26+
| duration_hours| decimal |
27+
+---------------+---------+
28+
meeting_id is the unique identifier for this table.
29+
Each row represents a meeting attended by an employee. meeting_type can be 'Team', 'Client', or 'Training'.
30+
31+
Write a solution to find employees who are **meeting-heavy** - employees who spend more than `50%` of their working time in meetings during any given week.
32+
33+
* Assume a standard work week is `40` **hours**
34+
* Calculate **total meeting hours** per employee **per week** (**Monday to Sunday**)
35+
* An employee is meeting-heavy if their weekly meeting hours `>` `20` hours (`50%` of `40` hours)
36+
* Count how many weeks each employee was meeting-heavy
37+
* **Only include** employees who were meeting-heavy for **at least** `2` **weeks**
38+
39+
Return _the result table ordered by the number of meeting-heavy weeks in **descending** order, then by employee name in **ascending** order_.
40+
41+
The result format is in the following example.
42+
43+
**Example:**
44+
45+
**Input:**
46+
47+
employees table:
48+
49+
+-------------+----------------+-------------+
50+
| employee_id | employee_name | department |
51+
+-------------+----------------+-------------+
52+
| 1 | Alice Johnson | Engineering |
53+
| 2 | Bob Smith | Marketing |
54+
| 3 | Carol Davis | Sales |
55+
| 4 | David Wilson | Engineering |
56+
| 5 | Emma Brown | HR |
57+
+-------------+----------------+-------------+
58+
59+
meetings table:
60+
61+
+------------+-------------+--------------+--------------+----------------+
62+
| meeting_id | employee_id | meeting_date | meeting_type | duration_hours |
63+
+------------+-------------+--------------+--------------+----------------+
64+
| 1 | 1 | 2023-06-05 | Team | 8.0 |
65+
| 2 | 1 | 2023-06-06 | Client | 6.0 |
66+
| 3 | 1 | 2023-06-07 | Training | 7.0 |
67+
| 4 | 1 | 2023-06-12 | Team | 12.0 |
68+
| 5 | 1 | 2023-06-13 | Client | 9.0 |
69+
| 6 | 2 | 2023-06-05 | Team | 15.0 |
70+
| 7 | 2 | 2023-06-06 | Client | 8.0 |
71+
| 8 | 2 | 2023-06-12 | Training | 10.0 |
72+
| 9 | 3 | 2023-06-05 | Team | 4.0 |
73+
| 10 | 3 | 2023-06-06 | Client | 3.0 |
74+
| 11 | 4 | 2023-06-05 | Team | 25.0 |
75+
| 12 | 4 | 2023-06-19 | Client | 22.0 |
76+
| 13 | 5 | 2023-06-05 | Training | 2.0 |
77+
+------------+-------------+--------------+--------------+----------------+
78+
79+
**Output:**
80+
81+
+-------------+---------------+-------------+---------------------+
82+
| employee_id | employee_name | department | meeting_heavy_weeks |
83+
+-------------+---------------+-------------+---------------------+
84+
| 1 | Alice Johnson | Engineering | 2 |
85+
| 4 | David Wilson | Engineering | 2 |
86+
+-------------+---------------+-------------+---------------------+
87+
88+
**Explanation:**
89+
90+
* **Alice Johnson (employee\_id = 1):**
91+
* Week of June 5-11 (2023-06-05 to 2023-06-11): 8.0 + 6.0 + 7.0 = 21.0 hours (> 20 hours)
92+
* Week of June 12-18 (2023-06-12 to 2023-06-18): 12.0 + 9.0 = 21.0 hours (> 20 hours)
93+
* Meeting-heavy for 2 weeks
94+
* **David Wilson (employee\_id = 4):**
95+
* Week of June 5-11: 25.0 hours (> 20 hours)
96+
* Week of June 19-25: 22.0 hours (> 20 hours)
97+
* Meeting-heavy for 2 weeks
98+
* **Employees not included:**
99+
* Bob Smith (employee\_id = 2): Week of June 5-11: 15.0 + 8.0 = 23.0 hours (> 20), Week of June 12-18: 10.0 hours (< 20). Only 1 meeting-heavy week
100+
* Carol Davis (employee\_id = 3): Week of June 5-11: 4.0 + 3.0 = 7.0 hours (< 20). No meeting-heavy weeks
101+
* Emma Brown (employee\_id = 5): Week of June 5-11: 2.0 hours (< 20). No meeting-heavy weeks
102+
103+
The result table is ordered by meeting\_heavy\_weeks in descending order, then by employee name in ascending order.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2025_07_09_Time_516_ms_(100.00%)_Space_0.0_MB_(100.00%)
3+
WITH process_1 AS (
4+
SELECT
5+
employee_id,
6+
SUM(duration_hours) AS duration_total
7+
FROM
8+
meetings
9+
GROUP BY
10+
employee_id,
11+
WEEKOFYEAR(meeting_date),
12+
YEAR(meeting_date)
13+
)
14+
SELECT
15+
p.employee_id,
16+
e.employee_name,
17+
e.department,
18+
COUNT(p.employee_id) AS meeting_heavy_weeks
19+
FROM
20+
process_1 p
21+
INNER JOIN employees e ON p.employee_id = e.employee_id
22+
WHERE
23+
duration_total > 20
24+
GROUP BY
25+
p.employee_id,
26+
e.employee_name,
27+
e.department
28+
HAVING
29+
COUNT(p.employee_id) > 1
30+
ORDER BY
31+
meeting_heavy_weeks DESC,
32+
employee_name ASC;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package g3601_3700.s3611_find_overbooked_employees;
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 employees ("
24+
+ " employee_id INTEGER,"
25+
+ " employee_name VARCHAR(50),"
26+
+ " department VARCHAR(50)"
27+
+ ");"
28+
+ "INSERT INTO employees (employee_id, employee_name, department) VALUES"
29+
+ " (1, 'Alice Johnson', 'Engineering'),"
30+
+ " (2, 'Bob Smith', 'Marketing'),"
31+
+ " (3, 'Carol Davis', 'Sales'),"
32+
+ " (4, 'David Wilson', 'Engineering'),"
33+
+ " (5, 'Emma Brown', 'HR');"
34+
+ "CREATE TABLE meetings ("
35+
+ " meeting_id INTEGER,"
36+
+ " employee_id INTEGER,"
37+
+ " meeting_date DATE,"
38+
+ " meeting_type VARCHAR(20),"
39+
+ " duration_hours DECIMAL(4,1)"
40+
+ ");"
41+
+ "INSERT INTO meetings (meeting_id, employee_id, "
42+
+ "meeting_date, meeting_type, duration_hours) VALUES"
43+
+ " (1, 1, '2023-06-05', 'Team', 8.0),"
44+
+ " (2, 1, '2023-06-06', 'Client', 6.0),"
45+
+ " (3, 1, '2023-06-07', 'Training', 7.0),"
46+
+ " (4, 1, '2023-06-12', 'Team', 12.0),"
47+
+ " (5, 1, '2023-06-13', 'Client', 9.0),"
48+
+ " (6, 2, '2023-06-05', 'Team', 15.0),"
49+
+ " (7, 2, '2023-06-06', 'Client', 8.0),"
50+
+ " (8, 2, '2023-06-12', 'Training', 10.0),"
51+
+ " (9, 3, '2023-06-05', 'Team', 4.0),"
52+
+ " (10, 3, '2023-06-06', 'Client', 3.0),"
53+
+ " (11, 4, '2023-06-05', 'Team', 25.0),"
54+
+ " (12, 4, '2023-06-19', 'Client', 22.0),"
55+
+ " (13, 5, '2023-06-05', 'Training', 2.0);")
56+
class MysqlTest {
57+
@Test
58+
void testScript(@EmbeddedDatabase DataSource dataSource)
59+
throws SQLException, FileNotFoundException {
60+
try (final Connection connection = dataSource.getConnection()) {
61+
try (final Statement statement = connection.createStatement();
62+
final ResultSet resultSet =
63+
statement.executeQuery(
64+
new BufferedReader(
65+
new FileReader(
66+
"src/main/java/g3601_3700/"
67+
+ "s3611_find_overbooked_employees/"
68+
+ "script.sql"))
69+
.lines()
70+
.collect(Collectors.joining("\n"))
71+
.replaceAll("#.*?\\r?\\n", "")
72+
.replace("WEEKOFYEAR", "ISO_WEEK"))) {
73+
assertThat(resultSet.next(), equalTo(true));
74+
assertThat(resultSet.getNString(1), equalTo("1"));
75+
assertThat(resultSet.getNString(2), equalTo("Alice Johnson"));
76+
assertThat(resultSet.getNString(3), equalTo("Engineering"));
77+
assertThat(resultSet.getNString(4), equalTo("2"));
78+
assertThat(resultSet.next(), equalTo(true));
79+
assertThat(resultSet.getNString(1), equalTo("4"));
80+
assertThat(resultSet.getNString(2), equalTo("David Wilson"));
81+
assertThat(resultSet.getNString(3), equalTo("Engineering"));
82+
assertThat(resultSet.getNString(4), equalTo("2"));
83+
assertThat(resultSet.next(), equalTo(false));
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)