Skip to main content

Second Day Verification - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min read
Practice this problem

Problem Statement

Table: emails

+-------------+----------+
| Column Name | Type     | 
+-------------+----------+
| email_id    | int      |
| user_id     | int      |
| signup_date | datetime |
+-------------+----------+
(email_id, user_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the email ID, user ID, and signup date.

Table: texts

+---------------+----------+
| Column Name   | Type     | 
+---------------+----------+
| text_id       | int      |
| email_id      | int      |
| signup_action | enum     |
| action_date   | datetime |
+---------------+----------+
(text_id, email_id) is the primary key (combination of columns with unique values) for this table. 
signup_action is an enum type of ('Verified', 'Not Verified'). 
Each row of this table contains the text ID, email ID, signup action, and action date.

Write a Solution to find the user IDs of those who verified their sign-up on the second day.

Return the result table ordered by user_id in ascending order.

The result format is in the following example.

 

Example:

Input:

emails table:

+----------+---------+---------------------+
| email_id | user_id | signup_date         |
+----------+---------+---------------------+
| 125      | 7771    | 2022-06-14 09:30:00|
| 433      | 1052    | 2022-07-09 08:15:00|
| 234      | 7005    | 2022-08-20 10:00:00|
+----------+---------+---------------------+

texts table:

+---------+----------+--------------+---------------------+
| text_id | email_id | signup_action| action_date         |
+---------+----------+--------------+---------------------+
| 1       | 125      | Verified     | 2022-06-15 08:30:00|
| 2       | 433      | Not Verified | 2022-07-10 10:45:00|
| 4       | 234      | Verified     | 2022-08-21 09:30:00|
+---------+----------+--------------+---------------------+
    

Output:

+---------+
| user_id |
+---------+
| 7005    |
| 7771    |
+---------+

Explanation:

  • User with user_id 7005 and email_id 234 signed up on 2022-08-20 10:00:00 and verified on second day of the signup.
  • User with user_id 7771 and email_id 125 signed up on 2022-06-14 09:30:00 and verified on second day of the signup.

Approach Overview

Problem Overview: The task is to identify users who completed their verification exactly on the second day after their initial signup. You are given two tables representing different events (such as registration and verification). The goal is to connect those records and filter the cases where the verification date occurs one day after the signup date.

Approach 1: Correlated Subquery (O(n log n) time, O(1) extra space)

A straightforward way is to query the signup table and check for a matching verification event using a correlated subquery. For each user record, the query searches the verification table to see if a verification exists exactly one day after the signup date. This works by applying a DATE_ADD or equivalent date comparison inside the subquery. While simple to write, the database may execute the subquery repeatedly for each row unless indexes are present, which increases runtime on large datasets. This approach is useful when datasets are small or when writing quick exploratory queries.

Approach 2: Joining Two Tables (O(n log n) time with indexes, O(1) extra space)

The more efficient and cleaner approach uses an explicit JOIN between the signup table and the verification table. Join the tables on the user identifier (for example user_id) and then filter rows where the verification date equals the signup date plus one day. The join allows the database optimizer to use indexes and execute the lookup in a single relational operation rather than repeated subqueries. The key step is the date comparison condition such as verification_date = DATE_ADD(signup_date, INTERVAL 1 DAY). This pattern is common in database interview questions that test your ability to combine event tables and reason about time differences.

Because the problem only requires matching records across two datasets and filtering by a date offset, the solution relies on fundamental SQL JOIN operations. The database engine handles scanning and matching rows internally, so the logical complexity remains simple while still performing well with proper indexing.

Recommended for interviews: The JOIN-based approach is the one interviewers expect. It shows that you understand how relational tables interact and how to express event relationships with SQL conditions. Mentioning the subquery alternative demonstrates awareness of multiple query styles, but the JOIN solution is cleaner, easier for query planners to optimize, and aligns with common production SQL patterns.

Solution

We can join the two tables and then use the DATEDIFF function to calculate whether the difference between the registration date and the operation date is equal to 1, and whether the registration operation is Verified, to filter out the user IDs that meet the conditions.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Correlated SubqueryO(n log n) with indexesO(1)Small datasets or quick exploratory SQL queries
JOIN Between TablesO(n log n) with indexed joinsO(1)Preferred approach in interviews and production SQL queries

Video Solution

Leetcode 3172 - Second Day Verification - Solved & Explained by Everyday Data Science |Job Interview • Everyday Data Science • 889 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Second Day Verification easy or hard?
Second Day Verification is classified as an Easy database problem. The main concept is understanding how to join two tables and apply a date difference filter to detect events that occur exactly one day after another event.
Second Day Verification Python/Java solution
This is a database problem, so the primary solution is written in SQL (MySQL). In real applications, Python or Java would execute the SQL query through a database connector, but the core logic remains the JOIN and date comparison in SQL.
How to solve Second Day Verification in O(n)?
Use a direct JOIN between the two tables and filter with a date comparison such as verification_date = DATE_ADD(signup_date, INTERVAL 1 DAY). With proper indexing on the join key and date columns, the database engine can process the join close to linear scan behavior.
What is the best approach for Second Day Verification?
The most effective solution uses a SQL JOIN between the signup table and the verification table. Join the tables on the user identifier and filter rows where the verification date equals the signup date plus one day. This approach lets the database optimizer use indexes and avoids repeated subquery execution.
Is Second Day Verification asked at Google/Amazon/Meta?
Database join and event-timeline questions like this appear frequently in SQL interview rounds at large companies. Variations of this pattern are used in data engineering and analytics interviews at companies such as Amazon, Meta, and Google.
What data structure is used in Second Day Verification?
The problem relies on relational database tables and SQL JOIN operations rather than traditional in-memory data structures. Internally, the database engine may use indexed lookups or hash joins to match rows efficiently.
What is the time complexity of Second Day Verification?
The logical complexity of the SQL solution is typically O(n log n) when indexes are used for joins or lookups. Without indexes, the database may perform full scans that behave closer to O(n^2) in worst cases. Proper indexing on user_id and date columns keeps the query efficient.

Ready to solve this problem?

Practice Second Day Verification with our built-in code editor and test cases.

Practice on FleetCode