Google Interview Question






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

Thinking out loud:

Can this be thought of as variant of quick sort. For eg: Take a cap and partition the pens depending on whether it is loose or tight. One will exactly fit. Use that pen and partition the caps too. Now do this recursively on the two partitions. O(nlogn).

Comments/Suggestions welcome.

- Rohith August 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is the first sensible answer on this thread.. Well Done!! I am pretty sure this will work, it is a small twist on QuickSort

- Aman August 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this is known as "matching nut and bolts" problem. However, your O(nlogn) analysis is not quite correct. The recurrence is like:

T(n) = T(k) + T(n-k) + O(n)

which basically leads to O(n^2). However, O(nlogn) algorithm exists which was published in 1995 at some journal - which is too hard to grasp!

- lol August 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@lol
You are right. But I can define partition function to get exact O(nlogn) using median of five approach for partition. Adding the partition cost in the analysis still makes it O(nlogn) the recurrence will then become:

T(n) = T(n/5) + T(7n/10) + O(n)

Source: Wiki selection_algorithm

- Rohith August 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In practice, choosing a random pivot is more than enough to avoid the n^2 case. Median of 5 approach is usually too expensive in comparison.

Wiki: Quicksort#Analysis_of_Randomized_quicksort

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

What is meant by sorting here?
Do we have to
1) match pens and caps? OR
2) Sort pens separately and caps separately?

- Akash July 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

match pens with its size cap..... there exist unique pairs ie. no pen will match exactly other than 1 cap and vice versa

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

Quick sort and binary search. Pick one pivot cap and try all pens, eventually you will pair the first cap with its pen and successfully partition all the pens -- those smaller to the left and bigger to the right. Then pick the next cap. How to determine the which partition?. Try on the pen(s) you already capped. When we have more pens, it will looks like a binary search.

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

Using this method, to be able to find immediately the partition in which a cap would belong, you need to keep track of every partition made so far.
You can instead sort the pens and loop through the caps, binary searching among the pens for the right one.

- Jagat July 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

complexity in sorting approach is N sqaure , in above approach it is much less. please feel free to correct me.

- DHEERAJ July 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will you sort when you cannot compare pens or caps with each other ? And does the size of a pen also determine the size of the cap. Is it given that bigger the pen bigger the cap ?

- AJ July 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The numbers = The numbers of caps? If so then sort caps first and then sort pens. Now cap matches which have the same index.

- Wolvi July 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try using pens to sort caps and caps to sort pens(random quicksort). then use sorted caps and pens to get final answer.

- Kritivasas July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maps caps by size, then iterate through pens and matches then.
Space: O(n)
Time: O(n)

public static void match(Cap[] caps, Pen[] pens) {
	Map<Integer, Cap> capSize = new HashMap<Integer, Cap>();
	for (Cap cap : caps) {
		capSize.put(cap.size(), cap);
	}
	for (int i=0; i<pens.lengh; ++i)
		caps[i] = capSize.get(pens[i].size());
}

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

I guess the question already says, you can't compare two caps or two pens, which means you can really tell which cap/pen's size.

- Anony August 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I mean you can't really tell a cap's or pen's size

- Anony August 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why the hell people don't clearly describe a problem? If s/he is too busy, why the another need to pay heed to a convoluted question.

- anon August 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

construct a Binary tree

take a cap and using that as reference to break the pile into "loose" and "does't" fit. you would definitely find the pen that fits the cap.

take the second cap and compare with the pen that fits to root cap, if the new cap is too loose, select the to tight pile, if the cap is tight select the loose pile. now using this new cap as reference break the pile into loose and tight again.


approximate complexity is n + n/2 + n/4 + n/8 + .... + n/2^k = O(n log(n))

- Anony August 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This approach achieves time complexity but its hard on space complexity.

Instead

int i=pens.length - 1;
int j= caps.length - 1;

while (i>=0) {
    int k = j;
    while (k >= 0)  {
       if(pens[i].fits(caps[k]) {
            temp = caps[j];
            caps[j] = caps[k];
            caps[k] = temp;
            i = i -1;
            j = j - 1;
       }
    }
}

time complexity = n + n-1 + n-2 + ... + 1 = O(n^2)

- Anony August 03, 2011 | 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