Table: Tweets
+----------------+---------+ | Column Name | Type | +----------------+---------+ | tweet_id | int | | content | varchar | +----------------+---------+ tweet_id is the primary key (column with unique values) for this table. content consists of characters on an American Keyboard, and no other special characters. This table contains all the tweets in a social media app.
Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Tweets table: +----------+-----------------------------------+ | tweet_id | content | +----------+-----------------------------------+ | 1 | Let us Code | | 2 | More than fifteen chars are here! | +----------+-----------------------------------+ Output: +----------+ | tweet_id | +----------+ | 2 | +----------+ Explanation: Tweet 1 has length = 11. It is a valid tweet. Tweet 2 has length = 33. It is an invalid tweet.
Problem Overview: The Invalid Tweets problem asks you to identify tweets whose content exceeds the platform’s character limit. Given a table Tweets(tweet_id, content), return the tweet_id for every tweet where the content length is greater than 15 characters.
Approach 1: SQL Query Filtering (O(n) time, O(1) space)
The most direct solution is a single SQL filter. Scan the Tweets table and compute the string length for each content value using a built-in function such as LENGTH() or CHAR_LENGTH(). Any row where the length exceeds 15 is considered invalid. The query engine performs a linear scan over the table, evaluates the condition, and returns the matching tweet_id. This approach is optimal for database problems because it pushes the filtering logic directly to the database engine and avoids unnecessary processing in application code.
Databases are optimized for operations like filtering and projection, so a simple condition such as WHERE LENGTH(content) > 15 executes efficiently. Time complexity is O(n) because each row is checked once, and extra space is O(1) since no additional data structures are created.
Approach 2: Iterative Filtering in Application Code (O(n) time, O(1) space)
If the tweets are already loaded into memory as objects or records, you can iterate through them and check the length of the content string. For each tweet, compute len(content) (or equivalent) and append the tweet_id to the result list when the value exceeds 15. This works the same way across languages such as Python, Java, C++, C#, or JavaScript.
The algorithm is straightforward: iterate over the collection once, evaluate a length condition, and collect the matching IDs. Each tweet is processed exactly one time, giving O(n) time complexity. Only a small output list is maintained, so auxiliary space remains O(1) aside from the result itself.
This approach appears frequently in introductory problems involving string processing and basic array iteration. The SQL variant falls under core database querying patterns.
Recommended for interviews: Interviewers typically expect the SQL filtering approach when the problem is presented as a database query. Writing a concise condition such as WHERE LENGTH(content) > 15 demonstrates that you understand how to leverage built‑in database functions efficiently. The iterative approach still shows correct reasoning about string length checks, but pushing the logic into SQL is the cleaner and more idiomatic solution.
This approach involves using SQL to filter out invalid tweets directly from the database. The key is to use the WHERE clause to check the length of the tweet content and return those with a length strictly greater than 15.
The SQL solution utilizes the LENGTH function to determine the length of each tweet's content. The WHERE clause effectively filters these lengths and retrieves the tweet IDs of any entries with a content length greater than 15.
SQL
Time Complexity: O(n), where n is the number of tweets in the table. This is due to the need to calculate the length for each tweet.
Space Complexity: O(1), as we are using in-place operations without any additional data structures.
This approach reads all tweets and checks their content length programmatically. It uses an iterative process to filter out the invalid tweets by measuring the length of each tweet's content and collecting the IDs of those that are longer than 15 characters.
In this C solution, we iterate over the array of tweet content, using the strlen function to get the number of characters in each tweet. If the length exceeds 15, we print the corresponding tweet ID. This simulates filtering the tweets manually, similar to SQL but done in C.
Time Complexity: O(n), where n is the number of tweets, as each tweet is checked once.
Space Complexity: O(1), constant extra space used regardless of input size.
The CHAR_LENGTH() function returns the length of a string, where Chinese characters, numbers, and letters are all counted as 1 byte.
The LENGTH() function returns the length of a string, where under utf8 encoding, Chinese characters are counted as 3 bytes, while numbers and letters are counted as 1 byte; under gbk encoding, Chinese characters are counted as 2 bytes, while numbers and letters are counted as 1 byte.
For this problem, we can directly use the CHAR_LENGTH function to get the length of the string, and filter out the tweet IDs with a length greater than 15.
MySQL
| Approach | Complexity |
|---|---|
| SQL Query Approach | Time Complexity: O(n), where n is the number of tweets in the table. This is due to the need to calculate the length for each tweet. |
| Iterative Filtering Approach | Time Complexity: O(n), where n is the number of tweets, as each tweet is checked once. |
| Using `CHAR_LENGTH` Function | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| SQL Query Filtering | O(n) | O(1) | When solving directly in SQL or querying a database table |
| Iterative Filtering in Application Code | O(n) | O(1) | When tweets are already loaded into memory as objects or arrays |
Invalid Tweets | Leetcode 1683 | SQL_50 Study Plan | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 11,071 views views
Watch 9 more video solutions →Practice Invalid Tweets with our built-in code editor and test cases.
Practice on FleetCode