Find trending topics in past n hours – code

This post is about how to find trending topics that were talked about most in twitter or facebook in past n hours (or days, months or years). To start with, the tweet can be defined as class with two attributes, topic and timeStamp . To keep track the topic’s counts, we can use a HashMap to store the entry as (topic, count) pair. After reading data stream, apply sort by value to get the topic that has the highest counts. If we just care the first one, we can use PriorityQueue and lambda expression to sort.

Amazon Interview Question:
Design twitter trending topics feature to show trending topics in past 24 hours.


Java Code :

Output:
find trending topics past 2 hour is : climate change

O Notation:
Time complexity: O(n*logn)
Space complexity: O(n)


Download FindTrendingTopicsLastHour.java
Sort with Lambda comparator – easy code
Java coding interview youtube series

Comments are closed