machine coding

Live Leaderboard

A fantasy-cricket contest table: add points, show the top k, answer “what is my rank?”.

Easy90 minutesJava · class Leaderboard

the brief

What you are building

During an IPL match, a fantasy-cricket app updates every player's points after each ball.

Lakhs of users keep refreshing two screens: the top 10 table and my rank.

Your job: the in-memory leaderboard behind those two screens. Points come in, the table must always be right.

must have

Requirements

  1. addScore(player, points) adds points to the player's total. A new player starts at 0.
  2. The table order: a higher total first. Equal totals go in alphabetical order of the name (String.compareTo).
  3. top(k) returns the best k players in that order. If there are fewer than k players, return all of them.
  4. rank(player) returns the player's 1-based position in that order, or -1 if they are not on the board.
  5. reset(player) takes the player off the board. Resetting an unknown player does nothing. If they score again, they start from 0.

the contract

The Java API: class Leaderboard

public Leaderboard()
An empty board.
public void addScore(String player, int points)
Adds points (≥ 1) to the player's total.
public List<String> top(int k)
The best k names, best first.
public int rank(String player)
1-based position, or -1.
public void reset(String player)
Removes the player from the board.
  • 1 ≤ points per call, and a player's total stays ≤ 10⁹ (so it fits in an int)
  • 1 ≤ k ≤ 100
  • up to 10,000 calls and 1,000 players
  • names are non-empty and case-sensitive (Ravi and ravi are two players)

example session

Calls in, answers out

Three players, a tie, a comeback and a reset.

callreturnswhy
new Leaderboard()—
addScore("rahul", 50)—
addScore("priya", 70)—
addScore("amit", 50)—
top(2)["priya", "amit"]amit and rahul tie at 50; amit comes first alphabetically
rank("rahul")3
addScore("rahul", 30)—rahul now has 80
top(3)["rahul", "priya", "amit"]
rank("neha")-1neha is not on the board
reset("priya")—
top(5)["rahul", "amit"]k is bigger than the board
rank("amit")2

time plan

Your 90 minutes

  1. 10 minclarify — How are ties broken? Can points be negative? What does top(k) do when k is bigger than the board?
  2. 10 mindesign — Two questions: what is this player's total? and who is in order? Pick one structure for each, and decide how they stay in sync.
  3. 40 mincore code — addScore and top first, run the example. Then rank and reset.
  4. 20 minedge cases — Empty board, one player, all tied, reset then score again, very large totals.
  5. 10 minpolish — Clear names, one comparator in one place. Be ready to discuss making rank faster.

Practise under real pressure: a 90-minute clock with the phases above. It keeps running if you reload the page.

your turn

Build it here

Write the whole class in the editor. Run replays the example session; Submit adds the hidden tests (edge cases and large random sessions). Stuck for 20 minutes? Open one hint at a time.

Build Leaderboard in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    The reference, the data-structure reasoning and the rubric are hidden so you can build it first. Even a half-working attempt teaches you more than reading a perfect answer.

    Opening all three hints also shows it.

    revise

    Chapters that teach these structures