Citrix Online Interview Question
SDE-2sCountry: India
Interview Type: In-Person
unsigned int remove_key(unsigned int a[], unsigned int size, unsigned int key)
{
unsigned int left = 0;
unsigned int right = size - 1;
while( left < right )
{
while( left < size && a[left] != key)
left++;
while( right >left && a[right] == key )
right--;
swap(a,left,right);
}
return right;
}
Hey parvez ,
after removing all the occurrences of the number array size will be changed right...??
so need to accept size as a pointer so it will be reflect in the main ()...
function is returing new size of array, all elements after "right" index are duplicate occurrences of "key".
Nikhil,
"main" is not the only functions that can/does call other functions.
So your function signature is something like this it seems [you should have maybe said it in original question :) ]:
typedef unsigned int uint;
void removeit(uint a[], uint remelem, uint *size)
{
for( i=0; i<(*size); i++)
if( a[i] == remelem )
a[i] = a[(*size)-- -1];
}
Psycho, he doesn't mention "stable" in the requirements.
Move onto next Q man.
int* removeNumberFromArray(int number, int array[])
{
int count = 0;
int size = (sizeof(array)/sizeof(array[0]));
int j= size - 1;
for (int i=0; i< (size - count); i++) {
if(array[i] == number)
{
while(array[j] == number)
{
j--;
}
array[i] = array[j];
array[j] = '\0';
j--;
count++;
}
}
return array1;
}
public static int RemoveAllOccurance_Suffle(int[] a, int key)
{
int left = 0;
int right = a.Length - 1;
int end = right;
while (left <= right)
{
if (a[left] == key)
{
a[left] = 0;
swap(a, left, end);
end--;
}
left++;
if (a[right] == key)
{
a[right] = 0;
swap(a, right, end);
end--;
}
right++;
}
return end;
}
public static int RemoveAllOccurance_Suffle(int[] a, int key)
{
int left = 0;
int right = a.Length - 1;
int end = right;
while (left <= right)
{
if (a[left] == key)
{
a[left] = 0;
swap(a, left, end);
end--;
}
left++;
if (a[right] == key)
{
a[right] = 0;
swap(a, right, end);
end--;
}
right++;
}
return end;
}
In java
- vcd April 03, 2014