2

Our assignment was to solve the Hackerrank question on arraylist without using 2D arrays or lists. Basically, you would need to input multiple arrays of different sizes and display an element based on the input of (array number, position). My implementation seemed to work just fine for my test cases but failed 4/6 of Hackerrank's test cases. Our lecturer's code (of course) worked perfectly. But what I fail to understand, is advantage of his approach:

My Code ::

import java.io.PrintStream;
import java.util.Scanner;

class arraylist {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int NoOfLines = sc.nextInt();
        int[] input = new int[20000];//To store all input arrays one after the other in one 1D array.
        int[] index = new int[NoOfLines];//Starting positions of each array input.
        int[] NoOfArrayElements = new int[NoOfLines];//Sizes of each corresponding input array.
        int position = 0;
        int count = 0;
        int arrayelementpos = 0;

        //Store the input and note the size of each array and the index position.
        for (int i = 0; i < NoOfLines; i++) {
            int arrarLength = sc.nextInt();
            NoOfArrayElements[arrayelementpos++] = arrarLength;
            index[position++] = count;
            for (int j = 0; j < arrarLength; j++)
                input[count++] = sc.nextInt();
        }

        //Code to input queries (array no, element position)
        int NoOfQueries = sc.nextInt();
        int[] result = new int[NoOfQueries];
        int pos = 0;

        for (int i = 0; i < NoOfQueries; i++) {
            int arrayNo = sc.nextInt();
            int element = sc.nextInt();

            if ((arrayNo > NoOfLines) || element > NoOfArrayElements[arrayNo - 1]) {
                System.out.println("ERROR!");
                continue;
            }

            pos = index[arrayNo - 1] + element - 1;
            System.out.println("THE ELEMENT IS ::" + input[pos]);

        }
    }
} 

My lecturer's code ::

import java.io.*;
import java.util.*;

public class arraylistsolved {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Object [] store=new Object[n];
        for(int i=0; i<n;i++){
            int d=sc.nextInt();
            int [] ar=new int[d];
            for(int j=0;j<d;j++) {
                ar[j]=sc.nextInt();                
            }
            store[i]=ar;
        }

        int q=sc.nextInt();
        for(int i=0;i<q;i++){
            int array=sc.nextInt()-1;
            int element=sc.nextInt()-1;
            Object obj=store[array];
            int [] retrieve = (int []) obj;

            if(array>n||element>retrieve.length-1)
                System.out.println("ERROR!");
            else
                System.out.println(retrieve[element]);


        }

    }


}

As mentioned, both the codes are working for small test cases, but mine breaks down for very large ones for some reason. You can try copy-pasting the code here: https://www.hackerrank.com/challenges/java-arraylist/problem

Macindows
  • 209
  • 3
  • 14
  • Are you asking us to first read the problem, then go through your code and find your errors? – Nazar Merza Feb 22 '19 at 15:55
  • You are basicly asking us to debug your program. It would be better to paste your code in an IDE an run the debugger yourself – curiouscupcake Feb 22 '19 at 15:57
  • 4
    your lecturer uses camelCase for class names, I wouldn't take anything he says seriously – Tim Feb 22 '19 at 15:59
  • Hi, welcome to StackOverflow. Actually, your code looks more in the spirit of what's expected in the task than the code of your lecturer, since he is using basically a 2D array. If your code fails in some cases, I'd propose to first run it locally with different inputs (checking also "corner cases", like the elements on the borders of arrays) and find the specific ones that fail. Best way to do this would be to write a unit test (see e.g. https://stackoverflow.com/questions/8751553/how-to-write-a-unit-test). When you know the specific cases that fail, use a debugger to find the bug in the code – Forketyfork Feb 22 '19 at 16:09
  • Yup, agreed. Both you and your lecturer should follow the Java Naming Conventions. Class names are in PascalCase, method and variable names in camelCase. Enum constants and `static final` variables in UPPER_SNAKE_CASE. Package names all lowercase. – MC Emperor Feb 22 '19 at 16:10

0 Answers0