Invalid Tweets - Problem
Find Invalid Tweets
Imagine you're working as a backend engineer for a social media platform that has strict content policies. Your platform enforces a 15-character limit for all tweets to keep conversations concise and engaging.
You have access to a
Table Schema:
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| tweet_id | int |
| content | varchar |
+----------------+---------+
Goal: Write a SQL query to find all tweet IDs where the content length is strictly greater than 15 characters.
Note: The content can contain alphanumeric characters, exclamation marks (!), and spaces - no other special characters are allowed.
Imagine you're working as a backend engineer for a social media platform that has strict content policies. Your platform enforces a 15-character limit for all tweets to keep conversations concise and engaging.
You have access to a
Tweets table containing all user posts. Your task is to identify and return the IDs of all invalid tweets - those that exceed the 15-character limit.Table Schema:
Tweets+----------------+---------+
| Column Name | Type |
+----------------+---------+
| tweet_id | int |
| content | varchar |
+----------------+---------+
Goal: Write a SQL query to find all tweet IDs where the content length is strictly greater than 15 characters.
Note: The content can contain alphanumeric characters, exclamation marks (!), and spaces - no other special characters are allowed.
Input & Output
example_1.sql โ Basic Example
$
Input:
Tweets table:
+----------+-----------------------------------+
| tweet_id | content |
+----------+-----------------------------------+
| 1 | Vote for Biden |
| 2 | Let us make America great again! |
+----------+-----------------------------------+
โบ
Output:
+----------+
| tweet_id |
+----------+
| 2 |
+----------+
๐ก Note:
Tweet 1: 'Vote for Biden' has 14 characters (โค 15) โ Valid. Tweet 2: 'Let us make America great again!' has 32 characters (> 15) โ Invalid.
example_2.sql โ Edge Cases
$
Input:
Tweets table:
+----------+-----------------+
| tweet_id | content |
+----------+-----------------+
| 3 | Exactly 15 char |
| 4 | Exactly 16 chars|
| 5 | Short |
+----------+-----------------+
โบ
Output:
+----------+
| tweet_id |
+----------+
| 4 |
+----------+
๐ก Note:
Tweet 3: Exactly 15 characters (= 15) โ Valid. Tweet 4: 16 characters (> 15) โ Invalid. Tweet 5: 5 characters (< 15) โ Valid.
example_3.sql โ All Valid Tweets
$
Input:
Tweets table:
+----------+---------+
| tweet_id | content |
+----------+---------+
| 6 | Hi |
| 7 | Good |
| 8 | Morning |
+----------+---------+
โบ
Output:
+----------+
| tweet_id |
+----------+
+----------+
๐ก Note:
All tweets have โค 15 characters, so no invalid tweets are found. The result table is empty.
Visualization
Tap to expand
Understanding the Visualization
1
Load Tweets
Database loads all tweets from the Tweets table
2
Apply LENGTH()
SQL LENGTH() function calculates character count for each content
3
Filter Results
WHERE clause filters tweets with length > 15
4
Return IDs
SELECT returns only the tweet_id column of invalid tweets
Key Takeaway
๐ฏ Key Insight: SQL's LENGTH() function provides instant character counting, making this a simple one-liner: `SELECT tweet_id FROM Tweets WHERE LENGTH(content) > 15`
Time & Space Complexity
Time Complexity
O(n ร m)
Where n is number of tweets and m is average content length
โ Linear Growth
Space Complexity
O(1)
Only storing current count and result
โ Linear Space
Constraints
- 1 โค tweet_id โค 106
- 1 โค content.length โค 100
- content consists only of alphanumeric characters, exclamation marks (!), and spaces
- Each tweet_id is unique (primary key)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code