Invalid Tweets - Problem

You are given a table Tweets containing tweet information from a social media app.

Problem: Find the IDs of invalid tweets where the content has strictly more than 15 characters.

Table Structure:

  • tweet_id (int): Primary key, unique identifier for each tweet
  • content (varchar): Tweet content with alphanumeric characters, '!', or ' ' only

Return the result in any order.

Table Schema

Tweets
Column Name Type Description
tweet_id PK int Primary key, unique identifier for each tweet
content varchar Tweet content consisting of alphanumeric characters, '!', or ' ' only
Primary Key: tweet_id
Note: Contains all tweets in the social media app

Input & Output

Example 1 — Mixed Valid and Invalid Tweets
Input Table:
tweet_id content
1 Hello World!
2 This is a very long tweet content that exceeds limit
3 Short tweet
Output:
tweet_id
2
💡 Note:

Tweet 1 has 12 characters, Tweet 3 has 11 characters (both ≤ 15, so valid). Tweet 2 has 54 characters (> 15, so invalid). Only tweet_id 2 is returned.

Example 2 — All Valid Tweets
Input Table:
tweet_id content
1 Hi there!
2 Good morning
3 15 char exactly
Output:
tweet_id
💡 Note:

All tweets have 15 or fewer characters. Tweet 1: 9 chars, Tweet 2: 12 chars, Tweet 3: 15 chars exactly. Since we need strictly greater than 15, no tweets are invalid.

Example 3 — Edge Case with Exactly 16 Characters
Input Table:
tweet_id content
1 Exactly16charstr
2 Normal tweet!
Output:
tweet_id
1
💡 Note:

Tweet 1 has exactly 16 characters, which is strictly greater than 15, making it invalid. Tweet 2 has 13 characters and is valid.

Constraints

  • 1 ≤ tweet_id ≤ 10^5
  • content consists of alphanumeric characters, '!', or ' ' only
  • 1 ≤ content.length ≤ 100

Visualization

Tap to expand
Invalid Tweets - Solution Visualization INPUT: Tweets Table tweet_id content 1 "Vote for TP" (11 chars) 2 "Let us learn programming!" (26 chars) INVALID 3 "Hello World" (11 chars) 4 "This is a very long tweet!" (27 chars) INVALID = Invalid (more than 15 chars) = Valid (15 chars or less) ALGORITHM STEPS 1 SELECT tweet_id Choose column to return 2 FROM Tweets Source table with data 3 CHAR_LENGTH(content) Count characters in content 4 WHERE length > 15 Filter invalid tweets only -- SQL Query: SELECT tweet_id FROM Tweets WHERE CHAR_LENGTH (content) > 15; FINAL RESULT Invalid Tweet IDs Found: tweet_id 2 4 Verification: ID 2: 26 chars > 15 - OK ID 4: 27 chars > 15 - OK 2 Rows Found Output: [2, 4] Key Insight: Use CHAR_LENGTH() instead of LENGTH() for accurate character count. LENGTH() counts bytes (problematic with multi-byte characters), while CHAR_LENGTH() counts actual characters. The condition uses strictly greater than (>) not >= to match "more than 15". TutorialsPoint - Invalid Tweets | Optimal Solution
Asked in
Meta 15 Twitter 12 TikTok 8
25.4K Views
High Frequency
~5 min Avg. Time
892 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