Alcatel Lucent Interview Question
Country: United States
Interview Type: In-Person
typo. O(1) Space complexity.
For quadratic equation ax^2 + bx + c = 0, the answer x = (b +/- sqrt(b * b - 4 * a * c)) / 2a;
This code isn't O(n2) the for loops only cycle through each array once and only touching each element once. The for loops would need to be nested to be O(n2). Although, depending on the language using ".length" can be frowned upon because rather than storing the value it is retrieving it every iteration. Well done on the solution.
public List<Integer> findDiff (int [] A , int [] B){
if (B.length > A.length) return findDiff (B,A);
HashSet<Integer> set = new HashSet<Integer> ();
List<Integer> result = new ArrayList<Integer> ();
for (int a : A) set.add(a);
for (int b : B) {
if (!set.contains(b)) result.add(b) ;
}
return result ;
}
The logic to the solution given by Jack,
Since we know that we are looking at 2 additional values
diff A - B will give the sum of 2 additional numbers ( x + y)
diff of A^2- B^2 will give x^2 + y^2
we also know that (x+y)^2 = x^2 + y^2 + 2*x*y, so x*y can be computed
We are trying to find x-y , using the property (x-y)^2 = (x+y)^2 - 4x*y
Then we can solve (x+y) and (x-y) to get x and y.
I hope this helps.
I edited my code incase arrayA out of bounds and also break when we have gotten the two unknown numbers instead of going through the whole thing:
var arrA =[1, 4, 2, 6, 3];
var arrB =[4, 0, 7, 6, 3, 2, 1];
arrA = arrA.sort();
arrB = arrB.sort();
exe();
function exe(){
var i = 0;
var j = 0;
var count = 0;
for(number in arrB){
if(arrA[i] != arrB[j]){
j++;
count++;
console.log(arrB[number]);
}else{
i != arrA.length ? i++ : i;
j++;
}
if (count == 2){
break;
}
}
}
def findTwo(A, B):
difference = sum(B) - sum(A)
differenceSqrs = sum(x ** 2 for x in B) - sum(x ** 2 for x in A)
a1 = int((difference + (2 * differenceSqrs - difference ** 2) ** 0.5) / 2)
a2 = int((difference + (2 * differenceSqrs - difference ** 2) ** 0.5) / 2)
if sum(1 for x in A if x == a1) != sum(1 for x in B if x == a1):
return a1, difference - a1
return a2, difference - a2
Algorithm in O(N) time complexity and O(1) space complexity.
Let 2 numbers be a,b
Find addition and multiplication of each numbers from 2 arrays.
a+b=(arrB[0]+...arrB[n+2]) - (arrA[0]+...arrA[n])
a*b=(arrB[0]*...*arrB[n+2]) / (arrA[0]*...*arrA[n])
Given a+b and a*b, finding a and b is one step.
Assumption-values are integers and result of multiplication is within range.
The shortest way, I suppose.
static void Main(string[] args)
{
int[] a = new int[] { 1, 4, 2, 6, 3 };
int[] b = new int[] { 4, 0, 7, 6, 3, 2, 1 };
var ans = a.Concat(b).GroupBy(x => x).Where(y => y.Count() == 1).Select(z => z.Key).ToList();
foreach (int item in ans)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
Suppose you are told that two numbers, x and y, have a certain sum x+y=S, and a certain product xy=P. How to find S and P?
We can use the fact that we know how to solve quadratic equations. Notice that
(t−x)(t−y) = t^2 −(x+y)t + xy= t^2−St+P.
That means that x and y are precisely the solutions to
t^2 − St + P = 0.
So what we can do is:
public static void main(String[] args) {
int[] A = { 1, 4, 2, 6, 3 };
int[] B = {4, 0, 7, 6, 3, 2, 1};
int S = 0, P = 1;
int a = 0, b = 0;
for(int i: A) {
S += i;
P *= (i==0)?1:i; //Avoid zeroing the product
}
for(int j: B) {
S -= j;
P /= (j==0)?1:j; //avoid division by zero
}
S = Math.abs(S);
//Solve t^2 - St + P = 0
a = (int) ( S + Math.sqrt(S*S - 4*P) ) / 2;
b = (int) ( S - Math.sqrt(S*S - 4*P) ) / 2;
System.out.println("The different numbers are: " + a + ", " + b);
}
That was a way that seemed clear to me... There could be easier ways for other people. But that`s the way I would go with.
Time complexity O(n) iterates only once through each array, and Space complexity O(1) because the only additional storage needed are the variables a,b,S and P.
Hope this helps somebody.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void findUnCommonNumbers(int[] a,int[] b)
{
HashMap<Integer,Integer> input = new HashMap<Integer,Integer>();
for(int i:a)
{
input.put(i,i);
}
for(int j:b)
{
if(!input.containsKey(j))
{
System.out.print(" "+j);
}
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int a[] ={1, 4, 2, 6, 3} ;
int b[]={4, 0,7, 6, 3, 2, 1};
findUnCommonNumbers(a,b);
}
}
public class UncommonData {
static int ar1[] = {1,4,2,6,3};
static int ar2[] = {4,0,7,6,3,2,1};
static void getUncommonData(int ar1[],int ar2[])
{
for(int i : ar2)
{
int count = 0;
for(int j: ar1)
{
if(i==j)
break;
else
count++;
}
if(count==5)
System.out.println(i);
}
}
public static void main(String[] args) {
getUncommonData(ar1,ar2);
}
Haven't tried, but I guess the following algo should work.
for (i=0; i< a.length; i++) A1 = a[i] ^ b[i];
A1 = A1 ^ b[a.length] ^ b[a.length + 1];
for (i=0; i<b.length; i++)
{
if (A1 ^ b[i]) continue;
else cout << b[i] << endl;
}
O(n) time complexity and O(1) time complexity.
- jack December 14, 2014