I'm doing this hackerrank problem and am confused. The first element in the parameter array is an int representing number of grades, the rest are the grades themselves. In hackerrank, itself, I have this code (yes it could likely be better), and it passes:
public static List<Integer> gradingStudents(List<Integer> grades) {
List<Integer> result = new ArrayList<>();
for (int i=0;i<grades.size();i++) {
int grade = grades.get(i);
if (grade >= 38) {
int nextMultipleOfFive=0;
for (int j=grade;j<grade+6;j++) {
if (j%5==0) {
nextMultipleOfFive = j;
break;
}
}
if (nextMultipleOfFive-grade < 3) {
result.add(nextMultipleOfFive);
} else {
result.add(grade);
}
} else {
result.add(grade);
}
}
return result;
}
Fine. But if I run that same code locally, I add main method
public static void main(String[] args) {
List<Integer> grades = Arrays.asList(4,73,67,38,33);
System.out.println(gradingStudents(grades));
}
...and get [4, 75, 67, 40, 33]. Wrong answer.
On the other hand, if I edit the for loop to start at 1: for (int i=1;i<grades.size();i++) {, locally I get [75, 67, 40, 33]. Correct answer.
It seems like I should start at 1 since the first list element is not a grade.
For instances where hackerrank is used as a code test for an interview, this is unnerving. Am I looking at this wrong? Doesn't looping from 1 make more sense? Why does looping from zero work, and looping from 1 not work?

