HackerRank Fun
I am posting about my self-indulgent time spent on Hackerrank exercises. So once you figure out how to use JS Bench and realise that these exercises are not necessarily about doing something helpful/useful, they can be quite fun. I did this exercise, plus another one, but I don’t think I’ll magically find the time to explain both of them. TLDR, it gives you a list of scores by different players that can be used to form a leaderboard. They are ranked as equal if they have the same score, so 2 people with a score of 20 would both be at first place if that were the highest score (they don’t give you their ranking). Then you have a list of scores that you have to try incorporating into this list of scores/leaderboard, one at a time, and return back their ranking.
-leads is the list of scores by different players that currently forms the leaderboard. All of its items are now original, it does not contain any duplicates. We don’t need to know that two players in the original leaderboard had the same result. This is in numerical order. -answer will be the final result. -alreadyDone will be an object that can be accessed via a key of a score. The value of that key will be its ranking. We don’t want to be figuring out a ranking if we’ve already done it. -isRanked keeps track of whether or not we’ve ranked something yet, as if we can’t find any score in the leaderboard equivalent to/lower than our score, we have to put it at the end of the list. -originals is the list of scores to incorporate one at a time. We have removed any scores that are in that list more than once. We will use it to fill alreadyDone. Some rankings of alreadyDone will probably need to be read more than once, but they certainly won’t need to be written more than once.
We loop through the leads array, which is a list of all the scores from the original leaderboard, without duplicates. We use this to get the ranking of the item in originals (we want to compare the items from leads and see if the items in originals are smaller than them). Items in leads are already in the order they should be in, indicating their ranking, so we just compare the items from originals to them and use their order to get the ranking of the items in originals. We ask if we are still at least 1000 items away from the answer (i.e. the number after which we will rank, because we are less than the number). If that’s true, we add 1000 to the index j so that the loop doesn’t unnecessarily run 1000 times. (Remember, the list is in numerical order). We ask if we should have the same ranking as the current item. If that’s the case, we create a new property with a key of our score and a value of the ranking. We set isRanked to true and we finish the inner loop as we’ve already found our answer.
After finishing the inner loop, we ask if the current item in our 2nd list is ranked. If it isn’t, we know that there are no items in the original leaderboard as low as this score, and we put it at the end of the ranking list by giving it the index of the last item. So if our list were 10 items long, the first item would have index 0, the second 1, etc. So having the actual length as the index we write to makes sense, as we will be giving it an index that is not in the list yet. If it is, we changed isRanked back to false so that we know that the next item in our list of scores to be ranked has not been ranked yet. After the outer loop finishes up, we loop through the list of scores that we would like to rank based on the original leaderboard. We already have them in such a way that we can reference them with their score, so we take each score and reference it via its score (the key) in alreadyDone. The value is pushed to answer.












