Lets say you're running a movie database website like IMDb/Netflix and users rate each movie from 1-10 star. When a user rate movie, I get id (long) and rating from 1-10 in the request. The Movie class looks like this.
class Movie
{
long id;
String name;
double avgRating; //Avg Rating of this movie
long numberOfRatings; //how many times this movie was rated.
}
public void updateRating(long movieId, int rating)
{
//code to update movie rating and update top 10 movie to show on page.
}
My question is what data structures I can choose to keep huge movies data in memory so that on each updateRating call, i update movie rating as well as update Top 10 movie and reflect on the webpage and users will always see the latest top 10 movies. I have a lot of space on web server and i can keep all the movies objects in memory. The challenges here are
1) Look up a movie by id.
2) update movie rating.
3) choose new location of this movie in the sorted collection of movies (sorted by ratings)
and if its new position is in first top 10, show it on web page.
All these operations should be done in best optimal time.
this is not a homework but a general programming and data structure question.