Federico Pintaluba
I'm a 28 years old developer, I have been coding for more than a decade, but I have focused in Android Development for about 7 years, and in Flutter for the past 2 years. Here you will find all my projects, ideas, thoughts. Basically everything I made with <3.

HackerRank: Grading Students


HackerLand University has the following grading policy:

  • Every student receives a grade in the inclusive range from 0 to 100.
  • Any grade less than 40 is a failing grade.

Sam is a professor at the university and likes to round each student’s grade according to these rules:

  • If the difference between the grade and the next multiple of 5 is less than 3, round  up to the next multiple of .
  • If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.

For example, grade = 48 will be rounded to 85 but grade = 29 will not be rounded because the rounding would result in a number that is less than 40.

Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.

Function Description

Complete the function gradingStudents in the editor below. It should return an integer array consisting of rounded grades.

gradingStudents has the following parameter(s):

  • grades: an array of integers representing grades before rounding

Input Format

The first line contains a single integer, n, the number of students.
Each line i of the n subsequent lines contains a single integer, grades[i], denoting student i‘s grade.


Output Format

For each grades[i], print the rounded grade on a new line.

Sample Input 0


Sample Output 0


Explanation 0

  1. Student 1 received a 73, and the next multiple of 5 from 73 is 75. Since 75 – 73 < 3, the student’s grade is rounded to 75.
  2. Student 2 received a 67, and the next multiple of 5 from 67 is 70. Since 70 – 67 = 3, the grade will not be modified and the student’s final grade is 67.
  3. Student 3 received a 38, and the next multiple of 5 from 38 is 40. Since 40 – 38 < 3, the student’s grade will be rounded to 40.
  4. Student 4 received a grade below 38, so the grade will not be modified and the student’s final grade is 38.


So we have two cases where we return grade = grade, when it’s below 38, and when the remainder of grade % 5 is < 3, that will make the difference between gradle and the next multiplier the opposite.

So, we can map our function parameter to set our new gradle values like this:

 grades.map {if (it < 38 || it.rem(5) < 3) it }

Now, otherwise, we want to get the next higher multiple of 5, so what we do is some basic math.

We sum 5 minus the remainder of gradle, to gradle.

it + (5 - it.rem(5))

Now, we can wrap this into the map and return as an expression body function, our solution will end like this:

fun gradingStudents(grades: Array<Int>): Array<Int> =
    grades.map {
        if (it < 38 || it.rem(5) < 3) it
        else it + (5 - it.rem(5))

Remember to convert this to an array, since map will return a List, we do this by calling toTypedArray() on our mapped list.

Let’s run the test cases…

Hope this helped you understand a fair solution made in Kotlin for this problem, if you have any suggestion or a better way of doing this in the same language, don’t hesistate on commenting below!


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *