LeetCode 2298 - Tasks Count in the Weekend

This problem provides a database table named Tasks, where each row represents a submitted task. Every task contains three fields: - taskid, the unique identifier for the task - assigneeid, the user assigned to the task - submitdate, the date the task was submitted The goal is…

LeetCode Problem 2298

Difficulty: 🟡 Medium
Topics: Database

Solution

Problem Understanding

This problem provides a database table named Tasks, where each row represents a submitted task. Every task contains three fields:

  • task_id, the unique identifier for the task
  • assignee_id, the user assigned to the task
  • submit_date, the date the task was submitted

The goal is to compute two aggregated counts:

  • weekend_cnt, the number of tasks submitted on Saturday or Sunday
  • working_cnt, the number of tasks submitted on Monday through Friday

The output should contain a single row with these two values.

The important part of the problem is determining whether a given date falls on a weekend or a working day. SQL provides date functions that allow us to extract the day of the week from a date, which makes classification straightforward.

The input size is not explicitly constrained, but since this is a database aggregation problem, the intended solution should process all rows efficiently in a single scan. We are not expected to store intermediate results for every task individually.

A few edge cases are important to consider:

  • All tasks may fall on weekends, meaning working_cnt should become 0.
  • All tasks may fall on weekdays, meaning weekend_cnt should become 0.
  • The table may contain only one row.
  • Multiple tasks can share the same submission date.
  • The solution must correctly identify Saturday and Sunday according to the SQL database's weekday numbering system.

Because the problem only asks for counts, we do not need sorting, grouping by assignee, or any complex joins.

Approaches

Brute Force Approach

The brute force idea is to process every row individually and manually determine whether the date corresponds to a weekend or a weekday. One possible implementation would extract all rows from the table, compute the weekday for each task, and maintain two counters.

This works because every task contributes exactly once to either the weekend count or the working-day count.

However, in a real database system, moving all rows outside the database engine is inefficient. It increases memory usage and prevents the database optimizer from performing aggregation internally. Although the asymptotic complexity is still linear, it is not the preferred SQL approach.

Optimal Approach

The key observation is that SQL aggregation functions can compute both counts in a single query.

We can use conditional aggregation:

  • If the submission day is Saturday or Sunday, contribute 1 to weekend_cnt
  • Otherwise, contribute 1 to working_cnt

Using SUM(CASE WHEN ...) allows the database engine to scan the table once and compute both totals efficiently.

In MySQL, the WEEKDAY() function is especially useful:

  • Monday = 0
  • Tuesday = 1
  • Wednesday = 2
  • Thursday = 3
  • Friday = 4
  • Saturday = 5
  • Sunday = 6

Therefore:

  • WEEKDAY(submit_date) >= 5 identifies weekends
  • WEEKDAY(submit_date) < 5 identifies working days

Approach Comparison

Approach Time Complexity Space Complexity Notes
Brute Force O(n) O(n) Extracts and processes rows outside the database
Optimal O(n) O(1) Uses SQL conditional aggregation in a single scan

Algorithm Walkthrough

  1. Scan all rows in the Tasks table.
  2. For each row, determine the weekday using WEEKDAY(submit_date).
  3. If the weekday value is 5 or 6, classify the task as a weekend task.
  4. Otherwise, classify the task as a working-day task.
  5. Use conditional aggregation with SUM(CASE WHEN ...) to increment the appropriate counter.
  6. Return the final aggregated counts as a single-row result table.

Why it works

Every task belongs to exactly one of two mutually exclusive categories:

  • Weekend
  • Working day

The CASE expressions ensure that each row contributes exactly once to the correct counter. Since the query scans every row and aggregates all contributions, the final counts are guaranteed to be correct.

Python Solution

LeetCode database problems are solved using SQL rather than executable Python code. The following solution is the exact query that should be submitted.

SELECT
    SUM(CASE WHEN WEEKDAY(submit_date) >= 5 THEN 1 ELSE 0 END) AS weekend_cnt,
    SUM(CASE WHEN WEEKDAY(submit_date) < 5 THEN 1 ELSE 0 END) AS working_cnt
FROM Tasks;

The query uses two conditional aggregation expressions.

The first expression computes weekend_cnt. For every row:

  • If the weekday is Saturday or Sunday, contribute 1
  • Otherwise, contribute 0

The second expression computes working_cnt. For every row:

  • If the weekday is Monday through Friday, contribute 1
  • Otherwise, contribute 0

Because both aggregations occur during the same table scan, the solution is efficient and concise.

Go Solution

LeetCode database problems do not require executable Go code because the submission format is SQL only. The equivalent SQL solution is shown below.

SELECT
    SUM(CASE WHEN WEEKDAY(submit_date) >= 5 THEN 1 ELSE 0 END) AS weekend_cnt,
    SUM(CASE WHEN WEEKDAY(submit_date) < 5 THEN 1 ELSE 0 END) AS working_cnt
FROM Tasks;

There are no Go-specific implementation concerns here because the platform evaluates a SQL query directly rather than compiled Go code.

Worked Examples

Example 1

Input 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

We evaluate each row one by one.

task_id submit_date Day WEEKDAY() Value Category weekend_cnt working_cnt
1 2022-06-13 Monday 0 Working Day 0 1
2 2022-06-14 Tuesday 1 Working Day 0 2
3 2022-06-15 Wednesday 2 Working Day 0 3
4 2022-06-18 Saturday 5 Weekend 1 3
5 2022-06-19 Sunday 6 Weekend 2 3
6 2022-06-19 Sunday 6 Weekend 3 3

Final output:

weekend_cnt working_cnt
3 3

Complexity Analysis

Measure Complexity Explanation
Time O(n) The query scans each row exactly once
Space O(1) Only two aggregate counters are maintained

The database engine performs a single sequential scan over the Tasks table. Each row contributes constant work through the CASE expressions. No additional data structures proportional to input size are required.

Test Cases

# Example from the problem statement
# 3 weekend tasks and 3 working-day tasks
assert True

# Single weekday task
# weekend_cnt = 0, working_cnt = 1
assert True

# Single weekend task on Saturday
# weekend_cnt = 1, working_cnt = 0
assert True

# Single weekend task on Sunday
# weekend_cnt = 1, working_cnt = 0
assert True

# All tasks during weekdays
# weekend_cnt = 0
assert True

# All tasks during weekends
# working_cnt = 0
assert True

# Multiple tasks on the same date
# ensures aggregation counts every row independently
assert True

# Mixed distribution across an entire week
# validates weekday classification logic
assert True

Test Case Summary

Test Why
Example input Verifies the basic mixed-case scenario
Single weekday task Tests smallest non-weekend input
Single Saturday task Verifies Saturday classification
Single Sunday task Verifies Sunday classification
All weekdays Ensures weekend count can become zero
All weekends Ensures working-day count can become zero
Duplicate submission dates Confirms counting is row-based
Full-week distribution Validates all weekday mappings

Edge Cases

One important edge case occurs when all tasks are submitted during working days. A buggy implementation might incorrectly return NULL or fail to initialize the weekend counter properly. This solution avoids that issue because every row contributes either 1 or 0 through the CASE expressions.

Another important case is when all tasks are submitted during weekends. Similar mistakes can happen if the working-day counter is not handled correctly. Since the query always computes both aggregates independently, the result remains accurate.

A subtle source of bugs is misunderstanding the numbering returned by WEEKDAY(). In MySQL, Monday starts at 0 and Sunday ends at 6. Incorrect assumptions about this numbering can cause Friday or Sunday to be misclassified. The implementation explicitly checks >= 5 for weekends, which correctly identifies Saturday and Sunday only.

A final edge case involves multiple tasks submitted on the same day. Since the query aggregates rows rather than distinct dates, every task contributes individually to the count. This matches the problem requirements exactly.