Amazon Interview Question for Developer Program Engineers


Team: kindle
Country: -
Interview Type: In-Person




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

find the index (k) of the smallest number within the [0..n) (excluding n) and put the smallest number at the last position
find the index (k) of the smallest number within the range [0..n-1) and put the smallest number at the last position
...
find the index (k) of the smallest number within the range [0..n-i) and put the smallest number at the last position
...
find the index (k) of the smallest number within the range [0..1) and put the smallest number at the last position

Time: O(n^2)

public void sort(int[] a) 
{
     for(int i = 0; i < n; i++) {
          int k = 0;
          for(int j = 1; j < n - i; j++) {
               if(a[j] < a[k]) k = j;
          }
          arrange(a, k);
     }
}

- dgbfs November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The time complexity will be O(n)*timetaken by arrange function
you hv not been given the implementation of it

- Geek December 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi. One possible algo..
Let the array size be n.
start = 0, end = n-1.
The first find smallest no. in [start, end]
give its position to Arrange.
start = 0, end = n-2.
Next find the smallest no. in [start, end] and give its position to Arrange.
Continue till end >= start.
No. of calls to Arrange is n.

- srikantaggarwal November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is good, but you dont need to arrange the smallest number so no of calls will be n-1. You have to start calling arrange with the second smallest number

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

Well... I didnt get it.. If say.. we didnt arrange the smallest no. how would we solve?

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

@mani: calling arrange with second with 2nd largest number will reduce number of calls, but finding 2nd largest number everytime will increase computation cost. what u say ??

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

Yes probably we can skip calling Arrange 1 time :
a. First scan to get smallest and next to smallest element. Say x, y. Now pass position of second smallest to Arrange to arrange it at last.
b. Scan from 0 to n-2 ignoring smallest x to fet third smallest. Pass position of 3rd smallest to Arrange. keep on doing this till only smallest is left to be arrange. It will by default now b on the first position.

- srikantaggarwal November 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,position,temp;
    printf("\nhow many Elements you want ??");
    scanf("%d",&n);
    int *a=(int *)malloc(n*sizeof(int));
    printf("\n");
    printf("\nEnter:");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        printf("\n%d",a[i]);
        printf("\n");
    }
    printf("\nEnter the position you want to operate on :");
    scanf("%d",&position);
    a[n]=a[position-1];
    for(i=position-1;i<n+1;i++)
        a[i]=a[i+1];
    printf("\new array :");
     for(i=0;i<n;i++)
    {
        printf("\n%d",a[i]);
        printf("\n");
    }
    return 0;
}

- Bismoy November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i dint use the arrange thing....anyway now whoever wants can see what i did....;) best of luck guys....

- Bismoy November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// sample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<iostream.h>

class Sort
{
private:
int *arr;
int n;

Sort()
{
arr = NULL;
n=0;
}

public:
Sort(int in_n)
{
arr = new int[in_n];
n = in_n;
}

void Arrange(int pos)
{
cout << " Big Position : "<< pos << endl;

int temp=arr[pos];

for(int i=pos; i<n-1; i++)
{
arr[i] = arr[i+1];
}

arr[n-1] = temp;
}

void sort()
{
int bigpos;
for(int i=n; i>0; i--)
{
bigpos = 0;
for(int j=1; j<i; j++)
{
if(arr[j] < arr[bigpos])
bigpos = j;
}

Arrange(bigpos);
}
}

void getData()
{
cout<<"Enter elements one by one:"<<endl;
for(int i=0; i<n; i++)
cin>>arr[i];
}

void putData()
{
cout << "Elements are : ";
for(int i=0; i<n; i++)
cout<<arr[i]<<",";
cout<<"\b."<<endl;
}

~Sort()
{
if(arr != NULL)
delete arr;
}
};


int main(int argc, char* argv[])
{
int n;

printf("\nhow many Elements you want ??");
cin >> n;

Sort s(n);
s.getData();
cout<<"Before Sorting an Array"<<endl;
s.putData();

s.sort();
cout<<"After Sorting an Array"<<endl;
s.putData();

return 0;
}

- Rahim Ismail November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Problem is description of selection sort

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

just like quicksort.

1. find the pivot, n/2, and call Arrange(n/2).
2. scan the array, if the ith value is greater than the pivot value call Arrange(i)
also keep track of the position of pivot, each time we call Arrange(i)
3. now, values greater than pivot is in the right side of the pivot, and smaller or equal values are in the left.
4. recursively run the two sub-problems.

since quicksort has O(nlogn) average case complexity, number of Arrange() call will be nlogn on average.

- ziaul December 08, 2012 | 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