Today, I was called for interview in Sopra Steria. Approximate 100 persons were present during the interview process. First round as technical test which consists of 2 complex questions from hackersearth websites. Though I failed in first round itself. One question I tries to solve and I solved that 80% but could not make to 2nd round. Question which I attempted is given below along with solution so that you can post and anybody can get help.
Q. 1 You have to take input from input device with the below sysntax :
Line 1 : T No of test cases to be checked.
Line 2 : Array Count.
Line 3 : No of elements of the array.
Line 4 : Kth largest element to be found. If not found return -1.
Line 2, 3 and 4 will get repeated accordingly what we have entered in line 1.
Rule : We need to find the differences of ant 2 numbers, if it is not present then we should add this to given array. So we would have all numbers difference present in array in last.
For Example : INPUT :
1
5
1 3 5 6 7
2
1
1
2
Input : Line 1 is : we want to pass 2 test cases.
Line 2 : Array size of First Array.
Line 3 : Array Elements separated by SPACE
Line 4 : Need to find out 2nd Largest elements.
Line 5 : Array Size of Second array for test case 2.
Line 6 : Array Elements
Line 7 : 2nd largest elements to be found
Output :
6
-1
Solutions :
package soptasteria;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
int testCasesCount = sc.nextInt();
int [] result = new int [testCasesCount];
for ( int i = 0 ; i < testCasesCount; i++) {
int arraySize = sc.nextInt();
// System.err.println("Enter arr element after arr size");
// System.err.println("Array Entered : "+sc.nextLine());
sc.nextLine();
String[] strArr = sc.nextLine().split( " " );
int [] array = makeArrayOfInteger(strArr);
// System.out.println("Printing integer array");
int kth = sc.nextInt();
// System.out.println("Array Entered : ");
printArray(array, arraySize);
result[i] = goForBusiness(array, arraySize, kth);
// System.out.println("Result : "+result[i]);
}
System.out.println( "Output : " );
for ( int i = 0 ; i < testCasesCount; i++) {
System.out.println(result[i]);
}
}
private static int goForBusiness( int arr[], int len, int kthLargest)
{
int lastIndex = len;
for ( int i = 0 ; i < len; i++) {
for ( int j = i + 1 ; j < len; j++) {
int d = Math.abs(arr[i] - arr[j]);
// System.out.println("Checked "+i+"th and "+j+"th element with differnece : "+d);
if (!searchInArray(arr, lastIndex, d)) {
arr[lastIndex++] = d;
}
}
}
Arrays.sort(arr, 0 , lastIndex);
// System.out.print("After Sort : ");
// printArray(arr, lastIndex);
if (kthLargest > lastIndex)
return - 1 ;
return arr[lastIndex - kthLargest];
}
private static int [] makeArrayOfInteger(String[] str)
{
int [] arr = new int [str.length * 4 ];
for ( int i = 0 ; i < str.length; i++)
arr[i] = Integer.parseInt(str[i]);
return arr;
}
private static boolean searchInArray( int arr[], int len, int ele)
{
for ( int i = 0 ; i < len; i++)
if (arr[i] == ele)
return true ;
return false ;
}
private static void printArray( int arr[], int len)
{
for ( int i = 0 ; i < len; i++)
System.out.print( " " + arr[i]);
System.out.println( "" );
}
} |