Quick Overview

This question evaluates string parsing and tokenization, frequency counting of elements, time-window filtering of events, and the ability to sort results with deterministic tie-breaking rules.

Find Top Hashtags

Company: F5Networks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given two arrays of length `n`: - `tweets[i]`: the content of the `i`-th tweet as a string - `timestamps[i]`: the publish time of the `i`-th tweet as an integer You are also given two integers: - `currentTime` - `timeWindow` Consider only tweets whose timestamp falls within the inclusive time interval: `[currentTime - timeWindow, currentTime]` A hashtag is any token in a tweet that starts with `#`. Count hashtag popularity by the number of times each hashtag appears in the tweets within the time window. Return the most popular at most 3 hashtags. Sort hashtags by descending count. If two hashtags have the same count, sort them lexicographically ascending. If fewer than 3 hashtags appear in the time window, return all of them.

Quick Answer: This question evaluates string parsing and tokenization, frequency counting of elements, time-window filtering of events, and the ability to sort results with deterministic tie-breaking rules.

You are given two arrays of length `n`: - `tweets[i]`: the content of the `i`-th tweet as a string - `timestamps[i]`: the publish time of the `i`-th tweet as an integer You are also given two integers `currentTime` and `timeWindow`. Consider only tweets whose timestamp falls within the inclusive interval `[currentTime - timeWindow, currentTime]`. A hashtag is any whitespace-delimited token in a tweet that starts with `#` (and has at least one character after the `#`). Count hashtag popularity by the number of times each hashtag appears across the tweets within the time window. Return the most popular **at most 3** hashtags. Sort hashtags by descending count; break ties by lexicographically ascending hashtag. If fewer than 3 distinct hashtags appear in the window, return all of them. Return the result as a list of hashtag strings (each including the leading `#`).

Constraints

  • 1 <= n <= 10^5 (tweets and timestamps have equal length)
  • Each tweet is a whitespace-separated string of tokens
  • A hashtag is a token starting with '#' followed by at least one character
  • 0 <= timestamps[i], currentTime <= 10^9
  • 0 <= timeWindow <= currentTime
  • Window bounds are inclusive: [currentTime - timeWindow, currentTime]

Examples

Input: (['Loving #python and #coding', 'I love #python', '#python rocks but #java too', '#old tweet'], [100, 90, 80, 10], 100, 30)

Expected Output: ['#python', '#coding', '#java']

Explanation: Window is [70, 100]; the tweet at ts=10 is excluded. #python appears 3 times, #coding once, #java once. Ties (#coding vs #java) broken lexicographically: '#coding' < '#java'.

Input: (['#a #b #c #d #e', '#a #b', '#a'], [50, 50, 50], 50, 0)

Expected Output: ['#a', '#b', '#c']

Explanation: Window [50,50] includes all three tweets. #a=3, #b=2, #c=1 (#d, #e each 1). Top 3 by count then name: #a, #b, #c.

Hints

  1. First filter to the tweets whose timestamp lies in the inclusive window [currentTime - timeWindow, currentTime].
  2. Split each surviving tweet on whitespace and keep only tokens that start with '#' (with something after it); tally them in a hash map.
  3. Sort the (hashtag, count) pairs by count descending, then by hashtag ascending, and take the first 3. Tie-breaking on the hashtag string is what distinguishes equal-count tags.

Loading coding console...