Amazon Interview Question






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

sounds like a variant of quick sort

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

can you give an example?

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

I doubt that constant space is reasonable to achieve for an interview question. Because this question is essentially the merging step of in-place merge sort, which is known to be too difficult to be practical.

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

Indeed. One of the saner posts here :-)

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

Can you explain the question further ? May be an example ?

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

It is like merging two sorted lists n/2 each

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

Merge sort in constant space...

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

I cannot imagine people thinking merge sort can be done in constant space!

- Ashish Kaila August 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Constant extra space. It reuses the array space cleverly.

Search the web for in-place merge sort.

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

We need to use modified Insertion sort.
i.e insert the element of second half within the elements of first half.
The range of search can be limited to improve efficiency.
Sorry, difficult to document the logic.

- gavinashg September 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can be done in constant space but time complexity will be greater than O(n).

- getjar.com/todotasklist my android app September 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

comjnl.oxfordjournals.org/cgi/reprint/35/6/643.pdf

- Prateek Caire November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think it can be done in constant space and linear time by taking two pointers at beginning of each sorted position and then swapping and adjusting pointers as we compare the values pointed by them

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

definitely..

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

Not that easy.

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

It won't solve the problem, the moment you swap the numbers, it may make one of the two sets unsorted.

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

Total number of integeres : n
As each half contains n/2 sorted integers, Is it safe to assume that we have two sorted array. Modified version of merge sort can be used.

Assume you have two arrays
each one of n/2,
if you start comparing the
element from the last element of each array . if the element in the first half is greater then swap it with the element with the second half element and decrement the counter for first half else decrement the counter for second half.

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

this is wrong...after the initial 2-3 swaps we will lose the sorted data and we will not get proper order

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

class InplaceMerger
{
public:
	static void MergeInplace(std::vector<int>& arr)
	{
		auto current1 = arr.begin();
		auto current2 = arr.begin() + arr.size()/2;
		auto end = arr.end();

		while(current1 != end &&  current2 != end)
		{
			if(*current1 > *current2)
			{
				std::swap(*current1, *current2);
			}
			if(current1 + 1 == current2) current2++;
			else current1++;
		}
	}
};

- Stan September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i tried a dry-run but seams incorrect logic
data set used: 12 14 25 26 30 7 12 13 18 27

- sb December 21, 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