Skip to content

Commit 2491b37

Browse files
authored
Added task 3611
1 parent 99dac72 commit 2491b37

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package g3601_3700.s3611_find_overbooked_employees
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 employees (" +
21+
" employee_id INTEGER," +
22+
" employee_name VARCHAR(50)," +
23+
" department VARCHAR(50)" +
24+
");" +
25+
"INSERT INTO employees (employee_id, employee_name, department) VALUES" +
26+
" (1, 'Alice Johnson', 'Engineering')," +
27+
" (2, 'Bob Smith', 'Marketing')," +
28+
" (3, 'Carol Davis', 'Sales')," +
29+
" (4, 'David Wilson', 'Engineering')," +
30+
" (5, 'Emma Brown', 'HR');" +
31+
"CREATE TABLE meetings (" +
32+
" meeting_id INTEGER," +
33+
" employee_id INTEGER," +
34+
" meeting_date DATE," +
35+
" meeting_type VARCHAR(20)," +
36+
" duration_hours DECIMAL(4,1)" +
37+
");" +
38+
"INSERT INTO meetings (meeting_id, employee_id, " +
39+
"meeting_date, meeting_type, duration_hours) VALUES" +
40+
" (1, 1, '2023-06-05', 'Team', 8.0)," +
41+
" (2, 1, '2023-06-06', 'Client', 6.0)," +
42+
" (3, 1, '2023-06-07', 'Training', 7.0)," +
43+
" (4, 1, '2023-06-12', 'Team', 12.0)," +
44+
" (5, 1, '2023-06-13', 'Client', 9.0)," +
45+
" (6, 2, '2023-06-05', 'Team', 15.0)," +
46+
" (7, 2, '2023-06-06', 'Client', 8.0)," +
47+
" (8, 2, '2023-06-12', 'Training', 10.0)," +
48+
" (9, 3, '2023-06-05', 'Team', 4.0)," +
49+
" (10, 3, '2023-06-06', 'Client', 3.0)," +
50+
" (11, 4, '2023-06-05', 'Team', 25.0)," +
51+
" (12, 4, '2023-06-19', 'Client', 22.0)," +
52+
" (13, 5, '2023-06-05', 'Training', 2.0);"
53+
),
54+
],
55+
)
56+
internal class MysqlTest {
57+
@Test
58+
@Throws(SQLException::class, FileNotFoundException::class)
59+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
60+
dataSource.connection.use { connection ->
61+
connection.createStatement().use { statement ->
62+
statement.executeQuery(
63+
BufferedReader(
64+
FileReader(
65+
(
66+
"src/main/kotlin/g3601_3700/" +
67+
"s3611_find_overbooked_employees/" +
68+
"script.sql"
69+
),
70+
),
71+
)
72+
.lines()
73+
.collect(Collectors.joining("\n"))
74+
.replace("#.*?\\r?\\n".toRegex(), "")
75+
.replace("WEEKOFYEAR", "ISO_WEEK"),
76+
).use { resultSet ->
77+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
78+
assertThat<String>(resultSet.getNString(1), equalTo<String>("1"))
79+
assertThat<String>(
80+
resultSet.getNString(2),
81+
equalTo<String>("Alice Johnson"),
82+
)
83+
assertThat<String>(
84+
resultSet.getNString(3),
85+
equalTo<String>("Engineering"),
86+
)
87+
assertThat<String>(resultSet.getNString(4), equalTo<String>("2"))
88+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
89+
assertThat<String>(resultSet.getNString(1), equalTo<String>("4"))
90+
assertThat<String>(
91+
resultSet.getNString(2),
92+
equalTo<String>("David Wilson"),
93+
)
94+
assertThat<String>(
95+
resultSet.getNString(3),
96+
equalTo<String>("Engineering"),
97+
)
98+
assertThat<String>(resultSet.getNString(4), equalTo<String>("2"))
99+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false))
100+
}
101+
}
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)