trending
algorithm
technology
data analysis
machine learning

Trending algorithm

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A trending algorithm decides what is rising in attention right now, not just what has accumulated the most total activity over all time. That distinction matters because a good trending score rewards recent velocity, filters spam, and normalizes for the fact that large accounts or already-popular topics naturally generate more raw volume than smaller ones.

Most trending systems are trying to capture some blend of:

  • recent activity volume
  • growth rate compared with a baseline
  • number of unique users participating
  • freshness or recency
  • quality or trust signals

The exact formula depends on the product. A social feed, a news site, and an internal analytics dashboard may all define "trending" differently.

The key idea is that a trending item is not simply the most clicked item. It is the item whose current activity is unusually strong relative to time and context.

A Simple Recency-Weighted Score

One common pattern is to decay older interactions so that new activity matters more than old activity.

python
1from math import exp
2from datetime import datetime, timezone
3
4def trend_score(events, now=None, half_life_minutes=60):
5    now = now or datetime.now(timezone.utc)
6    decay_constant = 0.693 / half_life_minutes
7
8    score = 0.0
9    for event_time, weight in events:
10        age_minutes = (now - event_time).total_seconds() / 60.0
11        score += weight * exp(-decay_constant * age_minutes)
12
13    return score

In this model, recent likes, posts, or mentions contribute strongly, but their influence fades over time. That makes a burst of current activity outrank stale popularity.

Normalize Against Expected Volume

Raw counts are often misleading. A topic that usually gets 10 mentions per hour and suddenly gets 200 may be far more interesting than a celebrity topic that always gets 10,000 mentions every hour.

That is why many systems compare current activity to a historical baseline.

python
1def relative_lift(current_count: float, baseline_count: float) -> float:
2    baseline = max(baseline_count, 1.0)
3    return current_count / baseline
4
5
6print(relative_lift(current_count=200, baseline_count=10))
7print(relative_lift(current_count=10000, baseline_count=9000))

The first item has far stronger lift even though the second has higher absolute volume.

Use More Than One Signal

A production trending algorithm rarely uses one number alone. For example:

  • views measure exposure
  • shares measure active propagation
  • comments measure engagement depth
  • unique users reduce the impact of one person repeating the same action

You can combine them into one score:

python
1def combined_score(views, shares, comments, unique_users):
2    return (
3        0.1 * views +
4        2.0 * shares +
5        1.5 * comments +
6        3.0 * unique_users
7    )

The weights are product decisions, not universal truths. The important design step is choosing signals that reflect the behavior you actually want to promote.

Defend Against Manipulation

Any trending system becomes a target for gaming once it affects visibility. That means the algorithm should discount suspicious activity such as:

  • repeated actions from the same account
  • bot-like bursts from new accounts
  • engagement farms or coordinated spam

Simple safeguards can include per-user caps, trust-weighted events, rate limits, and anomaly detection. Without these, a naive trending score can be dominated by manipulation rather than real interest.

The most important lesson is that trending is not a single canonical algorithm. The right score depends on what the platform values:

  • immediacy
  • quality
  • fairness to smaller creators
  • resistance to abuse
  • geographic or community-specific relevance

A platform that wants local trends may compute scores per city. A developer tool may show trends by workspace. A news product may put strong editorial or trust constraints around what can trend at all.

Common Pitfalls

The most common mistake is ranking by total count and calling that "trending." That produces popularity charts, not genuine trend detection.

Another is ignoring baseline normalization. Large established topics will dominate forever unless the score cares about unusual acceleration, not just volume.

Developers also sometimes forget abuse resistance. A trending algorithm without guardrails can quickly become an incentive system for spam.

Finally, avoid pretending one formula is permanently correct. Trending logic should be measured against actual product outcomes, then adjusted as user behavior changes.

Summary

  • Trending algorithms measure current acceleration and recency, not just total popularity.
  • Recency decay is a common way to keep old activity from dominating the ranking.
  • Baseline normalization helps smaller but fast-rising topics surface.
  • Good systems combine several signals and include anti-abuse defenses.
  • The best trending algorithm depends on the platform's goals, not on one universal formula.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.