Understand what the interviewer is asking for by using test cases and questions about the problem.
HAPPY CASE
Input: ["Dogecoin to the moon!", "One does not simply walk into Mordor", "Dogecoin to the moon!", "Distracted boyfriend", "One does not simply walk into Mordor"]
Output: ['Dogecoin to the moon!', 'One does not simply walk into Mordor']
Explanation: Both "Dogecoin to the moon!" and "One does not simply walk into Mordor" appear more than once.
EDGE CASE
Input: ["Y U No?", "First world problems", "Philosoraptor", "Bad Luck Brian"]
Output: []
Explanation: None of the memes appear more than once.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
This problem can be categorized under:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
We need to count how many times each meme appears in the input list. If a meme appears more than once, we consider it a "trending" meme. We can store the counts in a dictionary and then iterate through the dictionary to find memes that have a count greater than 1.
1) Initialize an empty dictionary `meme_count` to store the counts of each meme.
2) Loop through the input list of memes:
a) If the meme is already in the dictionary, increment its count.
b) If the meme is not in the dictionary, add it with a count of 1.
3) Initialize an empty list `trending_memes`.
4) Loop through the dictionary:
a) If the count of a meme is greater than 1, add it to the `trending_memes` list.
5) Return the `trending_memes` list.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def find_trending_memes(memes):
meme_count = {}
trending_memes = []
for meme in memes:
if meme in meme_count:
meme_count[meme] += 1
else:
meme_count[meme] = 1
for meme, count in meme_count.items():
if count > 1:
trending_memes.append(meme)
return trending_memes
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
Input: ["Dogecoin to the moon!", "One does not simply walk into Mordor", "Dogecoin to the moon!", "Distracted boyfriend", "One does not simply walk into Mordor"]
meme_count = {"Dogecoin to the moon!": 2, "One does not simply walk into Mordor": 2, "Distracted boyfriend": 1}
trending_memes = ['Dogecoin to the moon!', 'One does not simply walk into Mordor']
Output: ['Dogecoin to the moon!', 'One does not simply walk into Mordor']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Let N represent the number of memes in the input list.