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 gradesdoesn't make the list read-only. It means the variable can't be pointed at another list. The list frommutableListOfcan still grow withadd.- The function returns
List<String>, Kotlin's read-only list type. Returning aMutableList<String>where aList<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 calledscore, but each function has its own variables: the call hands the helper the loop's current value. - The letters are
Stringvalues in double quotes, notCharvalues in single quotes. The function's return type isString, soreturn '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 >= 60came first, a 95 would return"D", because every later test is unreachable. - Returning inside the loop. That ends
letterGradesafter 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].
