In my current company, we do something like... users play a quiz, top users win something. To show live leaderboard, we tell user to wait 1 minute, and each minute we run a crob to calculate the leaderboard by sorting the data, calculate rank of each user and save it somewhere, and everyone will see live leaderboard from that data, since sorting and calculating rank for each request will be too heavy.
Now I'm wondering, if I had to made that feature, I don't want to tell user to wait 1 minute to see his/her rank, but I'm unable to think an efficient approach. Then I remembered codeforces do it very well.
If I have to do it in C++, I will simply use ordered_set. But how will I do it if my data is stored in a database like MongoDB?
To be specific, I have only three requirements: 1) Each user can see their live rank 2) Rank of users on some page. Let's assume that page size is 20. 3) Insert in database