Home » Java » Timeout Exception while sorting bigger List

Timeout Exception while sorting bigger List

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’m trying to solve big-sorting problem from HackerRank. I have below solution for that:

static String[] bigSorting(String[] unsorted) {

    List<BigInteger> newValues = new ArrayList<BigInteger>();

    String[] newValuesArray = new String[unsorted.length];
    for (int i = 0; i < unsorted.length; i++) {
        newValues.add(new BigInteger((unsorted[i])));
    }

    Collections.sort(newValues);

    for (int i = 0; i < newValues.size(); i++) {
        newValuesArray[i] = newValues.get(i).toString();
    }
    return newValuesArray;
}

Although my solution is working if input is short but for lengthy input it giving the TimeoutException

As I come to know the testcase which causing issue is :

total input = 7693
And Values :
123121
22
2
23123
12312
2
8400195277734975809347292766456055069919837818826921595732345474832683881284408515491064519242069257576624629524016550879441266062080977804902962370685876553901611732
And So on...until 7693 values

So I have question here regarding Collections.sort(newValues); weather it causing the issue as there is huge numbers to be sort so it might be taking time or something else ?

Please find below snap regarding output :

enter image description here

And this is what it takes as input

enter image description here

Answers: