Building Leaderboards in C#

Welcome to the next exciting part of our backend system project. In this unit, we will focus on building leaderboard functionality using C# collections. Building a leaderboard is a popular use case for many applications, such as games and competitive platforms. You've got a good handle on managing user data from previous lessons, so let's build on that foundation.

What You'll Build

Let's briefly review what we'll focus on in this unit. Our main tasks will be:

  1. Adding user scores to a leaderboard: We will store user scores using C# collections.
  2. Retrieving the leaderboard: We will fetch and display the top users and their scores.
  3. Getting a user's rank and score: We will retrieve the ranking and score of a specific user.

Below are some key parts of the code you will be working with to perform these tasks using C#.

using System;
using System.Collections.Generic;
using System.Linq;

class Leaderboard
{
    private SortedDictionary<int, List<string>> scores = new SortedDictionary<int, List<string>>(Comparer<int>.Create((x, y) => y.CompareTo(x)));

    public void AddScore(string user, int score)
    {
        if (!scores.ContainsKey(score))
        {
            scores[score] = new List<string>();
        }
        scores[score].Add(user);
    }
    
    public List<(string, int)> GetLeaderboard(int topN)
    {
        var leaderboard = new List<(string, int)>();
        foreach (var score in scores)
        {
            foreach (var user in score.Value)
            {
                if (leaderboard.Count < topN)
                {
                    leaderboard.Add((user, score.Key));
                }
                else
                {
                    return leaderboard;
                }
            }
        }
        return leaderboard;
    }

    public (int rank, int score) GetUserRankAndScore(string user)
    {
        int rank = 0;
        foreach (var entry in scores)
        {
            rank += entry.Value.Count;
            if (entry.Value.Contains(user))
            {
                return (rank - entry.Value.Count + entry.Value.IndexOf(user) + 1, entry.Key);
            }
        }
        return (-1, -1); // User not found
    }
}

class Program
{
    static void Main()
    {
        var leaderboard = new Leaderboard();
        leaderboard.AddScore("user1", 100);
        leaderboard.AddScore("user2", 200);
        leaderboard.AddScore("user3", 150);

        var topUsers = leaderboard.GetLeaderboard(3);
        Console.WriteLine("Top Users:");
        foreach (var user in topUsers)
        {
            Console.WriteLine($"User: {user.Item1}, Score: {user.Item2}");
        }

        var userRankScore = leaderboard.GetUserRankAndScore("user3");
        Console.WriteLine($"User3's Rank: {userRankScore.rank}, Score: {userRankScore.score}");
    }
}
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal