## Interview Question for Java Developers

Team: 4
Country: United States
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1) Sort the array A of sizes
Step 2) Take 2 variables p and q, p =0 and q = 1
Loop:
for A[p] find the smallest A[q] by iteration, Then each of A[p] and A[q], A[q+1],..... ......... make one pair each.
temp = p+1
Loop:
if(A[p] == A[temp])
temp++;
else{
A[p] = A[temp];
break;
}
End Loop:
Loop:
Find next A[q] for the new p.by iteration.
if(q==A.length) return;
End Loop:
End Loop

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static void pairPencils(int[] pencils) {
Arrays.parallelSort(pencils);
int i = pencils.length/2;
int j = pencils.length - 1;
int pairs = 0;
while (i  > 0) {
if (pencils[i] <= pencils[j]) {
pairs++;
j--;
}
i--;
}
System.out.println(pairs+" "+(pencils.length - pairs * 2));``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static void pairPencils(int[] pencils) {
Arrays.parallelSort(pencils);
int i = pencils.length/2;
int j = pencils.length - 1;
int pairs = 0;
while (i  > 0) {
if (pencils[i] <= pencils[j]) {
pairs++;
j--;
}
i--;
}
System.out.println(pairs+" "+(pencils.length - pairs * 2));``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void pairPencils(int[] arr) {
Arrays.parallelSort(arr);

int i = 0;
int j = arr.length / 2;

int count = 0;

while (i < arr.length && j < arr.length) {
if (arr[i] == Integer.MAX_VALUE) {
i++;
continue;
}
if (arr[j] == Integer.MAX_VALUE) {
j++;
continue;
}
if (2 * arr[i] <= arr[j]) {
arr[i] = Integer.MAX_VALUE;
arr[j] = Integer.MAX_VALUE;
count++;
i++;
}
j++;
}

System.out.println(count + " " + (arr.length - 2 * count));
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void pairPencils(int []arr) {
Arrays.parallelSort(arr);

int i = 0;
int j = arr.length / 2;

int count = 0;

while (i < arr.length && j < arr.length) {
if (arr[i] == Integer.MAX_VALUE) {
i++;
continue;
}
if (arr[j] == Integer.MAX_VALUE) {
j++;
continue;
}
if (2 * arr[i] <= arr[j]) {
arr[i] = Integer.MAX_VALUE;
arr[j] = Integer.MAX_VALUE;
count++;
i++;
}
j++;
}

System.out.println(count + " " + (arr.length - 2 * count));
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static void pairPencils(int []arr) {
Arrays.parallelSort(arr);

int i = 0;
int j = arr.length / 2;

int count = 0;

while (i < arr.length && j < arr.length) {
if (arr[i] == Integer.MAX_VALUE) {
i++;
continue;
}
if (arr[j] == Integer.MAX_VALUE) {
j++;
continue;
}
if (2 * arr[i] <= arr[j]) {
arr[i] = Integer.MAX_VALUE;
arr[j] = Integer.MAX_VALUE;
count++;
i++;
}
j++;
}

System.out.println(count + " " + (arr.length - 2 * count));
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.