Visa Interview Question for Staff Engineers


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

function copy (A, i, j) {
  var B = []
  for (var k = 0; i <= j; ++i, ++k) {
    B[k] = A[i]
  }
  return B
}

function shiftForward (A, i, j) {
  var d = j - i + 1
  var k = j + 1
  for (; k < A.length; ++k) {
    for (var l = 1; l <= d; ++l) {
      A[k - l] = A[k]
    }
  }
}
function shiftBack (A, i, j) {
  var d = j - i + 1
  var k = j - d
  for (; k >= 1; --k) {
    for (var l = 1; l <= d; ++l) {
      A[k + l] = A[k]
    }
  }
}

function write(A, B, i, j) {
  var k = 0
  for (; k < B.length; ++k) {
    A[i + k] = B[k]
  }
}

function type2 (A, i, j) {
  var B = copy(A, i, j)
  shiftForward(A, i, j)
  write(A, B, A.length - 1 - (j - i))
}
function type1(A, i, j) {
  var B = copy(A, i, j)
  shiftBack(A, i, j)
  write(A, B, 1)
}

//var A = [null, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

//type1(A, 3, 4)
//type2(A, 1, 2)
//
//console.log(A)

function query (N, M) {
  var A = [null]
  for (var i = 1; i <= N; ++i) {
    A[i] = i
  }
  for (i = 0; i < M.length; ++i) {
    var q = M[i].split(' ')
    for (var j = 0; j < q.length; ++j) {
      q[j] = parseInt(q[j], 10)
    }
    if (q[0] === 1) {
      type1(A, q[1], q[2])
    } else {
      type2(A, q[1], q[2])
    }
  }
  console.log(A)
}

query(10, [
  '1 3 5',
  '2 1 2'
])

- srterpe December 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

}

- Madhavi December 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}

}

- Madhavi December 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}

}

- Madhavi December 03, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More