Content Virality Score
Calculate a virality score for each post — combining likes, comments, and shares with different weights — and rank posts by virality.
- CTEs
- Window functions
- Joins
- Subqueries
- String functions
Challenge brief
Understand the request
Feed Algorithm Team is building a virality detection model and needs a normalised virality score for every post to feed the ranking engine.
Compute weighted engagement and a 0-100 normalised virality score per post using two CTEs and RANK().
Return
- post_id
- username
- content_preview
- total_engagement
- virality_score
- virality_rank
Constraints
- Weighted engagement = likes + comments*1.5 + shares*2
- Normalize each score against the highest weighted engagement in the result
- Exclude posts with no engagement and show the highest virality first
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
posts
post_idINTEGERuser_idINTEGERcontentTEXTlikes_countINTEGERcomments_countINTEGERshares_countINTEGER
users
user_idINTEGERusernameVARCHAR(50)
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Two CTEs: CTE 1 computes weighted_engagement (shares*2 + comments*1.5 + likes*1) and total_engagement per post. CTE 2 normalises by dividing each post's weighted score by the dataset maximum, then multiplies by 100.
Hint 2
CTE post_engagement: JOIN posts to users, compute total_engagement and weighted_engagement. WHERE total > 0. CTE virality_calc: ROUND(weighted / (SELECT MAX(weighted) FROM post_engagement) * 100, 2). Main: RANK() OVER (ORDER BY virality_score DESC).
Hint 3
Start with the tables that establish the result grain for question 13, select the required output aliases, and add the remaining joins, filters, aggregation, and ordering one clause at a time.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
WITH post_engagement AS (SELECT p.post_id, u.username, SUBSTR(p.content, 1, 40) AS content_preview, (p.likes_count + p.comments_count + p.shares_count) AS total_engagement, p.shares_count * 2.0 + p.comments_count * 1.5 + p.likes_count AS weighted_engagement FROM posts p INNER JOIN users u ON p.user_id = u.user_id WHERE (p.likes_count + p.comments_count + p.shares_count) > 0), virality_calc AS (SELECT post_id, username, content_preview, total_engagement, ROUND(weighted_engagement * 100.0 / NULLIF((SELECT MAX(weighted_engagement) FROM post_engagement), 0), 2) AS virality_score FROM post_engagement) SELECT post_id, username, content_preview, total_engagement, virality_score, RANK() OVER (ORDER BY virality_score DESC) AS virality_rank FROM virality_calc ORDER BY virality_rank, post_id;Why this works
Compute one weighted score per post, normalize it against the maximum with a protected denominator, and rank the resulting scores. The measure now matches the stated virality definition without an unrelated likes-based ratio.
Success check
6 posts — jane_smith's 'Thoughts on remote work' scores 100 (top), john_doe's project post scores 18.5 (bottom)
Expected result
Use this output to verify values, aliases, ordering, and row count.
| post_id | username | content_preview | total_engagement | virality_score | virality_rank |
|---|---|---|---|---|---|
| 106 | jane_smith | Thoughts on remote work | 283 | 100 | 1 |
| 105 | sarah_jones | Check out my new design | 206 | 71.07 | 2 |
| 102 | jane_smith | Beautiful sunset today | 145 | 47.72 | 3 |
| 103 | mike_wilson | New blog post on AI | 129 | 46.4 | 4 |
| 104 | john_doe | Weekend vibes | 84 | 27.9 | 5 |
| 101 | john_doe | Just finished a great project! | 56 | 18.5 | 6 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Google
Independent Google-style search, advertising, user-engagement, and video-product SQL practice.
Netflix
Independent Netflix-style streaming, subscription, catalog, ratings, and engagement SQL practice.
Airbnb
Independent Airbnb-style marketplace, booking, listing, payment, review, and guest analytics SQL practice.
Return to the complete interview preparation experience.