Adobe Interview Question for Software Engineer / Developers






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

sort the array first or is it already sorted?

- Anonymous November 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

brute force: ( can b optimized)
1.sort the array (nlogn)
2.for every pair in the array, compute sqrt(num1^2+num2^2). do a binary search for this value (n^2 * logn)

total complexity:
O(nlogn + (n^2)logn) ==> O((n^2)logn)

- blueskin November 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

should square be done first before sort to handle negative numbers?

- Anonymous November 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya, that handles negatives and saves computations.

- blueskin.neo November 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

also check out pythagorean triple math forum
its theory might offer some optimizations I think

- Anonymous November 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a hashmap?

- Rayden November 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How does that improve the complexity?

- blueskin.neo November 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make an auxillary array b
such that
b[i]=a[i]*a[i]


Now there was a problem in which we are given a sorted array and we need to find all pair that sum up to x.

Now we can take x for each array index.

So total complexity would be O(n*n)

Now this problem reduces to a problem where we are provided a sorted array and we need to find all pair such

- nitin December 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is best approch as far as time is concerned but require an o(n) space.

- guruji December 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why do you want to create another array unless the condition is specified that you cannot manipulate the given array . We can manipulate the given array and solve the problem without extra space

- Anonymous February 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey20156" class="run-this">#!/usr/bin/env python

a = [-1, 2, 4, -3, 5, 10]

b = [x*x for x in a]

b = sorted(b)
print b

for x in reversed(b):
left = 0
right = len(b)-1
while not left is right:
tmp = b[left]+b[right]
if tmp is x:
print b[left], b[right], x
break
elif tmp > x:
right -= 1
else:
left += 1
</pre><pre title="CodeMonkey20156" input="yes">
</pre>

- Anonymous December 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you dont need to waste o(n) space. square the array itself in time o(n) and then the problem reduces to finding pairs summing to a value in the same array.
1. sort the new array o(nlogn)

for all i in n
   j from 1 to i and k from i-1 to 0 at the same time
      if a[k]+a[j]=a[i] bingo shift both indexes 
      inc j if a[j]+a[k]<a[i] else
      dec k if a[j]+a[k]>a[i]

inner loop - o(i) and i from 0-n
again this is o(n^2)

- fabregas February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I thought of one more solution , which is first to take all the elements and put them one by one into a hash table , and then run 2 loops , taking a[i] and a[j] and computing the value of sqrt(a[i]^2 + a[j]^2) and find the resulting elements in the hash table , if they are found they form the triplet , so it takes an overall time of

O(n) {to hash all the elements} + O(n^2) {to run the loop and check for the elements from the triplet} ...

Because I feel if u already do a square and then sort u might loose the triplets like 3,4,5 and 3,4,-5 ..

suggestions / modifications are welcome.

- banerjee.abhik.hcl January 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

HI ALL
i have striked something else like first we should put all odd numbers on one side and even on another o(n)
then square them o(n)
then if we analyse all even squars ends in either 0,4,6 and odd in 1,5,9
so we can further arrange in o(n)
Further with 0 all numbers will themselves be the pairs like 4+0=4 so we are leaving zero now will see further depending on question requirement
now for 1)odd+odd=even 2)even +even =even 3)even+odd=odd we wil apply these will looking for match
i do not know how to calculate last step time complexity and what could be the next best approach
any suggestions........and feedback

- CrazyFrog February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Further we can use hashing, but how is not clear right now

- CrazyFrog February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below is the java code. Complexity in O(n^2). and It uses O(n) extra space. If anyone is able to do it in less than this, please post the soln here:

public class Triplets {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int arr[] = {3, -4, 6, 2, 5, 24, 28, 25, 7};
		findTriplets(arr);
	}

	private static void findTriplets(int arr[]) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>(arr.length);
		
		for(int i = 0; i < arr.length; i++) {
			map.put(arr[i]*arr[i], arr[i]);
		}
		
		for(int i = 0; i < arr.length; i++) {
			for(int j = i+1; j <arr.length; j++) {
				int key = arr[i]*arr[i] + arr[j]*arr[j];
				Integer val = map.get(key);
				if(val != null) {
					System.out.println("(" + arr[i] + ", " + arr[j] + ", " + val + ")");
				}
			}
		}
	}
}

- droid.geek7 March 21, 2011 | 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