Meta-style Company ChallengeHardVerified answerSQLite live

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_idINTEGER
  • user_idINTEGER
  • contentTEXT
  • likes_countINTEGER
  • comments_countINTEGER
  • shares_countINTEGER

users

  • user_idINTEGER
  • usernameVARCHAR(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_idusernamecontent_previewtotal_engagementvirality_scorevirality_rank
106jane_smithThoughts on remote work2831001
105sarah_jonesCheck out my new design20671.072
102jane_smithBeautiful sunset today14547.723
103mike_wilsonNew blog post on AI12946.44
104john_doeWeekend vibes8427.95
101john_doeJust finished a great project!5618.56

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Interview Practice

Return to the complete interview preparation experience.