Visa Interview Question
Staff EngineersCountry: United States
Interview Type: Written Test
package com.Arrays;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
*
* Given two numbers N and M. N indicates the number of elements in the array A[](1- indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] .
You are given M queries. Queries can be of two types, type 1 and type 2.
Type 1 queries are represented as 1 i j : Modify the given array by removing elements from to and adding them to the front.
Type 2 queries are represented as 2 i j : Modify the given array by removing elements from to and adding them to the back.
Your task is to simply print | A[1] - A[N] | of the resulting array after the execution of M queries followed by the resulting array.
Note While adding at back or front the order of elements is preserved.
*
*
* */
public class Arrays1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader inr= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr);
int userInputN;
int userInputM;
String[] NArray;
Arrays1 a1 = new Arrays1();
try {
System.out.println("CEnter the valueof N: ");
userInputN =Integer.parseInt(br.readLine());
System.out.println("CEnter the valueof M: {Please enter 1 or 2} ");
userInputM = Integer.parseInt(br.readLine());
System.out.println("The valueof N: "+userInputN+"The valueof M:"+userInputM);
//Create an Array with the N Elements
System.out.println("Creating the N Array:");
System.out.println("Enter the Values one by one until the N number of Arrays: ");
NArray = new String[userInputN];
System.out.println("length of Array:"+NArray.length);
for(int i=0; i<NArray.length;i++){
System.out.println("Enter the "+i+" Element in the string Array:");
NArray[i]= br.readLine();
}
System.out.println("The Array consists of the folowing elements with indexes:");
for(int i=0; i<NArray.length;i++){
System.out.println("NArray["+i+"] --"+NArray[i].toString());
}
String[] type1Arry = new String[userInputN];
String[] type2Arry = new String[userInputN];
if(userInputM == 1){
type1Arry = a1.typeOneMquery(NArray, userInputN);
System.out.println("type1Arry.length===="+type1Arry.length);
for(int i=0; i<type1Arry.length-1;i++){
System.out.println("type1Arry["+i+"] --"+type1Arry[i].toString());
}
}else if(userInputM == 2){
type2Arry = a1.typeTwoMquery(NArray, userInputN);
System.out.println("type1Arry.length===="+type2Arry.length);
for(int i=0; i<type2Arry.length;i++){
System.out.println("type2Arry["+i+"] --"+type2Arry[i].toString());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[] typeOneMquery(String[] a,int userInputN){
String[] tempArr = new String[userInputN+1];
try{
System.out.println("Enter which element to be shifted to front");
InputStreamReader inr1= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr1);
int ele = Integer.parseInt(br.readLine());
tempArr[0]= a[ele];
for(int j=0;j<a.length;j++){
if(j!=ele){
System.out.println("inside j!=e -----J values is"+j);
tempArr[j+1]=a[j];
System.out.println("Element +"+tempArr[j+1].toString());
}
}
return tempArr;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return tempArr;
}
public String[] typeTwoMquery(String[] a,int userInputN){
String[] tempArr = new String[userInputN];
try{
System.out.println("Enter which element to be shifted to Back");
InputStreamReader inr1= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr1);
int ele = Integer.parseInt(br.readLine());
for(int j=0,i=j+1;j<a.length-1;j++,i++){
if(j!=ele){
System.out.println("inside j!=ele -----J values is"+j);
tempArr[j]=a[j];
System.out.println("Element +"+tempArr[j].toString());
}else{
System.out.println("inside j=ele -----J values is"+j);
tempArr[j]=a[i];
System.out.println("Element +"+tempArr[j].toString());
}
}
System.out.println("a.length"+a.length);
tempArr[a.length-1]= a[ele];
return tempArr;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return tempArr;
}
}
public class Arrays1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader inr= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr);
int userInputN;
int userInputM;
String[] NArray;
Arrays1 a1 = new Arrays1();
try {
System.out.println("CEnter the valueof N: ");
userInputN =Integer.parseInt(br.readLine());
System.out.println("CEnter the valueof M: {Please enter 1 or 2} ");
userInputM = Integer.parseInt(br.readLine());
System.out.println("The valueof N: "+userInputN+"The valueof M:"+userInputM);
//Create an Array with the N Elements
System.out.println("Creating the N Array:");
System.out.println("Enter the Values one by one until the N number of Arrays: ");
NArray = new String[userInputN];
System.out.println("length of Array:"+NArray.length);
for(int i=0; i<NArray.length;i++){
System.out.println("Enter the "+i+" Element in the string Array:");
NArray[i]= br.readLine();
}
System.out.println("The Array consists of the folowing elements with indexes:");
for(int i=0; i<NArray.length;i++){
System.out.println("NArray["+i+"] --"+NArray[i].toString());
}
String[] type1Arry = new String[userInputN];
String[] type2Arry = new String[userInputN];
if(userInputM == 1){
type1Arry = a1.typeOneMquery(NArray, userInputN);
System.out.println("type1Arry.length===="+type1Arry.length);
for(int i=0; i<type1Arry.length-1;i++){
System.out.println("type1Arry["+i+"] --"+type1Arry[i].toString());
}
}else if(userInputM == 2){
type2Arry = a1.typeTwoMquery(NArray, userInputN);
System.out.println("type1Arry.length===="+type2Arry.length);
for(int i=0; i<type2Arry.length;i++){
System.out.println("type2Arry["+i+"] --"+type2Arry[i].toString());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[] typeOneMquery(String[] a,int userInputN){
String[] tempArr = new String[userInputN+1];
try{
System.out.println("Enter which element to be shifted to front");
InputStreamReader inr1= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr1);
int ele = Integer.parseInt(br.readLine());
tempArr[0]= a[ele];
for(int j=0;j<a.length;j++){
if(j!=ele){
System.out.println("inside j!=e -----J values is"+j);
tempArr[j+1]=a[j];
System.out.println("Element +"+tempArr[j+1].toString());
}
}
return tempArr;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return tempArr;
}
public String[] typeTwoMquery(String[] a,int userInputN){
String[] tempArr = new String[userInputN];
try{
System.out.println("Enter which element to be shifted to Back");
InputStreamReader inr1= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr1);
int ele = Integer.parseInt(br.readLine());
for(int j=0,i=j+1;j<a.length-1;j++,i++){
if(j!=ele){
System.out.println("inside j!=ele -----J values is"+j);
tempArr[j]=a[j];
System.out.println("Element +"+tempArr[j].toString());
}else{
System.out.println("inside j=ele -----J values is"+j);
tempArr[j]=a[i];
System.out.println("Element +"+tempArr[j].toString());
}
}
System.out.println("a.length"+a.length);
tempArr[a.length-1]= a[ele];
return tempArr;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return tempArr;
}
}
public class Arrays1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader inr= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr);
int userInputN;
int userInputM;
String[] NArray;
Arrays1 a1 = new Arrays1();
try {
System.out.println("CEnter the valueof N: ");
userInputN =Integer.parseInt(br.readLine());
System.out.println("CEnter the valueof M: {Please enter 1 or 2} ");
userInputM = Integer.parseInt(br.readLine());
System.out.println("The valueof N: "+userInputN+"The valueof M:"+userInputM);
//Create an Array with the N Elements
System.out.println("Creating the N Array:");
System.out.println("Enter the Values one by one until the N number of Arrays: ");
NArray = new String[userInputN];
System.out.println("length of Array:"+NArray.length);
for(int i=0; i<NArray.length;i++){
System.out.println("Enter the "+i+" Element in the string Array:");
NArray[i]= br.readLine();
}
System.out.println("The Array consists of the folowing elements with indexes:");
for(int i=0; i<NArray.length;i++){
System.out.println("NArray["+i+"] --"+NArray[i].toString());
}
String[] type1Arry = new String[userInputN];
String[] type2Arry = new String[userInputN];
if(userInputM == 1){
type1Arry = a1.typeOneMquery(NArray, userInputN);
System.out.println("type1Arry.length===="+type1Arry.length);
for(int i=0; i<type1Arry.length-1;i++){
System.out.println("type1Arry["+i+"] --"+type1Arry[i].toString());
}
}else if(userInputM == 2){
type2Arry = a1.typeTwoMquery(NArray, userInputN);
System.out.println("type1Arry.length===="+type2Arry.length);
for(int i=0; i<type2Arry.length;i++){
System.out.println("type2Arry["+i+"] --"+type2Arry[i].toString());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[] typeOneMquery(String[] a,int userInputN){
String[] tempArr = new String[userInputN+1];
try{
System.out.println("Enter which element to be shifted to front");
InputStreamReader inr1= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr1);
int ele = Integer.parseInt(br.readLine());
tempArr[0]= a[ele];
for(int j=0;j<a.length;j++){
if(j!=ele){
System.out.println("inside j!=e -----J values is"+j);
tempArr[j+1]=a[j];
System.out.println("Element +"+tempArr[j+1].toString());
}
}
return tempArr;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return tempArr;
}
public String[] typeTwoMquery(String[] a,int userInputN){
String[] tempArr = new String[userInputN];
try{
System.out.println("Enter which element to be shifted to Back");
InputStreamReader inr1= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inr1);
int ele = Integer.parseInt(br.readLine());
for(int j=0,i=j+1;j<a.length-1;j++,i++){
if(j!=ele){
System.out.println("inside j!=ele -----J values is"+j);
tempArr[j]=a[j];
System.out.println("Element +"+tempArr[j].toString());
}else{
System.out.println("inside j=ele -----J values is"+j);
tempArr[j]=a[i];
System.out.println("Element +"+tempArr[j].toString());
}
}
System.out.println("a.length"+a.length);
tempArr[a.length-1]= a[ele];
return tempArr;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return tempArr;
}
}
- srterpe December 07, 2016