Facebook Interview Question
Country: United States
Interview Type: Phone Interview
1. Use Power function in log time
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
2. Use hash for duplicate numbers in the set.
I am still trying to understand the rationale behind this question.
Seems like a simple question
public class FacebookQuestion {
public static void main(String[] args)
{
int[] input = {1,3,5};
System.out.print("input array: {");
for(int i = 0; i < input.length; i++){
System.out.print(input[i]);
if(i < input.length - 1){
System.out.print(", ");
}
}
System.out.println("}");
int[] squareArray = squareArray(input);
System.out.print("squared array: {");
for(int i = 0; i < squareArray.length; i++){
System.out.print(squareArray[i]);
if(i < squareArray.length - 1){
System.out.print(", ");
}
}
System.out.println("}");
}
public static int[] squareArray(int[] input){
for(int i = 0; i < input.length; i++){
input[i] = input[i] * input[i];
}
return input;
}
}
the missing and very relevant constraint is that the input numbers can be negative and the returned array should be sorted
- Carlos February 26, 2017