Skip to main content

Find Trending Hashtags II - Solution & Explanation

HardPremiumFree on FleetCodeDatabase5 min read
Practice this problem

Problem Statement

Table: Tweets

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| tweet_id    | int     |
| tweet_date  | date    |
| tweet       | varchar |
+-------------+---------+
tweet_id is the primary key (column with unique values) for this table.
Each row of this table contains user_id, tweet_id, tweet_date and tweet.
It is guaranteed that all tweet_date are valid dates in February 2024.

Write a solution to find the top 3 trending hashtags in February 2024. Every tweet may contain several hashtags.

Return the result table ordered by count of hashtag, hashtag in descending order.

The result format is in the following example.

 

Example 1:

Input:

Tweets table:

+---------+----------+------------------------------------------------------------+------------+
| user_id | tweet_id | tweet                                                      | tweet_date |
+---------+----------+------------------------------------------------------------+------------+
| 135     | 13       | Enjoying a great start to the day. #HappyDay #MorningVibes | 2024-02-01 |
| 136     | 14       | Another #HappyDay with good vibes! #FeelGood               | 2024-02-03 |
| 137     | 15       | Productivity peaks! #WorkLife #ProductiveDay               | 2024-02-04 |
| 138     | 16       | Exploring new tech frontiers. #TechLife #Innovation        | 2024-02-04 |
| 139     | 17       | Gratitude for today's moments. #HappyDay #Thankful         | 2024-02-05 |
| 140     | 18       | Innovation drives us. #TechLife #FutureTech                | 2024-02-07 |
| 141     | 19       | Connecting with nature's serenity. #Nature #Peaceful       | 2024-02-09 |
+---------+----------+------------------------------------------------------------+------------+
 

Output:

+-----------+-------+
| hashtag   | count |
+-----------+-------+
| #HappyDay | 3     |
| #TechLife | 2     |
| #WorkLife | 1     |
+-----------+-------+

Explanation:

  • #HappyDay: Appeared in tweet IDs 13, 14, and 17, with a total count of 3 mentions.
  • #TechLife: Appeared in tweet IDs 16 and 18, with a total count of 2 mentions.
  • #WorkLife: Appeared in tweet ID 15, with a total count of 1 mention.

Note: Output table is sorted in descending order by count and hashtag respectively.

Approach Overview

Problem Overview: The task asks you to analyze post or message text and identify trending hashtags. Each post may contain multiple hashtags embedded in the text, so you must extract them, normalize them, and count how frequently they appear across the dataset to determine which ones are trending.

Approach 1: Regex Extraction + Aggregation (O(n * m) time, O(k) space)

The key challenge is extracting hashtags from free‑form text. A regular expression such as #\w+ identifies hashtag tokens directly from each post. You iterate through each row of text, apply regex extraction, and output each match as an individual record. After extraction, you perform aggregation with GROUP BY on the hashtag and compute counts. Sorting the results by frequency identifies trending tags. Time complexity is O(n * m), where n is the number of posts and m is the average text length processed by the regex engine. Space complexity is O(k) for storing unique hashtags.

This approach maps well to database queries where regex functions such as REGEXP_EXTRACT, REGEXP_SUBSTR, or equivalent features generate hashtag matches. Once the hashtags are extracted, standard SQL aggregation (COUNT, GROUP BY, ORDER BY) determines the most frequent ones.

Approach 2: Regex Extraction with Ranking Window Functions (O(n * m + k log k) time, O(k) space)

If the problem requires selecting only the top trending hashtags or ranking them, window functions simplify the query. After extracting hashtags using a regular expression, aggregate counts per tag. Then apply ranking functions such as RANK() or DENSE_RANK() ordered by frequency descending. This avoids manual sorting logic and integrates naturally into analytical SQL workflows. The complexity remains dominated by regex scanning of each post (O(n * m)), plus sorting the aggregated results (O(k log k)) where k is the number of distinct hashtags.

This technique commonly appears in SQL interview questions involving text analytics. Regex handles extraction, while grouping and ranking identify popularity patterns.

Recommended for interviews: Regex extraction combined with aggregation is the expected solution. Interviewers want to see that you can tokenize structured patterns from text using regular expressions, then leverage SQL grouping and sorting to compute frequency metrics. A naive manual string parsing approach demonstrates understanding of the problem, but the regex + aggregation method shows practical database querying skills.

Solution

We can use regular expressions to match all tags in each tweet, and then count the occurrence of each tag. Finally, we can sort the tags in descending order by the number of occurrences. If the number of occurrences is the same, we sort them in descending order by the tag name, and return the top three tags.

Code

Python

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Regex Extraction + GROUP BY AggregationO(n * m)O(k)General solution for extracting hashtags from text and counting their frequency
Regex Extraction + Ranking Window FunctionsO(n * m + k log k)O(k)When the query requires top trending hashtags or ordered ranking
Manual String ParsingO(n * m)O(k)Useful when regex functions are unavailable or restricted

Video Solution

Find Trending Hashtags II • Owen Wu • 35 views views

Frequently Asked Questions

Find Trending Hashtags II Python solution
In Python, you can extract hashtags using the re module with a pattern such as r'#\\w+'. Iterate through posts, collect matches, and store counts in a dictionary or collections.Counter. After counting, sort the dictionary by frequency to identify the trending hashtags.
Is Find Trending Hashtags II easy or hard?
The problem is rated Hard because it combines text parsing with database-style aggregation and ranking. Extracting multiple tokens from unstructured text and transforming them into analyzable records requires careful query design or regex handling.
How to solve Find Trending Hashtags II in O(n)?
Strict O(n) is difficult because each post must be scanned character by character. However, the practical solution runs in near‑linear time relative to total text size. Use regex to extract hashtags in one pass, emit each match as a row, then group by the hashtag and count occurrences.
Is Find Trending Hashtags II asked at Google/Amazon/Meta?
Text processing and aggregation problems like this appear frequently in data and backend interviews at large companies. Variations involving log analysis, hashtag extraction, or token frequency counting are common in companies that process large user-generated datasets.
What is the best approach for Find Trending Hashtags II ?
The most effective approach uses regular expression extraction combined with SQL aggregation. Extract hashtags from each text entry using a regex pattern like #\w+, then group by the hashtag and count occurrences. Sorting by the frequency identifies trending hashtags. This approach handles multiple hashtags per post and scales well for large datasets.
What data structure is used in Find Trending Hashtags II ?
Conceptually the solution uses a hash map or dictionary to count hashtag frequencies. In SQL implementations, this is represented through GROUP BY aggregation where the database engine internally maintains hash or sort-based structures for counting.
What is the time complexity of Find Trending Hashtags II ?
The dominant cost comes from scanning each post with a regular expression. If n is the number of posts and m is the average text length, regex extraction takes O(n * m) time. Aggregating k unique hashtags adds sorting cost of O(k log k) when ranking results.

Ready to solve this problem?

Practice Find Trending Hashtags II with our built-in code editor and test cases.

Practice on FleetCode