Skip to main content

Tasks Count in the Weekend - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase3 min read
Practice this problem

Problem Statement

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.

 

Example 1:

Input: 
Tasks table:
+---------+-------------+-------------+
| task_id | assignee_id | submit_date |
+---------+-------------+-------------+
| 1       | 1           | 2022-06-13  |
| 2       | 6           | 2022-06-14  |
| 3       | 6           | 2022-06-15  |
| 4       | 3           | 2022-06-18  |
| 5       | 5           | 2022-06-19  |
| 6       | 7           | 2022-06-19  |
+---------+-------------+-------------+
Output: 
+-------------+-------------+
| weekend_cnt | working_cnt |
+-------------+-------------+
| 3           | 3           |
+-------------+-------------+
Explanation: 
Task 1 was submitted on Monday.
Task 2 was submitted on Tuesday.
Task 3 was submitted on Wednesday.
Task 4 was submitted on Saturday.
Task 5 was submitted on Sunday.
Task 6 was submitted on Sunday.
3 tasks were submitted during the weekend.
3 tasks were submitted during the working days.

Approach Overview

Problem Overview: Given a Tasks table with a task submission date, compute how many tasks were created during the weekend. Weekend days are Saturday and Sunday, so the query needs to identify rows where the date falls on those two days and count them.

Approach 1: Filter with DAYOFWEEK() (O(n) time, O(1) space)

Scan the table once and use MySQL’s DAYOFWEEK(date) function to determine the weekday for each record. In MySQL, DAYOFWEEK() returns 1 for Sunday and 7 for Saturday. Filter rows where the result is either 1 or 7, then apply COUNT(*) to compute the total number of weekend tasks. This works because the database engine evaluates the function for each row and performs a simple conditional filter before aggregation.

The key insight is that you do not need joins, subqueries, or grouping. The database can evaluate the weekday directly from the date column and perform the aggregation in a single pass. The query effectively performs a linear scan of the table, which is optimal for this type of filtering problem.

An alternative variant uses WEEKDAY(date), which returns values from 0 (Monday) to 6 (Sunday). In that case, you filter for 5 and 6 to represent Saturday and Sunday. Both functions solve the same problem; the choice depends on which weekday numbering system you prefer.

This problem mainly tests your understanding of SQL date functions and conditional filtering rather than complex database operations. Recognizing how to extract weekday information from a date column is a common pattern in database queries, especially in analytics and reporting workloads.

Recommended for interviews: The DAYOFWEEK() filtering approach is the expected solution. It demonstrates practical knowledge of SQL date functions and efficient aggregation in relational databases. Simpler logic with a single scan shows stronger SQL fluency than overcomplicated queries.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Filter with DAYOFWEEK()O(n)O(1)Standard MySQL solution when using Sunday=1 and Saturday=7 weekday numbering
Filter with WEEKDAY()O(n)O(1)Preferred when using Monday=0 to Sunday=6 numbering for easier weekend checks

Video Solution

LeetCode Medium 2298 "Tasks Count in the Weekend" Interview SQL Question with Detailed Explanation • Everyday Data Science • 1,801 views views

Frequently Asked Questions

Is Tasks Count in the Weekend easy or hard?
Tasks Count in the Weekend is generally considered a medium-level SQL problem. The logic is simple once you know how weekday extraction functions work, but it checks practical knowledge of SQL date handling and conditional filtering.
Tasks Count in the Weekend Python/Java solution
This problem is designed for SQL rather than Python or Java. The solution is typically written in MySQL using date functions like DAYOFWEEK() or WEEKDAY() combined with COUNT(*) to compute the total number of tasks submitted on weekend days.
How to solve Tasks Count in the Weekend in O(n)?
Perform a single scan of the Tasks table and apply a date function to determine the weekday. In MySQL, use DAYOFWEEK(submit_date) and filter for values 1 and 7 to represent Sunday and Saturday. After filtering, apply COUNT(*) to return the total number of weekend tasks.
What is the best approach for Tasks Count in the Weekend?
The best approach filters rows using a SQL weekday function such as DAYOFWEEK() or WEEKDAY() and counts the matching rows. In MySQL, DAYOFWEEK(date) returns 1 for Sunday and 7 for Saturday, so filtering those values directly identifies weekend tasks. The query performs a single table scan with O(n) time and constant space.
Is Tasks Count in the Weekend asked at Google/Amazon/Meta?
Problems like Tasks Count in the Weekend appear frequently in SQL interview rounds across companies such as Amazon, Google, and Meta. The focus is usually on understanding date functions, filtering conditions, and writing efficient aggregation queries on relational tables.
What data structure is used in Tasks Count in the Weekend?
The problem operates on a relational database table rather than traditional in-memory data structures. The main concept involves SQL date functions and filtering operations within a database query engine.
What is the time complexity of Tasks Count in the Weekend?
The time complexity is O(n) because the database must evaluate the weekday function for each row in the Tasks table. Aggregation with COUNT(*) happens during the same scan. Space complexity is O(1) since no additional structures or intermediate tables are required.

Ready to solve this problem?

Practice Tasks Count in the Weekend with our built-in code editor and test cases.

Practice on FleetCode