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 - 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
๐Ÿ“ฑ Posts"I love java code""golden opportunity""blue sky clouds"๐Ÿ”‘ KeywordsTopic 1: golden, yellowTopic 2: code, javaTopic 3: sql, databaseMATCHKEYWORDS๐Ÿ“Š ResultsPost 1 โ†’ "2"Post 2 โ†’ "1"Post 3 โ†’ "Ambiguous!"๐Ÿ” Matching Process1. Case-insensitive search: "JAVA" matches "java"2. Collect all matching topic IDs3. Sort and join with commas: "1,2,3"โš ๏ธNo keywords found = "Ambiguous!"โœ…Keywords found = Topic IDs๐ŸŽฏMultiple topics = "1,2,3"
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

n
2n
โšก Linearithmic
Space Complexity
O(P ร— T)

P posts ร— T average topics per post for result storage

n
2n
โœ“ 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
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
28.8K Views
Medium-High Frequency
~25 min Avg. Time
1.2K 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