Top Co-Occurring Hashtag Pairs by Distinct Instagram Posters

Last updated: November 29, 2026

Quick Overview

Instagram posts can carry many hashtags, and the same tag can appear twice on a post. Find the 20 hashtag pairs used together by the most distinct users in the last 30 days, counting each user once per pair.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Instagram posts can carry many hashtags, and the same tag can appear twice on a post. Find the 20 hashtag pairs used together by the most distinct users in the last 30 days, counting each user once per pair.

Topic discovery on Instagram uses hashtag co-occurrence, and Meta onsite SQL rounds use it to test many-to-many self joins. The interviewer checks dedupe of tag rows, unordered pairs, a distinct-user metric instead of raw post counts, and controlling the join explosion.

What the Interviewer Expects
  • Normalizes and dedupes (post, tag) rows first
  • Self joins on post_id with tag_a < tag_b
  • Counts distinct users, not posts, so spammy accounts do not dominate
  • Caps tags per post to bound pairs per post
  • Breaks ties deterministically in the top 20
Key Topics to Cover
many-to-many self join
unordered pairs
COUNT DISTINCT
join explosion
co-occurrence
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you compute lift or PMI instead of raw counts?
  • How do you stop a single bot network from dominating?
  • How would you compute this for every week incrementally?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

posts(post_id, user_id, created_at) and post_hashtags(post_id, tag). Tags can repeat on a post and differ only by case (#Travel vs #travel). Some posts carry 30 tags. Metric: number of distinct us...

Approach
  1. Normalize tags with LOWER and dedupe to distinct (post, tag).
  2. Keep posts in window, attach user_id, and drop posts with more than 30 tags (30 tags make 435 pairs, a spam signal anyway).
  3. Sel...

Submit Your Answer
Markdown supported