-2

I have used arraylist for the same in a TCS hiring contest for a question in java and my friend use vector in c++ but my I didn't qualify and he did, but both had the same logic..what should I do for the next time .please give suggestion

  • 2
    A little too less information. Maybe explain a bit what the requirments are. And why your ArrayList approuch wasn't qualified? – n247s Aug 17 '18 at 11:24
  • What is the type of the elements stored in the ArrayList? – Henry Aug 17 '18 at 11:30
  • Welcome to StackOverflow! As @n247s said, please edit your question and include more details about the requirements and what your approach was. Cheers :) – vatbub Aug 17 '18 at 11:33
  • Please add the code of both the solutions, yours and your friend's. – mudit_sen Aug 17 '18 at 11:42

1 Answers1

3

I wrote this snippet to test your scenario -

BigDecimal i = BigDecimal.valueOf(10).pow(9);
    ArrayList<BigDecimal> arr = new ArrayList();

    while(i.compareTo(BigDecimal.ZERO) != 0) {
        arr.add(BigDecimal.ONE);
        i.subtract(BigDecimal.ONE);
    }

OUTPUT ->

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3210)

On the other hand, LinkedList holds up better(Reference:How much data can a List can hold at the maximum?) but fails in the end ->

BigDecimal i = BigDecimal.valueOf(10).pow(9);
    BigDecimal value = i;
    LinkedList<BigDecimal> ll = new LinkedList<>();

    while(i.compareTo(BigDecimal.ZERO) != 0) {
        ll.add(value);
        i.subtract(BigDecimal.ONE);
    }

OUTPUT ->

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded