Invalid Tweets II - Problem

Given a table Tweets containing all tweets in a social media app, find all invalid tweets.

A tweet is considered invalid if it meets any of the following criteria:

  • It exceeds 140 characters in length
  • It has more than 3 mentions (words starting with @)
  • It includes more than 3 hashtags (words starting with #)

Return the result table ordered by tweet_id in ascending order.

Table Schema

Tweets
Column Name Type Description
tweet_id PK int Primary key, unique identifier for each tweet
content varchar The text content of the tweet
Primary Key: tweet_id
Note: Each row represents a single tweet in the social media app

Input & Output

Example 1 — Multiple Validation Rules
Input Table:
tweet_id content
1 Traveling, exploring, and living my best life @JaneSmith @SaraJohnson @LisaTaylor @MikeBrown #Foodie #Fitness #Learning
2 Just had the best dinner with friends! #Foodie #Friends #Fun
4 Working hard on my new project #Work #Goals #Productivity #Fun
Output:
tweet_id
1
4
💡 Note:

Tweet 1 contains 4 mentions (@JaneSmith, @SaraJohnson, @LisaTaylor, @MikeBrown) which exceeds the limit of 3. Tweet 4 contains 4 hashtags (#Work, #Goals, #Productivity, #Fun) which exceeds the limit of 3. Tweet 2 is valid with only 3 hashtags.

Example 2 — Character Length Violation
Input Table:
tweet_id content
5 This is a very long tweet that exceeds the 140 character limit by containing way too much text and going on and on about nothing important just to make it invalid
6 Short tweet #OK
Output:
tweet_id
5
💡 Note:

Tweet 5 exceeds 140 characters in length, making it invalid. Tweet 6 is short and valid with only 1 hashtag.

Example 3 — All Valid Tweets
Input Table:
tweet_id content
7 Good morning! @friend1 @friend2 #beautiful #day
8 Simple tweet with no violations
Output:
tweet_id
💡 Note:

Both tweets are valid: Tweet 7 has 2 mentions and 2 hashtags (within limits), and Tweet 8 has no mentions or hashtags. No results returned.

Constraints

  • 1 ≤ tweet_id ≤ 1000
  • 0 ≤ content.length ≤ 1000
  • Content may contain letters, digits, spaces, and special characters
  • Mentions start with '@' followed by alphanumeric characters
  • Hashtags start with '#' followed by alphanumeric characters

Visualization

Tap to expand
Invalid Tweets II - Hash Approach INPUT: Tweets Table tweet_id content 1 "Hello @user1 #coding is fun" 2 "@a @b @c @d #1 #2 #3 #4 long text..." 3 "Short valid tweet @one #tag" Invalid Criteria: 1. Length > 140 chars 2. Mentions (@) > 3 3. Hashtags (#) > 3 Any condition = INVALID ALGORITHM STEPS 1 Hash Count Count @ mentions in text mentions = count('@') 2 Hash Tags Count # hashtags in text hashtags = count('#') 3 Length Check Check if length > 140 len = LENGTH(content) 4 Filter Invalid WHERE any condition met OR logic combines all Hash Map Check tweet_2: {mentions:4} --> 4 > 3 = INVALID tweet_2: {hashtags:4} FINAL RESULT Invalid tweet_id 2 Validation Results Tweet 1: OK - All criteria pass Tweet 2: FAIL - 4 mentions > 3 FAIL - 4 hashtags > 3 Tweet 3: OK - All criteria pass Output: [2] Key Insight: Use hash-based counting to efficiently track @ mentions and # hashtags in each tweet. The OR logic ensures any single violation marks the tweet as invalid. ORDER BY tweet_id ASC for output. TutorialsPoint - Invalid Tweets II | Hash Approach
Asked in
Twitter 28 Facebook 22 Instagram 18 TikTok 15
28.5K Views
Medium Frequency
~12 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