Problem · Sorting

Social Media Feed

Learn this problem
EasyTiktokOA
See Tiktok hiring insights

Problem statement

Sorting social media feeds by user and timestamp enhances user experience by ensuring posts are organized in a meaningful way. You are given a social media feed with entries containing timeStamp, userId, and post, and your task is to sort these entries first based on user and then chronologically.

Write a function sortSocialMediaFeed that takes a list of strings. Each string is a comma-separated value representing a post with timeStamp (a string representing Unix time), userId, and post. Return the feed sorted first by userId and then by timeStamp for each user.

Function

sortSocialMediaFeed(feed: String[]) → String[]

Examples

Example 1

feed = ["1654162021,user2,Post A", "1654163021,user1,Post B", "1654164021,user1,Post C", "1654165021,user2,Post D"]return = ["1654163021,user1,Post B", "1654164021,user1,Post C", "1654162021,user2,Post A", "1654165021,user2,Post D"]

First, the feed is sorted by userId, resulting in user1, user1, user2, user2. Within each user, the posts are then sorted by timeStamp. For user1, “Post B” comes before “Post C.” For user2, “Post A” comes before “Post D.”

Constraints

🍅🍅

More Tiktok problems

drafts saved locally
public String[] sortSocialMediaFeed(String[] feed) {
  // write your code here
}
feed["1654162021,user2,Post A", "1654163021,user1,Post B", "1654164021,user1,Post C", "1654165021,user2,Post D"]
expected["1654163021", "user1", "Post B", "1654164021", "user1", "Post C", "1654162021", "user2", "Post A", "1654165021", "user2", "Post D"]
checking account