Expedia Interview Question for SDE-2s

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
7
of 11 vote

private static int[] sortBasedOnZero(int[] arr) {
int k=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==0)
{
int temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
k++;
}
}
return arr;
}

Comment hidden because of low score. Click to expand.
0

swap'll change the order.

Comment hidden because of low score. Click to expand.
0

I've run your code and its wrong. The way your algorithm works is that you swap the value of the array if its zero into the counter from the start which is correct. But the thing there is that how the value stored in the temp will be moved? another thing about the question is that it is organized considering that the zero must be at the first position of the array.

Comment hidden because of low score. Click to expand.
0

The code certainly pushes all the zero to the begining but changes the order of the nonzero element.

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

O(N) solution:

int i, j;
for ( j=i=arr.length() ; i >=0 ; i--)
{
# Skip over zero elements
if(arr[i] == 0)
continue;

# i landed on index where there is a non-zero element,
# thus copy it back to position j, and move j to next spot to fill.
arr[j] = arr[i];
j--;
}

# Zero fill remaining leading spots in array.
while( j >= 0 )
{
arr[j]=0;
j--;
}

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

I've come up with this code. If someone gives me suggestions on its quality, it will be highly helpful.

#include<stdio.h>

int main()
{
int arr[] = {1,2,3,0,0,0,4,5,6};
int i = 0,count = 0,temp = 0;
for(i = 0;i < sizeof(arr)/sizeof(arr[0]); i++ )
{
if(arr[i] == 0)
{
arr[i] = arr[count];
arr[count] = 0;
count++;
}

}
for(i = 0;i < 9; i++ )
{
printf("%d  ", arr[i]);
}
}

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

Is input in the form of array?

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

static int[] sortBasedOnZero(int[] arr)
{
Arrays.sort(arr);
return arr;
}

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

public static int[] moveZeros(int[] input) {

int zeroIndex = 0, noIndex = 0;

while(zeroIndex < input.length && noIndex < input.length) {
while(zeroIndex < input.length && input[zeroIndex] != 0) zeroIndex++;
while(noIndex < input.length && input[noIndex] == 0) noIndex++;

if(zeroIndex > noIndex && zeroIndex < input.length && noIndex < input.length) {
int temp = input[zeroIndex];
input[zeroIndex] = input[noIndex];
input[noIndex] = temp;
noIndex++; zeroIndex++;
}
}

return input;
}

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

public static int[] moveZeros(int[] input) {

int zeroIndex = 0, noIndex = 0;

while(zeroIndex < input.length && noIndex < input.length) {
while(zeroIndex < input.length && input[zeroIndex] != 0) zeroIndex++;
while(noIndex < input.length && input[noIndex] == 0) noIndex++;

if(zeroIndex > noIndex && zeroIndex < input.length && noIndex < input.length) {
int temp = input[zeroIndex];
input[zeroIndex] = input[noIndex];
input[noIndex] = temp;
noIndex++; zeroIndex++;
}
}

return input;
}

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

public class MoveZero {
public static void main(String args[]){
int[] num={1,2,3,0,0,0,4,5};
for(int i=0;i<num.length;i++){
if(num[i]==0){
int k=num[i];
for(int j=0; j<=i ; j++){
num[j]+=k;
k=num[j]-k;
num[j]-=k;
}
}
}
for(int i = 0;i<num.length;i++)
System.out.print(num[i]+"\t");
}
}

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

void moveAllZerosToBeging(int iArr[])
{
int zeroCnt = 0;
for(int i=0;i<SIZE;i++)
{
if(iArr[i] == 0)
{
zeroCnt++;
iArr[i] = iArr[zeroCnt-1];
iArr[zeroCnt-1]=0;
}
}
}

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

void moveAllZerosToBegin(int iArr[])
{
int zeroCnt = 0;
for(int i=0;i<SIZE;i++)
{
if(iArr[i] == 0)
{
zeroCnt++;
iArr[i] = iArr[zeroCnt-1];
iArr[zeroCnt-1]=0;
}
}
}

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

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10];
int n,c,index,i,temp;
c=1;
printf(" enter the size of array:=>");
scanf("%d",&n);
printf(" enter the element of array :=>");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
{
if(a[i]>0 && c>0)
{
index=i;
temp=a[i];
c--;
}

else
{
if(a[i]==0 && c==0)
{
a[index]=a[i];
a[i]=temp;
index++;
temp=a[index];
}

}

}
printf(" now required array :=>");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;

}

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

<!DOCTYPE html>
<html>
<body>
If you have any UI position available in Washington State plz reach me @ 425 949 6101
<p>A function can access variables defined inside the function:</p>

<button type="button" onclick="myFunction()">Click Me!</button>

<p id="demo"></p>

<script>
function myFunction() {
var arr = [1,2,3,0,0,0,1,0,4,5,0,5,0,9,6,0],i=0;
var arrN = [];
var arrN1 = [];
for(i=0;i<arr.length;i++){
if(arr[i] == 0){
arrN.push(0);
}
else{
arrN1.push(arr[i]);
}
}
arrN = arrN.concat(arrN1);
}

</script>

</body>
</html>

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

Following code should work:

private static int[] moveZeroesAhead(int[] num) {
int j = 0;
for (int i=1; i<num.length;i++) {
if(num[i]==0)
{
int count = i;
while (count>j && num[count-1] >0) {
num[count] = num[count-1];
num[count-1] = 0;
count--;
}
j++;
}
}
return num;
}

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

public static int[] MoveZeroToBeginning(int[] a)
{
var temp = new int[a.Length];
var currentIndex = 0;
for (var i = 0; i < a.Length; i++)
if (a[i] == 0) temp[currentIndex++] = a[i];

for (var i = 0; i < a.Length; i++)
if (a[i] != 0) temp[currentIndex++] = a[i];

return temp;
}

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

public static int[] MoveZeroToBeginning(int[] a)
{
var temp = new int[a.Length];
var currentIndex = 0;
for (var i = 0; i < a.Length; i++)
if (a[i] == 0) temp[currentIndex++] = a[i];

for (var i = 0; i < a.Length; i++)
if (a[i] != 0) temp[currentIndex++] = a[i];

return temp;
}

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

int zerocount=0;

int start = 0;
int end = arr[arr.length-1];
while(start<=end){

If(arr[start] == 0){
arr[start] = arr[zerocount];
arr[zerocount]=0;
zerocount++
start++;
}
if(arr[end]==0){
arr[end]=arr[zerocount];
arr[zerocount]=0;
zerocount ++;
end--;
}

}

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

public static void pushZeroToStart(int[] a) {
int zeroIndex = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == 0 && (i - zeroIndex) >= 2) {
int toReplace = a[i];
for (int j = zeroIndex; j <= i; j++) {
int temp = a[j];
a[j] = toReplace;
toReplace = temp;
}
zeroIndex++;
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}

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

Please note that we need to make sure that:
- Move all zeros to the first
- Keeps the order of others

public static int[] moveZero2Firsts(int[] inp) {
int zeroCount = 0;
int otherCout = 0;
for(int i = 0;i < inp.length; i ++) {
if(inp[i] == 0)
zeroCount++;
else
otherCout++;
}

int[] out = new int[inp.length];
int j = zeroCount;
for(int i = 0;i < inp.length; i ++) {
if(inp[i] != 0) {
out[j] = inp[i];
j++;
}

}

return out;
}

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

public static void zeroFirstPlaceSortedArray(int[] arr){
int tempIndex = arr.length-1;
int zeroCount=0;

//to count zero
for(int i=0; i<arr.length;i++){
if(arr[i] == 0){
zeroCount++;
}

}

//to set non zero in ending indices
for(int i=tempIndex;i>=0 ;i--){
if(arr[i] != 0){
swap(arr, tempIndex, i);
tempIndex--;
}
}

//to set zero in initial indices
for(int i =0; i<zeroCount; i++)
arr[i]= 0;

}

private static void swap(int[] arr, int tempIndex, int i) {
int tempInt;
tempInt = arr[tempIndex];
arr[tempIndex]= arr[i];
arr[i]= tempInt;
}

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

int[] MoveZeroToFirst(int[] x)
{
int[] result=new int[x.Length];
int j=0;
for (int i=0; i<x.Length;i++)
{
if (x[i]==0)
{
j++;
}
}
for (int i = 0; i < x.Length; i++)
{
if (x[i]!=0)
{
result[j] = x[i];
j++;
}
}
return result;
}

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

def move_zero_to_first_stable(a):
n = len(a)
j = n-1
for i in range(n-1, -1, -1):
if a[i] is 0:
continue
a[j] = a[i]
j -= 1
while j >= 0:
a[j] = 0
j -= 1
return a

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

void moo(vector<int> &v)
{
int n = v.size(), z = n - 1;
for(int i = n - 1; i >= 0; --i) {
if(v[i] != 0) {
swap(v[i],v[z--]);
}
}
}

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

public static int[] moveZeros(int[] array) {
int left = 0;
int right = array.length -1;

while (left < right) {
if (array[left] != 0 && array[right] == 0) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
left++;
right--;
} else if (array[left] != 0 && array[right] != 0) {
right--;
} else if (array[left] == 0){
left++;
}
}
return array;
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

and

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

public class MoveZeros {
private static int[] moveZerosToFront(int[] arr) {
int count = arr.length -1 ;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
arr[count--] = arr[i];
}
}
for (int j = 0; j <= count; j++) {
arr[j] = 0;
}
return arr;
}

public static void main(String[] args) {
int[] arr = { 1, 2, 3, 0, 0, 0, 4, 5, 0, 6, 7, 0 };
int[] ans = moveZerosToFront(arr);
for (int i = 0; i < ans.length; i++) {
System.out.println(arr[i]);
}
}
}

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

// TimeComplexity O(n) and space Complexity O(1).
int[] zeroSortedArray(int[] arr) {

int counter = 0;
for (int index = 0; index < arr.length; index++) {
if (arr[index] == 0) {
arr[index] = arr[counter];
arr[counter] = 0;
counter++;

}
}

return arr;
}

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

public int[] moveZeroToleft(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
arr[count++] = arr[i];
}
}
int zero = arr.length - 1;
while (count > 0) {
arr[zero--] = arr[--count];
}
while (zero >= 0) {
arr[zero--] = 0;
}
return arr;
}

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

public int[] moveZeroToleft(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
arr[count++] = arr[i];
}
}
int zero = arr.length - 1;
while (count > 0) {
arr[zero--] = arr[--count];
}
while (zero >= 0) {
arr[zero--] = 0;
}
return arr;
}

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

public int[] moveZeroToleft(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
arr[count++] = arr[i];
}
}
int zero = arr.length - 1;
while (count > 0) {
arr[zero--] = arr[--count];
}
while (zero >= 0) {
arr[zero--] = 0;
}
return arr;
}

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

public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}

int curt = nums.length - 1;

for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] != 0) {
int temp = nums[i];
nums[i] = nums[curt];
nums[curt--] = temp;
}

}

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

#include <stdio.h>

int main()
{
int i,count,n;
int arr[]={0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9};
n=sizeof(arr)/sizeof(arr[0]);
//  printf("%d\n",n);
count=n-1;
for(i=n-1;i>=0;i--)
{
if(arr[i]!=0)
arr[count--]=arr[i];
}
while(count>=0)
arr[count--]=0;

for(i=0;i<n;i++)
printf("%d",arr[i]);

return 0;
}

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

using System;

public class Program
{
public static void Main()
{
int [] array = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
MoveZeroesFront(array);
for(int i=0; i<array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
public static void MoveZeroesFront(int [] array)
{
int count = array.Length-1;

for(int k = array.Length-1; k>=0;k--)
{
if(array[k] != 0)
{
int t = array[count];
array[count] = array[k];
array[k] = t;
count--;
}
}
}
}

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

using System;

public class Program
{
public static void Main()
{
int [] array = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
MoveZeroesFront(array);
for(int i=0; i<array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
public static void MoveZeroesFront(int [] array)
{
int count = array.Length-1;

for(int k = array.Length-1; k>=0;k--)
{
if(array[k] != 0)
{
int t = array[count];
array[count] = array[k];
array[k] = t;
count--;
}
}
}
}

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

public class MoveAllZerosToBeginning {

public static void main(String[] args) {
// TODO Auto-generated method stub

Integer a[]= {1,2,3,0,14,0,4,5,0,0,9,0,-2};
Arrays.sort(a, new CustomComparator(a));

System.out.println("Array after sorting: ");

for(int i: a) {
System.out.print(i+" ");
}
}

static class CustomComparator implements Comparator<Integer>{

Map<Integer,Integer> m=new HashMap<>();

public CustomComparator(Integer a[]) {
for(int i=0;i<a.length;i++) {
m.put(a[i], i);
}
}

@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if(o1==0) {
return o1.compareTo(o2);
}
else {
return m.get(o1).compareTo(m.get(o2));
}
}

}

}

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

#include <iostream>
using namespace std;

void pushZerosToEnd(int arr[], int n)
{
int count = n-1;
for (int i=n-1; i>=0; i--)
if (arr[i] != 0)
arr[count--] = arr[i];
while (count >= 0)
arr[count--] = 0;
}

// Driver program to test above function
int main()
{
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = sizeof(arr) / sizeof(arr[0]);
pushZerosToEnd(arr, n);
cout << "Array after pushing all zeros to end of array :n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}

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

#include <iostream>
using namespace std;

void pushZerosToEnd(int arr[], int n)
{
int count = n-1;
for (int i=n-1; i>=0; i--)
if (arr[i] != 0)
arr[count--] = arr[i];
while (count >= 0)
arr[count--] = 0;
}

// Driver program to test above function
int main()
{
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = sizeof(arr) / sizeof(arr[0]);
pushZerosToEnd(arr, n);
cout << "Array after pushing all zeros to end of array :n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}

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.

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.