Skip to content

[BUG] Daily/weekly/monthly leaderboards can show negative solved counts and negative scores #238

Description

@Tamcodes4

Description

processTimeframe() in scripts/sync-leaderboard.js computes each user's period delta via plain subtraction with no floor at zero. If a user's cumulative LeetCode totals ever read lower than the previous snapshot, the leaderboard displays a negative count and negative score for that period.

Steps to Reproduce

  1. Run sync-leaderboard.js to produce a baseline daily.json snapshot for a user.
  2. Simulate a lower easySolved/mediumSolved/hardSolved value on the next sync for that same user (e.g. via a temporary API glitch, or by manually editing the previous snapshot's data to be smaller before the next sync run).
  3. Run the sync again and inspect the resulting daily.json/weekly.json/monthly.json.

Expected Behavior

Period deltas should never go below zero, a user can't "unsolve" problems, so the displayed progress for that period should floor at 0.

Actual Behavior

The score and per-difficulty counts go negative and are rendered as-is (e.g. -3) on the leaderboard.

Root Cause

data[i].data.easySolved -= previousData[previousIndex].data.easySolved;
data[i].data.mediumSolved -= previousData[previousIndex].data.mediumSolved;
data[i].data.hardSolved -= previousData[previousIndex].data.hardSolved;
data[i].score =
  data[i].data.easySolved +
  data[i].data.mediumSolved * 3 +
  data[i].data.hardSolved * 5;

No Math.max(0, ...) guard, unlike equivalent delta logic elsewhere in the codebase (historical-graphs.js, compare.js) which already clamps correctly.

Suggested Fix

data[i].data.easySolved = Math.max(0, data[i].data.easySolved - previousData[previousIndex].data.easySolved);
data[i].data.mediumSolved = Math.max(0, data[i].data.mediumSolved - previousData[previousIndex].data.mediumSolved);
data[i].data.hardSolved = Math.max(0, data[i].data.hardSolved - previousData[previousIndex].data.hardSolved);

Affected Files

  • scripts/sync-leaderboard.js

Metadata

Metadata

Assignees

Labels

BackendTask mainly involving backendlevel:beginnerIndicates the difficultytype:bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions