383. Most Frequent Tags Across LinkedIn Connections
Most Frequent Tags Across LinkedIn Connections
A LinkedIn-style social network contains members, connections, and posts with tags. Starting from a given member, find the most frequently used tags among that member's direct connections and their connections.
Implement the following method:
List<String> findTopFrequentTags( String member, List<String> connections, List<String> postTags, int topCount )
  • member is the member from whom the search begins.
  • Each value in connections has the format "member1,member2" and represents a mutual connection.
  • Each value in postTags has the format "author,tag1,tag2,..." and represents one post.
  • topCount is the maximum number of tags to return.
  • The method returns the most frequently used qualifying tags.

Connection Range

  • A direct connection of member is at distance 1.
  • A connection of a direct connection is at distance 2.
  • Only members at distance 1 or 2 are included.
  • The starting member is not included, even if a connection path leads back to that member.
  • Each qualifying member and each of their posts is processed only once, even if multiple connection paths reach that member.

Tag Ranking

  • Every occurrence of a tag in a qualifying member's post increases its frequency by 1.
  • Tags with higher frequencies appear first.
  • Tags with the same frequency are ordered lexicographically.
  • If fewer than topCount distinct tags are found, return all available tags.
  • If no qualifying tags exist, return an empty list.

Constraints

  • 1 ≤ member.length() ≤ 50
  • 0 ≤ connections.size() ≤ 100,000
  • 0 ≤ postTags.size() ≤ 100,000
  • 1 ≤ topCount ≤ 10,000
  • Every connection contains exactly two distinct member names separated by one comma.
  • Every post contains an author followed by at least one tag.
  • A tag appears at most once within the same post.
  • Member names and tags contain lowercase English letters, digits, or underscores only.
  • Duplicate connection entries are not present, including reversed duplicates.
  • Neither connections, postTags, nor any value inside them will be null.

Examples

Example 1

findTopFrequentTags( member = "maya", connections = List.of( "maya,noah", "maya,olivia", "noah,liam", "olivia,emma", "liam,sophia" ), postTags = List.of( "maya,security", "noah,java,cloud", "noah,java", "olivia,design,cloud", "liam,java,backend", "emma,cloud,design", "sophia,java" ), topCount = 3 )
Output: List.of("cloud", "java", "design")
Noah and Olivia are direct connections, while Liam and Emma are at distance 2. Among their posts, "cloud" and "java" each occur three times, so lexicographical order places "cloud" first. "design" occurs twice. Maya and Sophia are excluded.

Example 2

findTopFrequentTags( member = "aarav", connections = List.of( "aarav,diya", "diya,kabir", "kabir,meera", "aarav,rohan" ), postTags = List.of( "aarav,management", "diya,analytics,leadership", "kabir,analytics", "rohan,leadership", "meera,analytics,networking" ), topCount = 5 )
Output: List.of("analytics", "leadership")
Diya, Kabir, and Rohan are within two connections of Aarav. "analytics" and "leadership" each occur twice, so they are returned in lexicographical order. Meera is three connections away and is excluded.

Example 3

findTopFrequentTags( member = "neha", connections = List.of(), postTags = List.of( "neha,java,cloud" ), topCount = 2 )
Output: List.of()
Neha has no direct or second-degree connections, and the starting member's own tags are not counted.


Please use Laptop/Desktop or any other large screen to add/edit code.