Start learning free

LESSON 243 OF 291 · HELPER FUNCTIONS

Letter Grades in Kotlin: A Helper Function and a Loop

Practice Read the full explanation

Given an array of integers scores, return the letter grade of every score, in the same order as the scores.

Each score gets one letter:

  • 90 to 100: "A"
  • 80 to 89: "B"
  • 70 to 79: "C"
  • 60 to 69: "D"
  • 0 to 59: "F"

The work is split between two functions. The first one is a helper: it gets one score and returns its letter. The second one, the function that is graded, goes over the scores and collects the helper's letter for each of them.

 

Example 1:

Input: scores = [95, 82, 71]
Output: ["A", "B", "C"]
Explanation: 95 is in the A range (90 to 100), 82 is in the B range and 71 is in the C range.

Example 2:

Input: scores = [65, 40]
Output: ["D", "F"]
Explanation: 65 is between 60 and 69, so it gets a D. 40 is below 60, so it gets an F.

Example 3:

Input: scores = [100, 90, 89]
Output: ["A", "A", "B"]
Explanation: 100 and 90 are both in the A range: 90 is its lowest score. 89 is one point short of it, so it gets a B.

 

Constraints:

  • scores has at least one score.
  • Every score is an integer from 0 to 100.
  • The result has one letter per score, in the same order as scores.
STEP 1 OF 9
    Ctrl + Enter to check

    KOTLIN · HELPER FUNCTIONS

    Letter Grades in Kotlin, explained

    Problem overview

    Given an IntArray of scores from 0 to 100, return the letter grade of each score, in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and 0 to 59 for "F".

    The work is split in two. The helper letterGrade turns one score into its letter. The function that is checked, letterGrades, goes over the scores and collects the helper's answer for each one. For [95, 82, 71] the result is [A, B, C], and for [100, 90, 89] it is [A, A, B]: 90 is the lowest A, and 89 is one point short.

    Approach

    Two small jobs are easier than one big one. Mapping a score to a letter is one job. Repeating it for every score is another. Keeping them apart means each function has one idea, and the loop reads almost like the problem statement.

    For the helper, check the ranges from the highest down. Because a return ends the function, a later check only sees scores that failed all earlier ones. After the "A" check, a score is below 90; after "B", below 80, and so on. So each check only needs a lower bound ("at least 80"), not a range like "between 80 and 89".

    Step by step

    The helper. Four if statements test score >= 90, score >= 80, score >= 70 and score >= 60, returning "A", "B", "C" and "D". The last line returns "F". Only scores below 60 get there, so it needs no test and no else.

    The main function. letterGrades starts with an empty list, mutableListOf<String>(), stored in a val named grades. A for loop visits every score in scores, and grades.add(letterGrade(score)) appends that score's letter. The loop runs once per score, so the list ends with one letter per score, in the original order. Finally return grades comes after the loop. Inside the loop it would return after the first score.

    Why split it into a helper

    Suppose the loop and the grade rules lived in one function. The four if statements would sit inside the for loop, each with its own return or its own add, and you would have to think about the loop and the ranges at the same time. With the helper, you can test the rules on their own, for example letterGrade(89) gives "B", before any list is involved. If the school later changes a boundary, only the helper changes.

    The same shape appears constantly in Kotlin code: a small function that transforms one value, and a loop that applies it to every element of a collection. Kotlin also has a shorter way to write the loop with map, but the explicit loop shows exactly what happens: one call per score, one add per call.

    The solution in Kotlin

    class Solution {
        fun letterGrade(score: Int): String {
            if (score >= 90) {
                return "A"
            }
            if (score >= 80) {
                return "B"
            }
            if (score >= 70) {
                return "C"
            }
            if (score >= 60) {
                return "D"
            }
            return "F"
        }
    
        fun letterGrades(scores: IntArray): List<String> {
            val grades = mutableListOf<String>()
            for (score in scores) {
                grades.add(letterGrade(score))
            }
            return grades
        }
    }
    

    Some Kotlin details:

    • val grades doesn't make the list read-only. It means the variable can't be pointed at another list. The list from mutableListOf can still grow with add.
    • The function returns List<String>, Kotlin's read-only list type. Returning a MutableList<String> where a List<String> is expected is fine, because a mutable list is also a list.
    • A function calls another function of the same class by name, as letterGrade(score) does. The helper's parameter is also called score, but each function has its own variables: the call hands the helper the loop's current value.
    • The letters are String values in double quotes, not Char values in single quotes. The function's return type is String, so return 'A' would not compile, and the list holds strings, so the lesson's tests compare text such as "A".
    • Printing a list of strings shows the items without quotes: [A, B, C].

    Complexity

    The helper does at most four comparisons, so it takes constant time. letterGrades calls it once per score, so for n scores the time is O(n). The result list holds n strings, so the extra space is O(n).

    Edge cases and common mistakes

    • The boundaries. 90, 80, 70 and 60 each belong to the higher grade, which is why the checks use >=. With >, a score of 90 would get a B. The scores 79, 80, 59, 60, 0, 69 and 70 give [C, B, F, D, F, D, C].
    • Checking in the wrong order. If score >= 60 came first, a 95 would return "D", because every later test is unreachable.
    • Returning inside the loop. That ends letterGrades after the first score.
    • Missing the final return. A function that declares a return type must return on every path, so the closing return "F" is required for the code to compile.

    Testing

    • letterGrades(intArrayOf(95, 82, 71)) gives [A, B, C].
    • letterGrades(intArrayOf(65, 40)) gives [D, F].
    • letterGrades(intArrayOf(100, 90, 89)) gives [A, A, B].
    • letterGrades(intArrayOf(79, 80, 59, 60, 0, 69, 70)) gives [C, B, F, D, F, D, C].
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free