# Selenium Automation Interview questions | Java algorithms 11-15

As soon as you write Selenium Java in your resume, interviewer is bound to give you a sheet of paper & ask write a Java program. Though Selenium doesn’t require extensive Java knowledge but still there is a set of basic Java programs that you should be able to write on-the-spot. Continuing on our Java interview series for Selenium Automation, this article covers five of the common Java programs frequently asked in the basic technical interview.

The questions are easy, but don’t forget to mark these java-program in your to do list before attending any entry-level technical interview for Selenium automation testing.

Flashback: Selenium Automation Interview Questions | Java Algorithms 6-10

**Java program | Remove Duplicate numbers**

One of the simple, yet common algorithms – given an array of numbers, write a program to remove the duplicates. As you might have guessed, the logic is quite simple – compare the current element with further elements until you find a duplicate OR identify its uniqueness. Keep adding the unique elements to the output array.

```
package Java;
public class RemoveDuplicates {
public static int[] removeDuplicates(int[] input){
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
output[k] = input[k];
}
return output;
}
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" ");
}
}
}
```

**Reverse a number**

This particular algorithm takes me back to school days. How will you reverse a number? Or rather how will you extract the last digit of a particular number? Simple, keep the remainder after diving it by 10. Now that you have extracted the last digit, what’s next? Yeah! Remove the last digit and iterate until you get to the first digit. And how do you remove the last digit keeping all other digits as-is? Yeah! Divide the number by 10 to get an integer value.

```
package Java;
public class ReverseNumber {
public int reverse(int number){
int reverse = 0;
while(number != 0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
public static void main(String a[]){
ReverseNumber nr = new ReverseNumber();
System.out.println("Result: "+nr.reverse(17868));
}
}
```

**Calculate the sum of first 1000 Prime numbers**

A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11). The thumb rule – Every number is divisible by 1 and won’t be divisible by any number greater than n/2. Take the opposite route to identify if the number is prime, i.e. try dividing the number by 2, 3, 4…until n/2. If it is divisible, mark it as not prime – else if the loop ends, it is a prime number. At last, keep adding all the prime numbers from 1 to 1000.

```
package Java;
public class Sum1000Prime {
public static void main(String args[]){
int number = 2;
int count = 0;
long sum = 0;
while(count < 1000){
if(isPrimeNumber(number)){
sum += number;
count++;
}
number++;
}
System.out.println(sum);
}
private static boolean isPrimeNumber(int number){
for(int i=2; i<=number/2; i++){
if(number % i == 0){
return false;
}
}
return true;
}
}
```

**Swap two numbers without using temporary variable**

The general way to swap two numbers is to use a temporary variable to hold value of either one. But this program asks us specifically to swap two numbers without using a temporary variable. How? Again, it’s a simple mathematics trick.

- A = 10, B = 20;
- A = A + B, i.e. A = 30
- B = A – B, i.e. 30 – 20 = 10
- A = A – B, i.e. 30 – 10 = 20

Finally A = 20, B = 10. Simple right?

```
package Java;
public class Swap {
public static void main(String a[]){
int x = 10;
int y = 20;
System.out.println("Before swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
x = x+y;
y=x-y;
x=x-y;
System.out.println("After swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
}
}
```

**Identify two max numbers**

Out of a given array of numbers, identify the two maximum numbers. Initially let’s take the maxOne & maxTwo as 0. Now iterate through the array comparing the current element’s value first with maxOne and then with maxTwo.

- If its greater than maxOne, copy maxOne to maxTwo and element to maxOne.
- If its greater than maxTwo, copy the element to maxTwo
- If it is not greater than either maxOne & maxTwo, move on.

```
package Java;
public class TwoMaxNumbers {
public void printTwoMaxNumbers(int[] nums){
int maxOne = 0;
int maxTwo = 0;
for(int n:nums){
if(maxOne < n){
maxTwo = maxOne;
maxOne =n;
} else if(maxTwo < n){
maxTwo = n;
}
}
System.out.println("First Max Number: "+maxOne);
System.out.println("Second Max Number: "+maxTwo);
}
public static void main(String a[]){
int num[] = {5,34,78,2,45,1,99,23};
TwoMaxNumbers tmn = new TwoMaxNumbers();
tmn.printTwoMaxNumbers(num);
}
}
```

These are just some simple programs related to array processing. Very simple, in fact. But something is better than nothing. It’s never too late to start learning something new. Forget the notion of “Software Testers need not program”. In today’s competitive era, we as Software Testers need to adapt to changing times & at least learn the basics of programming. At least!

## Community Talks