Watch 10 video solutions for Invalid Tweets, a easy level problem involving Database. This walkthrough by Learn With Chirag has 11,071 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |