Interview Q&A

Selenium Automation Interview questions | Java algorithms 6-10

Algorithms are the heart of computer science, and the subject has countless practical applications as well as intellectual depth. Algorithms power the biggest web companies and the most promising startups. Interviews at tech companies start with questions that probe for good algorithm thinking.

FlashbackWrite A Java Program | 5 Commonly Asked Java-Program In Selenium Interview

Algorithms are the heart of computer science

Java algorithms for Selenium Interview

An algorithm is a well-defined procedure that allows a computer to solve a problem. Another way to describe an algorithm is a sequence of unambiguous instructions. The use of the term ‘unambiguous’ indicates that there is no room for subjective interpretation. Every time you ask your computer to carry out the same algorithm, it will do it in exactly the same manner with the exact same result.

6. Binary Search in an Array

Binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful or the remaining half is empty.

Binary Search Algorithm

The logic: Define three variables denoting lower index, highest index and middle index. Check for the element at middle index and then shift either the lower index or the highest index accordingly.

Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and log is the logarithm.

7. Find out duplicate number between 1 to N numbers.

An array is given with a range of numbers between 1 to N, where one of the numbers is repeated. You need to write a program to find out the duplicate number.

The logic: Ideally the sum of 1 to N numbers is n*(n+1)/2. Since one of the number is repeated, calculate the actual sum and minus it from the ideal sum to get the duplicate number.

8. Linear Search

Linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.

The logic: Run a loop from 0 till array length searching for the element.

Linear search runs in at worst linear time O(n) and makes at most n comparisons, where n is the length of the list.

9. Identify Odd Even

Any integer that can be divided exactly by 2 is an even number.

The logic: Check for the remainder when number is divided by 2. If the remainder is 0, the number is even – else odd.

10. Check for Prime number

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime because 1 and 5 are its only positive integer factors, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6.

The logic: Run a loop from 2 till n/2 and return false in case the input is divisible by any of the iterator.

Hope these initial algorithms will help you get started with Java programming. In subsequent articles we will try to cover as many popular Java algorithms as we can – so that you are prepared for basic Selenium automation interview. If you know of other popular algorithms, do let us know in the comments section below.

By the way, don’t forget to share it with your friends & colleagues!

Leave a Reply

Your email address will not be published. Required fields are marked *