Finding the Topic of Each Post - Problem
Finding the Topic of Each Post
Imagine you're building a content management system for a social media platform! ๐ฑ You have a collection of posts and need to automatically categorize them based on their content.
The Challenge:
Given two tables -
Rules for Topic Assignment:
โข If a post contains any keyword from a topic (case-insensitive), that post belongs to that topic
โข If a post has no matching keywords, mark it as
โข If a post matches multiple topics, list all topic IDs sorted in ascending order, separated by commas
โข Remove duplicate topic IDs from the result
Input:
โข
โข
Output:
Return each post_id with its corresponding topic string (either topic IDs or "Ambiguous!").
Imagine you're building a content management system for a social media platform! ๐ฑ You have a collection of posts and need to automatically categorize them based on their content.
The Challenge:
Given two tables -
Keywords (which maps topics to their associated words) and Posts (containing post content), you need to determine which topics each post belongs to.Rules for Topic Assignment:
โข If a post contains any keyword from a topic (case-insensitive), that post belongs to that topic
โข If a post has no matching keywords, mark it as
"Ambiguous!"โข If a post matches multiple topics, list all topic IDs sorted in ascending order, separated by commas
โข Remove duplicate topic IDs from the result
Input:
โข
Keywords table: Maps topic_id to words that represent that topicโข
Posts table: Contains post_id and content (English letters and spaces only)Output:
Return each post_id with its corresponding topic string (either topic IDs or "Ambiguous!").
Input & Output
example_1.sql โ Basic Topic Matching
$
Input:
Keywords table:
+----------+--------+
| topic_id | word |
+----------+--------+
| 1 | golden |
| 1 | yellow |
| 2 | code |
| 2 | java |
+----------+--------+
Posts table:
+---------+-----------------------------------+
| post_id | content |
+---------+-----------------------------------+
| 1 | We have golden opportunity ahead |
| 2 | I love java code very much |
| 3 | blue sky and white clouds |
+---------+-----------------------------------+
โบ
Output:
+---------+------------+
| post_id | topic |
+---------+------------+
| 1 | 1 |
| 2 | 2 |
| 3 | Ambiguous! |
+---------+------------+
๐ก Note:
Post 1 contains 'golden' which belongs to topic 1. Post 2 contains both 'java' and 'code' which both belong to topic 2. Post 3 doesn't contain any keywords, so it's marked as 'Ambiguous!'.
example_2.sql โ Multiple Topic Matching
$
Input:
Keywords table:
+----------+--------+
| topic_id | word |
+----------+--------+
| 1 | golden |
| 2 | code |
| 3 | sql |
+----------+--------+
Posts table:
+---------+---------------------------+
| post_id | content |
+---------+---------------------------+
| 1 | golden code is beautiful |
| 2 | learn sql and code today |
+---------+---------------------------+
โบ
Output:
+---------+-------+
| post_id | topic |
+---------+-------+
| 1 | 1,2 |
| 2 | 2,3 |
+---------+-------+
๐ก Note:
Post 1 contains 'golden' (topic 1) and 'code' (topic 2), so topics are '1,2'. Post 2 contains 'sql' (topic 3) and 'code' (topic 2), resulting in '2,3' (sorted order).
example_3.sql โ Case Insensitive Matching
$
Input:
Keywords table:
+----------+--------+
| topic_id | word |
+----------+--------+
| 1 | Python |
| 2 | JAVA |
+----------+--------+
Posts table:
+---------+------------------------+
| post_id | content |
+---------+------------------------+
| 1 | I love python coding |
| 2 | java is great |
| 3 | Learning c++ today |
+---------+------------------------+
โบ
Output:
+---------+------------+
| post_id | topic |
+---------+------------+
| 1 | 1 |
| 2 | 2 |
| 3 | Ambiguous! |
+---------+------------+
๐ก Note:
Case insensitive matching: 'python' matches 'Python' (topic 1), 'java' matches 'JAVA' (topic 2). Post 3 has no matching keywords.
Visualization
Tap to expand
Understanding the Visualization
1
Keyword Database
System maintains a database of topics and their associated keywords (like 'technology' โ ['code', 'java', 'python'])
2
Content Analysis
For each new post, scan the content for any keywords that appear in our database
3
Topic Matching
If keywords are found, collect all corresponding topic IDs and sort them
4
Result Generation
Output the sorted topic list or 'Ambiguous!' if no keywords match
Key Takeaway
๐ฏ Key Insight: Use SQL's LEFT JOIN with LIKE pattern matching for efficient keyword detection, combined with conditional aggregation to handle both matching and non-matching cases in a single query.
Time & Space Complexity
Time Complexity
O(P ร K ร log K)
P posts ร K keywords with efficient string matching and sorting
โก Linearithmic
Space Complexity
O(P ร T)
P posts ร T average topics per post for result storage
โ Linear Space
Constraints
- 1 โค number of keywords โค 100
- 1 โค number of posts โค 100
- 1 โค topic_id โค 100
- 1 โค word.length โค 30
- 1 โค content.length โค 300
- word and content consist of English letters and spaces only
- All words are case insensitive for matching
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code