Extract Hashtags from a Tweet
Updated
Problem statement
Given a string tweet, return every hashtag word in it, in the order the words appear.
A hashtag is a # followed by one or more word characters. A word character is an English letter (a to z or A to Z), a digit 0 to 9, or an underscore _. The hashtag word is the longest run of word characters right after the #, without the # itself. It ends at the first character that is not a word character, or at the end of tweet.
- A
#that is not followed by a word character starts no hashtag. This includes a#at the end oftweetand a#followed by a space, a punctuation mark or another#. - A
#starts a hashtag wherever it appears, even right after a letter, soa#bcontains the hashtag wordb. - Keep each hashtag word exactly as written, with the same uppercase and lowercase letters. If a word appears more than once, keep it once for each time it appears.
If tweet has no hashtags, return an empty list.
Function
extractHashtags(tweet: String) → List<String>Examples
Example 1
tweet = "this is a tweet! #awesome #lovingit"return = ["awesome","lovingit"]The first # is followed by awesome, which ends at the space. The second is followed by lovingit, which ends at the end of the string. The ! is not part of any hashtag. So the answer is ["awesome","lovingit"].
Example 2
tweet = "#Day1 of #100DaysOfCode: #day1!"return = ["Day1","100DaysOfCode","day1"]Digits are word characters, so Day1 and 100DaysOfCode are whole words. 100DaysOfCode ends at the colon, and day1 ends at the !. Day1 and day1 differ only in case, and both are kept as written. So the answer is ["Day1","100DaysOfCode","day1"].
Example 3
tweet = "C#, F# and ## are not tags #"return = []Each # here is followed by a comma, a space or another #, or is the last character. None of these is a word character, so no # starts a hashtag, and the answer is the empty list [].
Example 4
tweet = "##go#up #go"return = ["go","up","go"]The first # is followed by another #, so it starts nothing. The second starts go, which ends at the next #. That # starts up, which ends at the space. The last # starts go again, and the repeat is kept. So the answer is ["go","up","go"].
Constraints
0 <= tweet.length <= 1000tweetconsists of printable ASCII characters (character codes 32 to 126), including spaces.
More eBay Onsite Interview questions
- Validate Ada Integer LiteralONSITE INTERVIEW · Seen Nov 2026
- Count Distinct Pairs With Sum KONSITE INTERVIEW · Seen Nov 2026
- Diameter of Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Count Staircases with Strictly Increasing StepsONSITE INTERVIEW · Seen Oct 2026
- Hierarchical Permission CheckONSITE INTERVIEW · Seen Oct 2026
- Matrix MultiplicationONSITE INTERVIEW · Seen Oct 2026
- N-Queens Solution CountONSITE INTERVIEW · Seen Oct 2026
- Gas StationONSITE INTERVIEW · Seen Oct 2026
- Onion-Peel Layers of a Binary TreeONSITE INTERVIEW · Seen Oct 2026
- Container With Most WaterONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Non-Repeating CharacterAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Container Ledger with TradesFlexport · ONSITE INTERVIEWFlexport interview questions
- Christmas Tree with N TiersVerily · ONSITE INTERVIEW
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Implement tail -nConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions