Find Trending Hashtags - Problem

Given a table Tweets containing user tweets with hashtags, find the top 3 trending hashtags in February 2024.

Requirements:

  • Each tweet contains exactly one hashtag
  • Filter tweets from February 2024 only
  • Return top 3 hashtags by count
  • Order results by hashtag count (descending), then by hashtag name (descending)

Table Schema

Tweets
Column Name Type Description
user_id int ID of the user who posted the tweet
tweet_id PK int Primary key, unique identifier for each tweet
tweet_date date Date when the tweet was posted
tweet PK varchar Content of the tweet containing one hashtag
Primary Key: tweet_id
Note: Each tweet contains exactly one hashtag that needs to be extracted and counted

Input & Output

Example 1 — Basic Hashtag Counting
Input Table:
user_id tweet_id tweet_date tweet
1 101 2024-02-01 Love this #AI trend!
2 102 2024-02-02 Working on #AI project
3 103 2024-02-03 Excited about #tech
4 104 2024-02-04 New #blockchain news
5 105 2024-02-05 Learning #AI today
6 106 2024-01-30 January #AI post
Output:
hashtag hashtag_count
#AI 3
#tech 1
#blockchain 1
💡 Note:

From the February 2024 tweets, #AI appears 3 times (most frequent), while #tech and #blockchain each appear once. The January tweet is excluded. Results are ordered by count DESC, then hashtag DESC, so #tech comes before #blockchain alphabetically in descending order.

Example 2 — Tie Breaking by Hashtag Name
Input Table:
user_id tweet_id tweet_date tweet
1 201 2024-02-01 Love #python coding
2 202 2024-02-02 Great #java tutorial
3 203 2024-02-03 Amazing #react framework
4 204 2024-02-04 Building with #nodejs
Output:
hashtag hashtag_count
#react 1
#python 1
#nodejs 1
💡 Note:

All hashtags have the same count (1), so they are ordered by hashtag name in descending order: #react, #python, #nodejs, #java. Only the top 3 are returned.

Constraints

  • 1 ≤ user_id ≤ 10000
  • tweet_id is unique for each row
  • tweet_date is in YYYY-MM-DD format
  • Each tweet contains exactly one hashtag starting with #
  • Hashtag can contain letters, numbers, and underscores

Visualization

Tap to expand
Find Trending Hashtags INPUT tweet_id hashtag date 1 #AI 2024-02-01 2 #Tech 2024-02-05 3 #AI 2024-02-10 4 #Code 2024-02-12 5 #AI 2024-02-15 6 #Tech 2024-02-18 7 #Code 2024-01-20 Filter: Feb 2024 only Tweets Table 7 tweets total 6 in February 2024 ALGORITHM STEPS 1 Filter by Date WHERE date BETWEEN '2024-02-01' AND '2024-02-29' 2 Build Hash Map Count each hashtag #AI : 3 #Tech : 2 #Code : 1 (hashtag --> count) 3 Sort Results ORDER BY count DESC, hashtag DESC 4 Limit to Top 3 LIMIT 3 FINAL RESULT hashtag count 1 #AI 3 2 #Tech 2 3 #Code 1 OK - Top 3 Found Trending Hashtags February 2024 #AI #Tech #Code Key Insight: Hash Map provides O(n) time complexity for counting hashtags. Using GROUP BY with COUNT() in SQL achieves the same result. The hash approach efficiently tracks frequency by storing hashtag as key and count as value, enabling fast lookup and update operations. TutorialsPoint - Find Trending Hashtags | Hash Approach
Asked in
Meta 38 Twitter 45 LinkedIn 28
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