← All exercises

The leaderboard where 9 beats 10

A leaderboard in a fitness app · APIs and contracts · difficulty 1/3 · ~5 min

A teammate asked the AI assistant: "Write the leaderboard helpers for the weekly challenge: topScores(scores, n) for the top-N list, personalBest(scores) for the profile card, and rankOf(scores, score) for the 'you are #3 this week' badge. Scores are numbers of points; they can be fractional. Don't mutate the input."

The code came back short, readable, with a shared helper and passing tests. Contract from the docstrings:

The tests are green. Check whether the helpers are right for the scores a real user would have.

Repair the code until the hidden tests pass.

code.ts

Visible tests (they pass on the code as given)
import { describe, it, expect } from "@debunk/harness";
import { topScores, personalBest, rankOf } from "./code";

describe("leaderboard", () => {
  it("topScores returns the highest scores first", () => {
    expect(topScores([3, 1, 2, 5, 4], 3)).toEqual([5, 4, 3]);
  });

  it("personalBest returns the highest score", () => {
    expect(personalBest([2, 9, 4])).toBe(9);
  });

  it("rankOf gives rank 1 to the best score", () => {
    expect(rankOf([7, 3, 8], 8)).toBe(1);
    expect(rankOf([7, 3, 8], 3)).toBe(3);
  });
});