Problem
Table: Tasks
+-------------+------+
| Column Name | Type |
+-------------+------+
| task_id | int |
| assignee_id | int |
| submit_date | date |
+-------------+------+
task_id is the primary key (column with unique values) for this table.
Each row in this table contains the ID of a task, the id of the assignee, and the submission date.
Write a solution to report:
- the number of tasks that were submitted during the weekend (Saturday, Sunday) as
weekend_cnt
, and - the number of tasks that were submitted during the working days as
working_cnt
.
Return the result table in any order.
The result format is shown in the following example.
Examples
Example 1:
|
|
Solution
Approach
We count the number of tasks submitted on weekends (Saturday, Sunday) and working days. We use the day-of-week function to distinguish between them.
Code
MySQL
|
|
Oracle
|
|
PostgreSQL
|
|
Explanation
We use date functions to determine the day of the week for each submission. In MySQL, DAYOFWEEK
returns 1 for Sunday and 7 for Saturday. In Oracle, TO_CHAR(..., 'D')
returns the day number (may depend on NLS settings). In PostgreSQL, ISODOW
returns 6 for Saturday and 7 for Sunday.
Complexity
- ⏰ Time complexity:
O(N)
where N is the number of tasks. - 🧺 Space complexity:
O(1)
.