Fabrix Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




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

O(n) runtime and O(n) memory:

public boolean hasTwoNumbersThatSumValue(int[] arr, int num){
    if(arr == null){
        throw new NullPointerException();
    }

    HashSet<Integer> alreadyEncountered = new HashSet<Integer>();
    for(int i = 0; i < arr.length; i++){
        int val = arr[i];
        if(alreadyEncountered.contains(num-val)){
            return true;
        }
        alreadyEncountered.add(val);
    }
    return false;
}

- zortlord June 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

what will happen if the array has a billion integers and the duplicate element is at the very end of the array?

- Anonymous June 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ask your interviewer if there are any limitations known for supplied integer values. If there are not, watch out for possible integer overflows when calculating num - val.

- anonymous June 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

dude,
when using HashSet, you can't say memory is O(n).

- spoderman June 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

@spoderman -
Yes it is O(n). At most 1 Integer will be created for each integer in the array hence the O(n) memory.

@jaja
Look ups into a HashSet and HashMap are O(1) because it relies upon the Hash function computation to find the appropriate bucket.

- zortlord June 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This answer is insufficient, if the array is too big to fit into memory this solution will not work. If the duplicate element resides at the very end of the array, you will run out of memory before you can build your entire hashtable.
@bt has the right answer.

- Anonymous June 13, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Well zortlord, interviewer deliberately put "large" array in the question. It means that you cannot afford to have one more O(N) memory into RAM. here we have two approaches

1) use bitset for candidacy. It will use 1/32 memory than set or hashmap.
2) have a distributed hash table if you have N nodes. and distribute array values to arr[i]%N node. while traversing back again check into respective node for candidacy.

- bt June 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is the correct answer, @zortlord's solution will not work if array is too large to fit into memory

- Anonymous June 13, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean hasTwoNumbersThatSumValue(int[] arr, int num){
    if(arr == null){
        throw new NullPointerException();
    }

	// Sort begin,  the array, in increasing order
	//Sort end

	int left = 0;
	int right = arr.length;
	while(left < right) {
		int sum = arr[left] + arr[right];
		if(sum == num) {
			return true;
		}
		else if(sum < num) {
			left++;
		}
		else{
			right++;
		}
	}
	
	return false;
}

- Vivek Kumar June 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

space O(n) if merge sort
or O(1) if quick sort

time O(nlogn)

public boolean hasTwoNumbersThatSumValue(int[] arr, int num){
    if(arr == null){
        throw new NullPointerException();
    }

	// Sort begin,  the array, in increasing order
	//Sort end

	int left = 0;
	int right = arr.length;
	while(left < right) {
		int sum = arr[left] + arr[right];
		if(sum == num) {
			return true;
		}
		else if(sum < num) {
			left++;
		}
		else{
			right--;
		}
	}
	
	return false;
}

- Vivek June 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think you want to do right-- in below else condition

else{
			right++;
		}

- diptivs June 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes , you are right.

- Vivek June 13, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will take time O(n) and space O(n)

map<int,bool>m;
bool TwoNumbersThatSumValue(int a[],int num)
{
    for(int i=0;i<sizeof(a)/sizeof(a[0]),i++)
    {
        if(m[num-a[i]]==true)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    int a[]={1,9,4,5};  //let us take this for example;
    int sum=14;        //Searching for sum==14
    for(int i=0;i<sizeof(a)/sizeof(a[0]);i++)
        m[a[i]]=true;
    TwoNumbersThatSumValue(a,sum);
}

- competitivecoder June 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is an O(nlogn) Time complexity solution with O(1) space complexity

1. Sort the array
2. Take two indexes from start and end
3. If values of indexes is equal to sum, this is the answer
else if value is > sum, Move last index down
else as value < sum, move start index up.

This algorithm will change the array.

- pc June 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ruby impl using Set. Running time: O(n). Space: O(n). In practice, it's more efficient to allocate enough space to a data structure in advance.

require 'set'

def two_numbers_sum_to_value_using_set(array, num) 
  return [] if !array.kind_of?(Array) || array.length<=1
  return array if array.kind_of?(Array) && array.length==2 && array[0]+array[1]==num
  
  set=Set.new(array)
  
  array.any? do |value|
    num-value != value && set.include?(num-value)
  end
end

array=[1,2,3,4,5,9,8,7,6]
puts "Two numbers? #{two_numbers_sum_to_value_using_set(array,13)}"

- Yev June 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

boolean hasTwoNumbersThatSumValue(int[] arr, int num) {
    for (int i = 0; i < arr.length; i++) {
        for (int j = (i + 1); j < arr.length; j++) {
            if ((arr[i] + arr[j]) == num) {
                return true;
            }
        }
    }
    
    return false;
}

- Daniel June 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you Know the meaning of Large?

- vishgupta92 June 12, 2015 | Flag


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