As part of a test I was asked to develop an application to manage high-score data in highly competitive environments. This application should not persist on disk or use libraries and external frameworks . Basically the requirements say:
- Optimizing memory and CPU usage is very important
- the service will receive a stellar number of simultaneous requests
- For the http part we should use the HttpServer .
The application is basically a set of services rest for the user:
- logar (should return a token and keep the user authenticated for 10 minutes)
- post a high score for a certain level
- Get a list of high scores for a certain level (in order)
The system should only return 15 high scores for each level, high scores are unique for each User / Level pair (ie for xo user phase y you should only have a high score ).
Given the functional and non-functional requirements, I thought of the following data structure to store high scores (with minimum retention):
// Mapa que liga um nivel a uma lista ordenada de High Scores
private static final Map<Integer, ConcurrentSkipListSet<HighScore>> scores =
new ConcurrentHashMap<>();
// Conjunto de usuários autenticados
private static final Set<AuthToken> authorized =
Collections.newSetFromMap(new ConcurrentHashMap<AuthToken, Boolean>());
While this data structure seems to me efficient in the sense of reducing retention, I do not know how much it ends up wasting memory internally.
In the same way, ConcurrentSkipListSet
allows you to insert in log(n)
and make the implementation of the method that returns trivial scores .
On the other hand, to limit Set
to top 15 scores by level (to avoid wasting memory and crashes ), I did some real gymnastics including using < AtomicInteger to count the amount of high scores high scores by level (the size
method of ConcurrentSkipListSet
is costly) and the need to synchronize multiple points with locks .
In short, I'm not convinced that I'm using the most efficient strategy to solve the problem, and I'm not used to thinking of such a low level ...
In the "real" world, you would solve this problem trivially with the use of a container Jetty >, a Servlet for the login by storing the authentication in the session and probably an embedded database as the H2 . Unfortunately I am not allowed to use any of this.
In this way, how would you approach such a problem given the constraints technology devices? Would they use what data structures (or would they develop their own?)? Is my strategy "good enough" for the problem or did I travel the solution?
PS This problem submitted to Code Review looks a lot like what I'm solving (and the solution was similar to mine too).