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 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
Tweets Tabletweet_idcontentLENGTH()Valid?Result1Vote for Biden14โœ“-2Let us make America great again!32โœ—2LENGTH(content)WHERE > 15Final ResultSELECT tweet_id FROM Tweets WHERE LENGTH(content) > 15tweet_id: 2
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing current count and result

n
2n
โœ“ 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)
Asked in
Twitter 85 Meta 42 TikTok 38 LinkedIn 25
89.2K Views
High Frequency
~8 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen