Amazon Interview Question for Developer Program Engineers


Country: United States
Interview Type: Phone Interview




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

#include <stdio.h>
int main()
{
    int n = 0;
    scanf("%d", &n);

    if (n < 3)
	return 0;
    int x, y;
    bool bStartFromLeft = true;
    int limit = 0;
    for (int i = 0; i < 2*n-1 ; ++i)
    {
	if (i < n)
	{
		limit = i + 1;
		if (bStartFromLeft)
		{
			x = 0;
			y = i - x;
		}
		else
		{ 
			y = 0;
			x = i - y;
		}
	}
	else
	{
		limit = 2*n - i - 1;
		if (!bStartFromLeft)
		{
			x = n - 1;
			y = i - x;
		}
		else
		{
			y = n - 1;
			x = i - y;
		}
	}

	 
	for (int j = 0; j < limit; ++j)
	{
		printf("%d ", y*n + x + 1);
		if (bStartFromLeft)
		{
			--y; 
			++x;
			
		}
		else
		{
			++y; 
			--x;
		}
	}
	bStartFromLeft = !bStartFromLeft;
	
    }
    return 0;
}

- pranaymahajan August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how you figured out that loop needs to be executed for 2*n-1 times??
Can you pls explain the algo?

- jane August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@jane: Let me explain you this with an example:

For n = 5,

Upper Triangle:
------------------------------
0: arr[0][0]
1: arr[0][1] arr[1][0]
2: arr[2][0] arr[1][1] arr[0][2]
3: arr[0][3] arr[1][2] arr[2][1] arr[3][0]
4: arr[4][0] arr[3][1] arr[2][2] arr[1][3] arr[0][4]
Lower Triangle:
------------------------
5: arr[1][4] arr[2][3] arr[3][2] arr[4][1]
6: arr[4][2] arr[3][3] arr[2][4]
7: arr[3][4] arr[4][3]
8:arr[4][4]

so for n = 5, the lop iterates from 0 to 8(ie 0 to 2n-2)

- pranaymahajan August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

/* This just prints the upper triangle.
Similar logic can be applied to
print the lower triangle too */

public void getUpperTri(int n) {
int val = 0;
for(int i=1;i<=n;i++) {
val++;
for(int j=1;j<=i;j++) {
if(i%2==1) {
if(j!=1) {
val -= n-1;
}
} else {
if(j!=1) {
val += n-1;
}
}
System.out.println(val);
if(i%2==0 &&i==j) {
val += n-1;
}
}
}
}

- Shivam Maharshi August 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Waste a lot of char on preparation stuff

#include <iostream.h>
#include <cstdlib>
int main ()
{
int N,c,k,h,m,n,x,j,i=0;
char b[9];
std::cin>>b;
sscanf(b,"%d",&N);
c=k=m=x=1;h=N-1;
n=N;
for(;x<=N*N;h=-h,k+=c)
{
std::cout<<x<<" ";
x+=h>0?m:n;
for(j=0;j<k;j++,x+=h)std::cout<<x<<" ";
if (k==N-1){m=N;n=1;c=-1;}
}
}

- hwer.han August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

given T(n) is n th number in the output sequence:
T(n) = T(n-1) + d(n)
d(n) is anther sequence having following pattern:

0 1 (N-1) N -(N-1) -(N-1) 1 (N-1) (N-1) (N-1) N ...
^0 -(N-1) ^1 (N-1) ^2 -(N-1) ^3 (N-1)
once the number of |N-1| increases to N-1, the number of IN-1| starts to decrease at the next repeat.
Also 1 and N inbetween those (N-1)s has following pattern:
..1..N..1..N..1 (N-1 times of |N-1|) 1...N..1...N...1...
or
..1..N..1..N..1..N (N-1 times of |N-1|) N..1...N..1...N...1...

Reason:
given N=3
We have
1 2 3
4 5 6
7 8 9
if we move right on the top or bottom edge of matrix
1+1==> 2
2+1 ==>3
x+1 ==> the right neighbor of x
similarly
on the left and right edges:
x+N==> the element below x

if we move along diagonals
x+(N-1) ==> move toward bottom-left
x-(N-1) ==> move toward top-right

- hwer.han August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@hwer.han brilliant! really blow my mind...

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

public static void zigzagShow(int input){
int[][] matrix=new int[input][input];
int beginSet=1;
for(int i=0;i!=matrix.length;i++){
for(int j=0;j!=matrix.length;j++){
matrix[i][j]=beginSet++;
}
}

boolean direct=false;
//direct==true, show from top-right to bottom-left
//direct==false, show from bottom-left to top-right
int index_x=0;
int index_y=0;

while(!(index_x==input-1&&index_y==input-1)){
System.out.print(matrix[index_x][index_y]+" ");
if(direct){
try{
matrix[index_x+1][index_y-1]=matrix[index_x+1][index_y-1];
}
catch(Exception e1){
try{
matrix[index_x+1][index_y]=matrix[index_x+1][index_y];
}
catch(Exception e2){
index_y++;
direct=!direct;
continue;
}
index_x++;
direct=!direct;
continue;
}
index_x++;
index_y--;
}
else{
try{
matrix[index_x-1][index_y+1]=matrix[index_x-1][index_y+1];
}
catch(Exception e1){
try{
matrix[index_x][index_y+1]=matrix[index_x][index_y+1];
}
catch(Exception e2){
index_x++;
direct=!direct;
continue;
}
index_y++;
direct=!direct;
continue;
}
index_x--;
index_y++;
}

}
System.out.println(matrix[index_x][index_y]);

}

- Chengyun Zuo August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

/* This just prints the upper triangle.
Similar logic can be applied to
print the lower triangle too */
public void getUpperTri(int n) {
int val = 0;
for(int i=1;i<=n;i++) {
val++;
for(int j=1;j<=i;j++) {
if(i%2==1) {
if(j!=1) {
val -= n-1;
}
} else {
if(j!=1) {
val += n-1;
}
}
System.out.println(val);
if(i%2==0 &&i==j) {
val += n-1;
}
}
}
}

- Shivam Maharshi August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Shortest code ?? since when amazon started asking code golf problems :)

- Cerberuz August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not clear with examples...can you please elaborate?

- victor August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose the input is 3
so it will create a matrix of size 3*3 with values
123
456
789
traverse matrix in this fashion
"en.wikipedia.org/wiki/File:JPEG_ZigZag.svg "

- jane August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] a14486004(int n, int[][] arr) {
		int[] seq = new int[n * n];
		int x = 0;
		int y = 0;
		int indx = 0;
		boolean dir = true;
		int d = 1;
		int d1 = 0;
		for (int i = 0; i < 2 * n - 1; i++) {
			int s = x - y;
			if (s<0){
				s *= -1;
			}
			for (int j = 0; j < s; j++) {
				seq[indx] = arr[y][x];
				if (dir) {
					x++;
					y--;
				} else {
					x--;
					y++;
				}
				indx++;
			}
			seq[indx] = arr[y][x];
			indx++;
			if (s == n - 1) {
				d = 0;
				d1 = 1;
			}
			if (dir) {
				x += d;
				y += d1;
			} else {
				x += d1;
				y += d;
			}
			dir = dir ? false : true;
		}
		return seq;
	}

- godzilaa August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// using C#.
// some functions' implementation are ignored: print, overloading != for class Node.
class Node
{
 public:
   int x;
   int y;
}

void f(int n)
{
   Node a = new Node(0,0);
   Node last = new Node(n-1, n-1);

   while(a!=last)
   {
      Traverse1st(a); //print the first stroke of Z
      Traverse2nd(a); //print the second stroke of Z
      Traverse3rd(a); //print the third stroke of Z
      if(a!=last)
      {
         Traverse4th(a); //print the reverting stroke
      }      
   }   

   print(a);
}

void Traverse1st(Node a)
{
    print(a);
    if(a.x<n-1)
    {
       a.x++;
    }
    else
    {
       a.y++;
    }
}

void Traverse2nd(Node a)
{
    print(a);
    while(a.x>0 && a.y<n-1)
    {
       a.x--;
       a.y++;
       print(a);
    }
}

void Traverse3rd(Node a)
{
    print(a);
    if(a.y<n-1)
    {
      a.y++;
    }
    else
    {
      a.x++;
    }
}

void Traverse4th(Node a)
{
    print(a);
    while(a.x<n-1 && a.y>0)
    {
       a.x++;
       a.y--;
       print(a);
    }
}

- jiangok2006 August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
using namespace std;
int main()
{
int n,flag=0,i,j,k=2;
cout<<"Enter n:(n>=3)";
cin>>n;
while(n<3)
{
cout<<"Enter a valid Value:";
cin>>n;
}
int number = 1;
int loop_count = 4*n-5;
int ac;
cout<<number<<" ";
for(i=1;i<=loop_count;i++)
{
if(i%2==0)
{
if(i>loop_count/2+1)
{
ac = i/2 - k;
k = k+2;
}
else
ac = i/2;
for(j=0;j<ac;j++)
{
if(i%4==0)
number-=(n-1);
else
number+=(n-1);
cout<<number<<" ";
}
}
else
{
if(i == loop_count/2+2 && n%2 == 0)
flag = 0;
else if(i==loop_count/2+2 && n%2 != 0)
flag = 1;

if(flag == 0)
{
number+=1;
flag = 1;
}
else
{
number+=n;
flag = 0;
}
cout<<number<<" ";
}

}

}

- arun August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

awesomecoder.blogspot.com/2012/08/printing-n-x-n-matrix-using-zigzag.html

works fine. Can we use a single dimensional array to simulate a 2 D array to solve problems in interviews ?

- rkt August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i =0, dir = upright, turn = righ
while(i=0;i<2n-2;i++)
switch(dir)
upright:

if(x+1<n && y-1>=0)
print (x,y);
x++;y--;
else
turn==right:doTurn(right)?doTurn(down);
dir = downleft;
turn==right:turn=down?turn=right;

downleft:

if(x-1>=0 && y+1<n)
print (x,y);
x--;y++;
else
dir = upright;
turn==right:doTurn(right)?doTurn(down);
dir = upright;
turn==right:turn=down?turn=right;


doTurn(turn)
if(turn==right)
if(y+1<n)
y++;

if(turn==down)
if(y+1<n)
x++;

- Divesh Dixit August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i did not get the problem can anybody make it clear what is asked

- rocker August 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

to know about zig-zag scanning visit the following link:
stackoverflow.com/questions/3025595/code-golf-zigzag-pattern-scanning

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

Move right when at top or bottom edge. move down when at left edge or right.


following is the code. Order of conditional statements should be reserved.

public void traverse_zigzag(int size)
	{
		traverse_recurse(size-1, 0,0, true);
	}

	private void traverse_recurse(int limit, int i, int j, boolean flag) {
		
		int val = (i*(limit+1))+(j+1);
		System.out.println(val);
		if(val == (limit+1)*(limit+1))
		{
			return;
		}
		
		if(i==0 & flag)
		{
			traverse_recurse(limit, i, j+1, !flag);
		}
		
		else if(i==limit & !flag)
		{
			traverse_recurse(limit, i, j+1, !flag);
		}
		else if(j==0 & !flag)
		{
			traverse_recurse(limit, i+1, j, !flag);
		}
		else if(j==limit & flag)
		{
			traverse_recurse(limit, i+1, j, !flag);
		}
		
		else if(flag)
		{
			traverse_recurse(limit, i-1, j+1, flag);

		}
		else if(!flag)
		{
			traverse_recurse(limit, i+1, j-1, flag);
		}
		
	}

- Adi September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.


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