chocalaca
chocalaca

Reputation: 406

Do hackerrank challenge(s) treat first element specially?

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;
    }

enter image description here enter image description here

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?

Upvotes: 0

Views: 296

Answers (2)

Bohemian
Bohemian

Reputation: 425083

The "lines of data", which includes the length, are not passed to gradingStudents(). Only the data elements (ie not the length) are passed.

If you scroll down in that problem, you'll see that the main() method reads all lines of data from stdin and builds the list of 4 elements then passes that list to the gradingStudents() that you must implement.

Upvotes: 1

trincot
trincot

Reputation: 350345

The text of the code challenge says:

The first line contains a single integer, 𝑛, the number of students.

Each line 𝑖 of the 𝑛 subsequent lines contains a single integer, grades[𝑖].

But then if you look at the code template you get to work with, you'll see it takes care of reading the input and creating the data structure for it. It then calls your function with that data structure as argument. Obviously this does not include the information that was in the first line, just a List (which has a size) or whatever array type you get in the different languages you can use to solve these challenges.

Upvotes: 2

Related Questions