Amazon Interview Question for Software Engineer / Developers






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

hey arun
Is it the question of amazon online exam. I got the same question.
R u able to solve. ?

- Varun March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Probably not, since no one has solved the pancake sorting problem: http :// en.wikipedia.org / wiki / Pancake_sorting

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

Simple Bubble sort or shell sort.

- Sad boy March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yeah..simple bubble sort,since in bubble sort we swap only consecutive elements, so that can be done using reverse(i,i+1);

- XYZ March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

or what abt selection sort

- abc March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- bubble sort
- selection sort
- insertion sort

One example:

void insertionSort(int[] arr){
for( int i = 1; i < arr.length; i++ ){
int index = i;
for( int j = i-1; j >= 0; j-- ){
if( arr[j] > arr[index] ){
reverse(arr, j, index);
index = j;
}
}
}
}

- m@}{ March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah it can be done by bubble sort reverse(i,i+1)
But we are not making use of reversing entire sub array.
We just swapping ...
I think there exists a better algo..

- eshwar_147 March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Quicksort maybe also used:

void quicksort(int[] arr, int from, int to){

if( to -from < 2 ){
return;
}

int i = -1;
int j = 0;

int pivotElement = arr[to];

for(int k = 0; k < to; k++){
if(arr[k] <= pivotElement ){
reverse(arr, i+1, j);
++i;
}
++j;
}

reverse(arr, i+1, to);

quicksort(arr, from, i);
quicksort(arr, i+2, to);
}

- m@}{ March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry error:
if( to -from < 2 ){
return;
}

Fixed:

if( from >= to ){
return;
}

- m@}{ March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Vector is a wrapper to std::vector with length(), get(i), and reverse(i,j)

#include <vector>
template <typename T>
class Vector
{
public:
    Vector(T t[], unsigned int n) {
        for(unsigned int i = 0; i < n; i++) {
            v.push_back(t[i]);
        }
    }
    size_t length() {
        return v.size();
    }
    T get(unsigned int i) {
        if (i >= length())
            std::__throw_out_of_range;
        return v[i];
    }
    void reverse(unsigned int i, unsigned int j) {
        if (i == j)
            return;
        v[i] = get(i) ^ get(j);
        v[j] = get(i) ^ get(j);
        v[i] = get(i) ^ get(j);
    }
protected:
    std::vector<T>v;
};
void quicksort(Vector<int>&v, unsigned int left = 0, unsigned int right = 0)
{
    if (right == 0 || right >= v.length() - 1)
        right = v.length() - 1;
    int pivot = v.get(right);
    unsigned int j = left;
    for (unsigned int i = left; i <= right - 1; i++) {
        if (v.get(i) < pivot)
            v.reverse(i, j++);
    }
    v.reverse(j, right);
    if (left < j - 1)
        quicksort(v, left, j - 1);
    if (j + 1 < right)
        quicksort(v, j + 1, right);
}

int main()
{
    int a[] = {9, 27, 35, -8, 7, -3, 4, 12};
    Vector<int>v(a, sizeof(a) / sizeof(a[0]));
    quicksort(v);
    for(unsigned int i = 0; i < v.length(); i++)
        printf("%d ", v.get(i));
    printf("\n");
}

Output:
-8 -3 4 7 9 12 27 35

- leeym March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gone mad?? reverse(i,j) should reverse all the elements from i to j... looks at "pancake sorting" in wiki

- swathi June 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@varun Yes , it was asked in amazon online test.

Looks like merge sort is the most efficient solution for this problem.

- arun March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey Arun. Out of 4 how many u solved.
I was able to solve 3 question.

- varun March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@varun the time given is too short. i was able to solve 3 questions and fourth partially. i was not able to pass all the testcases for certain questions. How about u ?

- arun March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Arun.

I have done 2 question completely
one sorting one i used selection sort
and 4th string one i cannot able to submit.

Hey can i have ur gmail id.So tht we can add each other.

- varun March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@varun wats ur gmail id. let me add u ..

- arun March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

quich sort with single index .... see keringhan ...

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

Hi... Can i have the gmail id's of varun and arun... Guys.. I am also looking for the Job in Amazon.. But i am preparing rite now.. Please can i discuss with you some of your experiences about the tests @ amason.....

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

How to appear for online test @ Amazon??

- Rahul March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is a O(n) algorithm for this particular problem.
Hint:

"The system exposes 3 functions of O(1)"
and one of them is reverse(i,j) - reverses the elements in the array from index i to index j (both indexes

doing reverse(i,j) in O(1) is impossible in real world, but nevertheless this hypothetical system gives you this power, use it.

- the wizard March 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@wizard: O(n)..???

- NA March 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Think of insertion sort. Assume that 1 to k is sorted. Consider x the number at k+1. Using binary search find the location p where the x has to be inserted. then to insert x perform reverse(p,k+1) and reverse(p+1,k+1). Now the array 1 to k+1 is sorted. This is worst case nlog(n).

- its no worse than O(nlog(n)), Is O(n) possible? March 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It seems that you are expecting reverse(i,j) as an O(1) operation

- Vikash March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes,that's what is given

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

Are you sure this is 0(nlogn) solution . It seems to be 0(n^2) to me.

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

k=0;
while(k <length(arr))
{
for(i=1;i<length(arr);i++)
{
if(getindex(a[k])>getindex(a[i+1]))
{
reverse(a[i],a[i+1])
}
}
k++
}

all functions used ...

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

<pre lang="" line="1" title="CodeMonkey50466" class="run-this">void
sort () {
int i, j, m, n, L;

L = length ()

for (i = 1; i < L; i++) {
m = get (i - 1)
n = get (i)
k = i

while (m > n) {
reverse (k - 1, k)
k = k - 1;
if (k == 0)
break;
m = get (k - 1)
n = get (k)
}
}
}</pre>

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

It implements insertion sort. Forgot to mention that.

- junaid June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use quicksort...

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

how can you implement quick sort in it?
reverse(i, j) does not exchange ith and jth element but reverses entire subrray (which is not what we do in quick sort).

- someone June 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly. reverse(i,j) is not similar to swap(i,j) which runs in O(1) time. So, total efficiency will be definitely greater than O(nlogn) for average case.

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

Bubble sort?

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

Yes!

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

I'm not sure you may implement quick sort. Bublesort is doable.

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

yes! i implemented bubble sort in during the test. I could not think of an O(nlogn) solution for this or better.

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

quick sort is doable too.
swap(i,j) = reverse(i,j), then reverse(i+1,j-1);

- dood June 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same as Question id 7935667

- Same as Question id 7935667 June 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No need to use reverse two times in quick sort. Just use the middle element as the pivot for quick sort. Implementation code

public void quickSort(List<Integer> array) {
int pivot = array.get(array.size() / 2);
int left = 0, right = array.size() - 1;
while (left < right) {
while (array.get(left) < pivot) {
left++;
}

while (array.get(right) > pivot) {
right--;
}

reverse(array, left, right);
/*if (left < right) {
int temp = array.get(left);
array.set(left, array.get(right));
array.set(right, temp);
}*/
}

if (left > 0) {
quickSort(array.subList(0, left));
}

if (right < array.size() - 1) {
quickSort(array.subList(right + 1, array.size()));
}
}

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

<pre lang="" line="1" title="CodeMonkey93594" class="run-this">public void quickSort(List<Integer> array) {
int pivot = array.get(array.size() / 2);
int left = 0, right = array.size() - 1;
while (left < right) {
while (array.get(left) < pivot) {
left++;
}

while (array.get(right) > pivot) {
right--;
}

reverse(array, left, right);
/*if (left < right) {
int temp = array.get(left);
array.set(left, array.get(right));
array.set(right, temp);
}*/
}

if (left > 0) {
quickSort(array.subList(0, left));
}

if (right < array.size() - 1) {
quickSort(array.subList(right + 1, array.size()));
}
}</pre><pre title="CodeMonkey93594" input="yes">
</pre>

- Anonymous June 18, 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