Some array based coding interview questions, which require to build some logic to solve the problem. These questions are mainly asked to check your problem solving skill apart from your command on programming language. Actually, once you know the logic, you can solve these problem in any programming language e.g. C, C++, Python, Ruby or JavaScript.
Problem 1 : How to find missing number in array of 1 to 100 in Java? [
solution]
You have given an array of integer which contains numbers from 1 to 100, but exactly one number is missing, how do you find that number? You can use additional data structure.
Problem 2 : How do you find all pair whose sum is equal to given number from integer array in Java? [
solution]
You have given an array of int primitives and a number, you need to find all pairs in array whose sum is equal to given number e.g. if array is {1, 2, 3, 4, 5} and given sum is 6 then your program should return {2, 4} and {1, 5}
Problem 3 : How do you remove duplicates from array in Java? [
solution]
You have given an array, which could be array of numbers or Strings or any objects. Some objects are added multiple times in array, you need to remove those elements from array. You don't know how many duplicates are there. You can use additional data structure and memory to solve this problem.
Problem 4 : How do you reverse an array in Java? [
solution]
Given an array of String or integer, how do you reverse array, so that first element becomes last and last element becomes first e.g. if given array is {1, 2, 3, 4} then your program should reverse it as {4, 3, 2, 1}. You can use additional memory but reversing array in place will get bonus point.
Problem 5 : How to find duplicates numbers in array of integers in Java? [
solution]
You have given an array {1, 1, 2, 3, 3, 4}, write a program to return duplicate elements e.g. 1 and 3 because they have appeared more than once in array.
Problem 6 : How to find top two numbers from an integer array in Java? [
solution]
You have given an integer array e.g. { 3, 4, 5, 6 , 7}, you need to find top 2 numbers from this array e.g. your program should print 6 and 7.
What would the following program will printpublic class Main {public static void main(String[] args) {Collection c = new HashSet();print(c);}public static void print(Collection c){System.out.println("Collection");}public static void print(Set s){System.out.println("Set");}public static void print(HashSet hs){System.out.println("HashSet");}}
Ans: It will print "Collection" because methods are static and so they are bonded during compile time and at that time only type information is available because object is created at runtime.