'Foobar code working in IDE but not in Solution file

I've written code for a foobar challenge that works in my IDE but not in the solutions file provided by foobar. Also, is there anyway to show the output even if the test fails? Possibly to with it being a static method or the input being {1, 2, 3, 4} whereas mine is working with new int {1,2,3,4,5}? My code is:

public static int solution(int[] l) {
    List<Integer> numberList = Arrays.stream(l).boxed().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    while (true) {
        StringBuilder number = new StringBuilder();
        int i = 0;
        while (i < numberList.size()) {
            number.append(numberList.get(i));
            i++;
        }
        List<Integer> startingList = Arrays.stream(l).boxed().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        int testValue = numberList.size();
        for (Integer integer : numberList) {
            if (startingList.contains(integer)) {
                startingList.remove(integer);
                testValue--;
            }
        }
        if (testValue == 0) {
            int f = 0;
            int total = 0;
            while (f < numberList.size()) {
                total = total + numberList.get(f);
                f++;
            }
            if (total % 3 == 0) {
                StringBuilder answer = new StringBuilder();
                int c = 0;
                while (c < numberList.size()) {
                    answer.append(numberList.get(c));
                    c++;
                }
                return Integer.parseInt(answer.toString());
            }
        }
        Integer nextNumber = Integer.parseInt(number.toString()) - 1;
        String[] stringArray = valueOf(nextNumber).split("");
        numberList = new ArrayList<>();
        for (String s : stringArray) {
            numberList.add(Integer.parseInt(s));
        }
    }
}

Pretty rubbish but it does the job (at least in my IDE!)



Solution 1:[1]

As mentioned in a comment on the question, you should undoubtedly give some more context for your questions (since it is pretty unclear what your code is intended to do). I'm pretty sure I've inferred the actual question from context though, and I can suggest a couple of problems. In short (and a pretty good assumption for coding in general) the issue is not the environment running your code incorrectly, but rather your code having missed bugs due to lack of comprehensive testing. If you had presented a number of sample inputs and results I would guess you would have seen that your solution does not work locally.

  1. The Java List.remove() method takes an index rather than a value to be removed (https://docs.oracle.com/javase/8/docs/api/java/util/List.html). The way it is used in your sample will result in throwing exceptions in a number of circumstances. Proper testing would have identified this (and will pick up most of your problems if fixed)

  2. What happens if there is no solution? For example, an input of {1, 1} is going to get into a pretty messy state as the 'nextNumber' value slips below 0. You should know what the desired behavior is in this situation, and your tests should cover it before you try to upload a solution

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user18462886