1

I got a test on codility for a job interview yesterday. This is the second question and I still can't figure out the solution. You have to find the bug in the code without removing or adding lines. I added a while loop it compiled fine but didn't change much. Any solutions?

import java.util.*;

class minimum {
    int minimum(int[] A, int[] B) {
        int n = A.length;
        int m = B.length;;
        Arrays.sort(A);
        Arrays.sort(B);
        int i = 0;
        for (int k = 0; k < n; k++) {
            if (i < m - 1 && B[i] < A[k])
                i += 1;
            if (A[k] == B[i])
                return A[k];
        }
        return -1;
    }
}
  • What your application is supposed to do and where are you stuck? – PM 77-1 Nov 01 '18 at 18:43
  • 5
    https://stackoverflow.com/questions/31828623/finding-common-minimum-value-between-two-arrays :-) – Lachezar Balev Nov 01 '18 at 18:44
  • It is supposed to check the minimum value in two arrays and return the minimal value that occurs in both arrays. More than likely didn't get the job because couldn't complete the question. I looked at https://stackoverflow.com/questions/31828623/finding-common-minimum-value-between-two-arrays example, but the test only allowed you to modify two lines, you couldn't add or delete lines. –  Nov 01 '18 at 18:46
  • @LachezarBalev already seen that. Test couldn’t allow you to add or delete lines. –  Nov 01 '18 at 18:47

1 Answers1

0

Assume a and b are your integer arrays, then you can use a code piece like below:

int commonMin = Integer.MAX_INTEGER;
for (int i=0; i<b.length; i++) {
  if (Arrays.asList(a).contains(b[i])) {
    if (b[i] < commonMin){
      commonMin = b[i];
    }
  }
}
System.out.print(commonMin);
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82