Finding the Topic of Each Post - Problem

You are given two tables: Keywords and Posts.

The Keywords table contains topic IDs and words that express those topics. Multiple words can express the same topic, and one word may be used for multiple topics.

The Posts table contains post IDs and their content (English letters and spaces only).

Task: Find the topic of each post based on these rules:

  • If a post contains no keywords from any topic → topic is "Ambiguous!"
  • If a post contains keywords from one or more topics → topic is a comma-separated string of topic IDs in ascending order (no duplicates)
  • Keyword matching is case insensitive

Table Schema

Keywords
Column Name Type Description
topic_id PK int ID of the topic
word PK varchar Keyword that expresses this topic
Primary Key: (topic_id, word)
Posts
Column Name Type Description
post_id PK int Unique identifier for the post
content varchar Post content (English letters and spaces only)
Primary Key: post_id

Input & Output

Example 1 — Posts with Multiple Topic Matches
Input Tables:
Keywords
topic_id word
1 golden
1 retriever
2 Puppy
2 dog
Posts
post_id content
1 My DOG is a Golden Retriever
2 A good boi doggy
3 train transport
Output:
post_id topic
1 1,2
2 2
3 Ambiguous!
💡 Note:

Post 1 contains 'DOG' (matches topic 2), 'Golden' and 'Retriever' (both match topic 1) → topics '1,2'. Post 2 contains 'doggy' which doesn't exactly match 'dog' → 'Ambiguous!'. Post 3 has no keyword matches → 'Ambiguous!'.

Example 2 — Case Insensitive Matching
Input Tables:
Keywords
topic_id word
1 code
1 programming
2 SQL
Posts
post_id content
1 Learning SQL and Programming
2 Writing CODE is fun
3 Hello world
Output:
post_id topic
1 1,2
2 1
3 Ambiguous!
💡 Note:

Case insensitive matching: Post 1 matches 'SQL' (topic 2) and 'Programming' (topic 1). Post 2 matches 'CODE' which matches keyword 'code' (topic 1). Post 3 has no matches.

Constraints

  • 1 ≤ topic_id ≤ 1000
  • 1 ≤ post_id ≤ 1000
  • word and content consist of English letters and spaces only
  • All words in Keywords table are unique per topic

Visualization

Tap to expand
Finding Topic of Each Post INPUT Keywords Table: topic_id word 1 python 1 programming 2 database 2 sql Posts Table: id content 1 Learn Python prog... 2 SQL database tips 3 Random thoughts 4 Python and SQL... ALGORITHM STEPS 1 Build Keyword Map Create word --> topic_ids mapping (lowercase) 2 Process Each Post Split content into words Convert to lowercase 3 Match Keywords For each word, check if exists in keyword map 4 Collect Topic IDs Use SET to avoid dups Sort ascending order Example Match: "python" --> {1} "database" --> {2} "random" --> not found FINAL RESULT Output: post_id topic 1 1 2 2 3 Ambiguous! 4 1,2 Notes: Post 1: matched "python" --> topic 1 Post 2: matched "sql","database" --> 2 Post 3: no match --> Ambiguous! Post 4: both topics Key Insight: Use a HashMap to store keyword --> topic_id mappings for O(1) lookup. Convert all text to lowercase for case-insensitive matching. Use a SET to collect unique topic IDs, then sort and join with commas. Return "Ambiguous!" when the SET is empty (no keywords matched). TutorialsPoint - Finding the Topic of Each Post | Optimal Solution
Asked in
Meta 28 Amazon 22 Microsoft 18
23.4K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen