VMWare Inc Interview Question for Software Engineer / Developers






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

public static void findAllPossibleBetter(int [] array, int target){
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();

for (int i=0;i<array.length;++i){
if (map.containsKey(array[i])){
System.out.println(map.get(array[i])+":"+i);
}else{
int dif=target-array[i];
map.put(dif, i);
}
}

}

- Anonymous May 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

This is still O(n^2) time and O(n) solution.

- Anonymous September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This looks like a neat trick. cool

- Nipun March 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if there is a duplicate number?

- shajib khan December 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if there is a duplicate number?

- shajib khan December 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if there is duplicate?

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

if there is duplicate?

- khan December 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

If extra space is not available, use a in-place O(nlogn) sort algorithm to sort the array. Have two pointers left, right that increase/decrease if current total is smaller/larger than target.

O(nlogn) space O(1)

- Anonymous April 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1 for effective solution.

- Anonymous September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the question says, return the index numbers. If we sort the array, the index for the numbers would change. So the question should either say return the numbers or this solution would require looking up index numbers for the found numbers. Am i missing something?

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

+1

- hr September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

<pre lang="" line="1" title="CodeMonkey91480" class="run-this">class Main
{
public static void hashMethod2(int[] arr, int sum) {

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

boolean found = false;
System.out.println(Arrays.toString(arr));
for (int i = 0; i < arr.length; ++i) {
Integer temp = map.get(sum - arr[i]);
if (temp == null) {
map.put(arr[i], i);
} else {
found = true;
System.out.println(String.
format("Given sum %d, found %d at index %d and %d at index %d.", sum,
arr[i], i, sum - arr[i], temp));
}
}
if (!found) {
System.out.println(String.format("Given %d, no solution is found.", sum));
}
}

public static void main(String[] args) {
int[] arr = { 1, 2, 13, 34, 9, 3, 23, 45, 8, 7 };
hashMethod2(arr, 11);
}
}</pre>

- Anonymous April 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

how abt this??

1. store in harsh table
2. for element i (0 to n)... look in to harsh for element (target - a[i])....

a look up will tale O(1) for n look up complexity is O(n)

- Anonymous July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Harsh table...seriously. What are you suggesting we put into this "harsh" table?

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

we store the array members as the key and their indices as the value in hash table.
The search will be faster and we can return the indices as well when a match is found.
we need space of O(n) but the algo will run in O(n)

- Heramb April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

searching takes O(log n) for sorted arrays and O(n) for non-sorted arrays how will u possibly check in the HASH map in O(1) time....

- hr September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh sorry HASH map is not array
M HIGH

- hr September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use hash table or another array.

go thru the arraylist and compute the difference between the target sum (e.g. 11) and the current member of the list you are lookng at. store that difference in hash or array storage, and also store the index from the original list in which this difference is obtained.

When done (thru the arraylist), do a search in the hash or storage for members in the arraylist that matches the difference. If found, you have the two indexes you need- one stored in the hash/storage, the other is the search result index.

This should be O(n).

- charles April 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

that only works when numbers are positive.

- Anonymous April 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array in O(nlogn) time , and then use binary search . O(nlogn) complexity .

- krishna June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int arr[5]={11,24,10,30,4};
int target=41;
int i=0;
int j=0;
for(i=0;i<5;i++)
{
int number = arr[i];
for(j=i+1;j<5;j++)
{
int number2= arr[j];
if(target==number+number2)
{

printf("\n-----------------");
printf("\n 1st number %d",arr[i]);
printf("\n 1st number index %d",i);
printf("\n 2nd number %d",arr[j]);
printf("\n 2nd number index %d",j);
printf("\n-----------------");
}

}
}


getchar();

- ajay August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The algorithm below finds 1 pair. However, it can be easily worked to find all possible pairs. Also it return 1 or -1 depending on whether any required pair exists or not. Users can work it to return indexes

hasArrayTwoCandidates (A[], ar_size, sum)
1) Sort the array in increasing order.
2) Initialize two index variables to find the candidate
elements in the sorted array.
(a) Initialize first to the leftmost index: l = 0
(b) Initialize second the rightmost index: r = ar_size-1
3) Loop while l < r.
(a) If (A[l] + A[r] == sum) then return 1
(b) Else if( A[l] + A[r] < sum ) then l++
(c) Else r--
4) No candidates in whole array - return -1

- Saurabh Singhal August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int check_sum(int a[],int n,int k){
	sort(a,n);                     //o(nlogn)
	int left=0,right=n-1;
	while(left<right){
		if(a[left]+a[right]==k)
			return 1;
		else if(a[left]+a[right]>k)
			right--;
		else
			left++;
	}
	return 0;
}

- mgatuiet September 21, 2014 | Flag Reply


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