## Amazon Interview Question for Software Developers

Country: United States
Interview Type: Phone Interview

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

Ok mate seems like this is what you are looking for

``````int main(int argc, char const *argv[])
{
int n = 0;

printf("Enter Size N : ");
scanf("%d", &n);

int a[n][n];

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
printf("Enter A[%2d][%2d] : ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

int si = 0, sj = 0, ei = n - 1, ej = n - 1;
int direction = 0;
while (si < ei || sj < ej)
{
switch (direction % 4)
{
case 0: sj++; break;
case 1: ei--; break;
case 2: ej--; break;
case 3: si++; break;
}
direction++;
}

printf("the last element is %d", a[si][sj]);

return 0;
}``````

based on the direction it keeps removing one row or column to get to the central element

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

Great

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

Explain the examples Correctly... don't make any sense

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

Start matrix travese from cell (0,0) and move in anticlockwise by skipping alternate and print last number suppose 1 2 <---3
|. | |
4 5 6
|. |
7-> 8 ->9
Start from 1 skip 4 move to 7 skip 8 move to 9 skip 6 move to 3 skip 2 Finally print only last element that is 5

If 2×2 size matrix then print only last element of first column

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

Start matrix travese from cell (0,0) and move in anticlockwise by skipping alternate and print last number suppose
1. 2 <---3
|. |. |
4 5 6
|. . |
7-> 8 -> 9
Start from 1 skip 4 move to 7 skip 8 move to 9 skip 6 move to 3 skip 2 Finally print only last element that is 5

If 2×2 size matrix then print only last element of first column

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

In this solution, we circle around each level of the matrix, stepping in one level every iteration, until we get to the center. It runs in O(n) (n being the number of items in the matrix) time and takes O(1) space.

``````function findSpiralsCenter(matrix) {
// We are circling around the outter most ring then
// the next outter most, then the next...until we get
// to the center. We use these variables to put boundries
// around the current "ring"/"level" we are working on.
let minRowIndex = 0
let maxRowIndex = matrix.length - 1
let minColIndex = 0
let maxColIndex = matrix[0].length - 1

// for tracking our progress through the matrix
// we start at index [0][0]
const cursor = {
row: 0,
col: 0
}

// move counterclockwise around the matrix
// so left side, then bottom, then right,
// then top, then repeat one level in...
while(true) {
// traverse left
while(cursor.row < maxRowIndex) {
cursor.row += 2
if(cursor.row > maxRowIndex) {
cursor.row = maxRowIndex
if(cursor.col < maxColIndex) {
cursor.col++
}
}
console.log('a', cursor)
}
minColIndex++
if(minColIndex > maxColIndex) break

// traverse bottom
while(cursor.col < maxColIndex) {
cursor.col += 2
if(cursor.col > maxColIndex) {
cursor.col = maxColIndex
if(cursor.row > minRowIndex) {
cursor.row--
}
}
console.log('b', cursor)
}
maxRowIndex--
if(minRowIndex > maxRowIndex) break

// traverse right
while(cursor.row > minRowIndex) {
cursor.row -= 2
if(cursor.row < minRowIndex) {
cursor.row = minRowIndex
if(cursor.col > minColIndex) {
cursor.col--
}
}
console.log('c', cursor)
}
maxColIndex--
if(minColIndex > maxColIndex) break

// traverse top
while(cursor.col > minColIndex) {
cursor.col -= 2
if(cursor.col < minColIndex) {
cursor.col = minColIndex
if(cursor.row < maxRowIndex) {
cursor.row++
}
}
console.log('d', cursor)
}
minRowIndex++
if(minRowIndex > maxRowIndex) break
}

return matrix[cursor.row][cursor.col]
}

const matrixFull =
[[  1,  2,  3,  4],
[  5,  6,  7,  8],
[  9, 10, 11, 12],
[ 13, 14, 15, 16]]
const matrixEmpty = [[]]
const matrixOneItem = [[1]]
const matrixTwoRow = [[3, 5]]
const matrixTwoCol =
[[3],
[5]]
const matrixTwoByTwo =
[[3, 6],
[5, 1]]

// tests
console.log(findSpiralsCenter(matrixFull))
console.log(findSpiralsCenter(matrixEmpty))
console.log(findSpiralsCenter(matrixOneItem))
console.log(findSpiralsCenter(matrixTwoRow))
console.log(findSpiralsCenter(matrixTwoCol))
console.log(findSpiralsCenter(matrixTwoByTwo))``````

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

``````private static void printMatrixAntiClockwiseSkipAlt(int[][] m) {
int start =0;
int size = m.length ;
int r= start, c= start;
int count = 0;
while(start <= (size-1)){

while(r < size){
if(count%2 == 0)
System.out.print("["+r+", "+c +"] ");
r++;
count++;
}

r--;//
c++;
while(c< size){
if(count%2 == 0)
System.out.print("["+r+", "+c +"] ");
c++;
count++;
}

c--;
r--;//do not visit last element again

while(r >= start){
if(count%2 == 0)
System.out.print("["+r+", "+c +"] ");
r--;
count++;
}

r++;
c--;

while(c > start){
if(count%2 == 0)
System.out.print("["+r+", "+c +"] ");
c--;
count++;
}

start ++;
size --;
r =start;
c =start;
System.out.println();
}
}``````

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

Just another solution

``````public class AntiClockWise {

public static void main(String[] args) {
AntiClockWise app = new AntiClockWise();
app.antiClockWise(new int[][] {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12},
{13,14,15,16},
});
}

void antiClockWise(int[][] arr) {
int n = arr.length;
for(int i=0; i<n-1; i++) {
for(int j=i; j<n-i-1; j++)
System.out.print(arr[i][j] +" ");
System.out.println();
for(int j=i; j<n-i-1; j++)
System.out.print(arr[j][n-i-1] +" ");
System.out.println();
for(int j=n-i-1; j>=i+1; j--)
System.out.print(arr[n-i-1][j] +" ");
System.out.println();
for(int j=n-i-1; j>=i+1; j--)
System.out.print(arr[j][i] +" ");
System.out.println();

}
}

}``````

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

``````""""
-Input Array
-Requirement is to loop the Array such such that
1   2---3---4
|   |       |
5   6   7   8
|   |   |   |
9   10--11  12
|           |
13--14--15--16

Python Code

"""

x =[
[1,   2,  3,  4],
[5,   6,  7,  8],
[9,  10, 11, 12],
[13, 14, 15, 16]

]

flag = True

minColumn = 0
maxColumn = len(x) - 1
maxRow = len(x) - 1
minRow = 0
rowCursor = 0
colCursor = 0

while(flag):

#Move down
while(True):
rowCursor += 2
if (rowCursor >= maxRow):
rowCursor = maxRow
#minRow += 1
break

#Move left
while(True):
colCursor += 2
if (colCursor >= maxColumn):
colCursor = maxColumn
minColumn += 1
break

#Move up
while(True):
rowCursor -= 2
if(rowCursor <= minRow):
rowCursor = minRow
maxRow -= 1
minRow += 1
break

#Move right
while(True):
colCursor -= 2
if(colCursor <= minColumn):
colCursor = minColumn
maxColumn -= 1
break

if(minColumn >= maxColumn or minRow >= maxRow ):
flag = False

print(x[rowCursor][colCursor])``````

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

``````""""
-Input Array
-Requirement is to loop the Array such such that
1   2---3---4
|   |       |
5   6   7   8
|   |   |   |
9   10--11  12
|           |
13--14--15--16

Python Code

"""

x =[
[1,   2,  3,  4],
[5,   6,  7,  8],
[9,  10, 11, 12],
[13, 14, 15, 16]

]

flag = True

minColumn = 0
maxColumn = len(x) - 1
maxRow = len(x) - 1
minRow = 0
rowCursor = 0
colCursor = 0

while(flag):

#Move down
while(True):
rowCursor += 2
if (rowCursor >= maxRow):
rowCursor = maxRow
#minRow += 1
break

#Move left
while(True):
colCursor += 2
if (colCursor >= maxColumn):
colCursor = maxColumn
minColumn += 1
break

#Move up
while(True):
rowCursor -= 2
if(rowCursor <= minRow):
rowCursor = minRow
maxRow -= 1
minRow += 1
break

#Move right
while(True):
colCursor -= 2
if(colCursor <= minColumn):
colCursor = minColumn
maxColumn -= 1
break

if(minColumn >= maxColumn or minRow >= maxRow ):
flag = False

print(x[rowCursor][colCursor])``````

Comment hidden because of low score. Click to expand.
-2
of 2 vote

Write c program

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

do you want something like this?

``````-1--2--3--4
|
5--6--7  8
|     |  |
9 10-11 12
|        |
13-14-15-16``````

where the last number is 10?

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

The problem wants anti-clockwise not clockwise ;) From your example, it should be 7

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.

### 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.