Learn practical skills, build real-world projects, and advance your career

Climbing the Leaderboard

project_name = "climbing-the-leaderboard"

Problem Statement

An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this:

  • The player with the highest score is ranked number one the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

Example

ranked=[100,90,90,80]ranked = [100, 90, 90, 80]
player=[70,80,105]player = [70, 80, 105]

The ranked players will have ranks , 1,2,21, 2, 2 and 33, respectively. If the player's scores are 70,8070, 80 and 105105, their rankings after each game are 4th,3rd4th, 3rd and 1st1st. Return [4,3,1][4, 3, 1].

Function Description

Complete the climbingLeaderboard function in the editor below.

climbingLeaderboard has the following parameter(s):

int ranked[n]: the leaderboard scores
int player[m]: the player's scores

Returns

int[m]: the player's rank after each new score

Source: HackerRank

The Method

Here's the systematic strategy we'll apply for solving problems:

  1. State the problem clearly. Identify the input & output formats.
  2. Come up with some example inputs & outputs. Try to cover all edge cases.
  3. Come up with a correct solution for the problem. State it in plain English.
  4. Implement the solution and test it using example inputs. Fix bugs, if any.
  5. Analyze the algorithm's complexity and identify inefficiencies, if any.
  6. Apply the right technique to overcome the inefficiency. Repeat steps 3 to 6.

Solution

1. State the problem clearly. Identify the input & output formats.

While this problem is stated clearly enough, it's always useful to try and express in your own words, in a way that makes it most clear for you.

Problem

We are given a leader board with ranked scores for an arcade game. A player plays the game for some number of times and make some scores each time. We have to display the scores on leader at the end of each game and return the ranking for each game score. Dense ranking is used to rank the scores on leader board.


Input

  1. ranked: List of scores on leaderboard (sorted in descending order)
  2. player: List of Player's score after each game

Output

  1. List of rank of player after each game (If the player made lowest score from previous one then there will be no change in the rank of the player on the leaderboard)

Based on the above, we can now create a signature of our function: