Amazon Interview Question for Interns


Country: India
Interview Type: Phone Interview




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

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void input(int *a[3],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void print(int *a[3],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
}
void rotate(int *a[3],int n,int k)
{
int temp;
int nc=n;
for(int i=0;i<n/2;i++)
{

if((nc!=2))
for(int j=i;j<=n-2;j++)
{
temp=a[i][j];
a[i][j]=a[j][n-i-1];
a[j][n-i-1]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[n-j-1][i];
a[n-j-1][i]=temp;
}
if((nc==2))
for(int j=i;j<n/2;j++)
{
temp=a[i][j];
a[i][j]=a[j][n-i-1];
a[j][n-i-1]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[n-j-1][i];
a[n-j-1][i]=temp;
}
nc-=2;

}
print(a,n);
for(int i=2;i<n;i++)
{
int f=0;
if(i==2)
{
for(int j=0;j<n;j++)
{
a[j][i-1]+=k;
}
}
else
{
for(int m=2;i<sqrt(i);m++)
{
if(i%2)
{
f=1;
break;
}
}
if(!f)
{
for(int j=0;j<n;j++)
{
a[j][i-1]+=k;
}
}
}
}
print(a,n);
}
int main()
{
int n;
cin>>n;

int **a=(int **)malloc(sizeof(int *)*n);
for(int i=0;i<n;i++)
{
a[i]=new int[n];
}

input(a,n);
cout<<"\n";
rotate(a,n,2);
getch();
return 0;
}

- pintuguptajuit(PINTU GUPTA) March 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The sample output should be the following matrix, since 3 is also a prime number.

3 11 14
2 10 13
1 9 12

- Sample output is not correct... March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output should be the following matrix :
3 11 14
2 10 13
1 9 12

And here is simple code on C#:

public static void Rotate(int[,] a, int size, int key)
{
    for (int first = 0; first < size / 2; first++)
    {
        int last = size - 1 - first;
        for (int i = first; i < last; i++)
        {
            int offset = i - first;

            int tmp = a[first, i];

            a[first, i] = a[i, last];
            a[i, last] = a[last, last - offset];
            a[last, last - offset] = a[last - offset, first];
            a[last - offset, first] = tmp;
        }
    }

    for (int j = 0; j < size; j++)
    {
	if (IsPrime(j + 1))
	{
		for(int i = 0; i< size;i++)
		{
			a[i,j] += key;
		}
	}
    }
}

private static bool IsPrime(int n)
{
	if(n<2) return false;

	for(int i = 2; i <= (int)Math.Sqrt(n); i++)
	{
		if(n%i==0) return false;
	}

	return true;
}

- ZuZu May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

code in c please

- Anonymous June 07, 2013 | 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