DEV Community

xdmarttt
xdmarttt

Posted on

1 of 365 Coding challenge

Hey! So, here's my first article, diving into this whole 365-day coding challenge thing. I'm just getting started, so go easy on me. I'm not a pro at English, so thanks for putting up with that too.

I started early in the morning, all gung-ho, but I kinda dove into the coding without really getting what the problem was asking. Ended up with a wonky solution and took me a good two hours to realize where I messed up. Not the greatest start, but I learned something.

Now, my first challenge was 'Climbing the Leaderboard,' and you can find it here: https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem.

So, let's delve into my solution:

The first part involves obtaining the unique ranks:

const uniqueRanked = [...new Set(ranked)];
Enter fullscreen mode Exit fullscreen mode

Additionally, I created another array that serves as a placeholder for the indices:

const rankedSet = Array.from({ length: uniqueRanked.length });
Enter fullscreen mode Exit fullscreen mode

Moving on, I iterated through the player scores:

player.forEach(score => {
    if (score < uniqueRanked[uniqueRanked.length - 1]) {
        finalRank.push(rankedSet[rankedSet.length - 1] + 1);
    } else if (score > uniqueRanked[0]) {
        finalRank.push(1);
    } else {
        const midPoint = Math.floor((rankedSet.length + 1) / 2) - 1;
        const start = score <= uniqueRanked[midPoint] ? midPoint : 0;

        for (let i = start; i <= rankedSet[rankedSet.length - 1]; i++) {
            if (score >= uniqueRanked[i]) {
                finalRank.push(rankedSet[i]);
                break;
            }
        }
    }
});
Enter fullscreen mode Exit fullscreen mode

As indicated in the code above, I excluded the first and last indices to streamline the iteration process. The nested iteration involves selecting a middle point that divides the array into two. Although not strictly necessary, I implemented this approach to optimize performance in specific cases on Hackerrank where speed is crucial. The goal is to swiftly determine the correct placement of the rank.

I know this may not be the best solution, but I just want to share my journey here :) . I'm open to any new suggestions and constructive criticism to improve my solution. Hope you have a good day! Thanks, everyone!

Top comments (4)

Collapse
 
fpaghar profile image
Fatemeh Paghar

πŸš€ Hey there! Thanks for sharing your coding journey and your solution to the 'Climbing the Leaderboard' challenge! Starting with a coding challenge can be a bit tricky, but kudos to you for diving in and learning along the way. πŸ‘

πŸ€” In the part where you iterate through player scores, the logic seems well-structured. I like how you handle cases where the player's score is less than the lowest or greater than the highest rank. The middle-point strategy for optimizing the search process is a nice touch, especially for performance considerations.

Collapse
 
ra_jeeves profile image
Rajeev R. Sharma

Congratulations on your first post. πŸ™Œ

I see that you've been a member for quite some time, wondering what made you take the plunge? πŸ™‚

Collapse
 
xdmarttt profile image
xdmarttt

Thank you!!
To be honest, I’ve always wanted to post and be more active. However, I was held back by a lack of confidence and was unsure about how my contributions would be received. Taking this first step was a big deal for me, and I’m genuinely happy and relieved that I’ve finally done it!!

Collapse
 
ra_jeeves profile image
Rajeev R. Sharma

Overcoming your fears is indeed a great achievement. I'm very happy for you πŸ™‚