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

- PeyarTheriyaa March 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great

- Rising star March 18, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- V March 12, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Rising star March 13, 2019 | Flag
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

- Rising star March 13, 2019 | Flag Reply
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))

- Ninja March 14, 2019 | Flag Reply
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();
		}
	}

- Sam March 17, 2019 | Flag Reply
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();
			
			
		}
	}

}

- spiroso March 24, 2019 | Flag Reply
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])

- Armax March 28, 2019 | Flag Reply
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])

- armaghanwa March 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We don't need to comment minRow += 1

- SK November 06, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be solved using simple indexing techniques. An N x N matrix will have N//2 (floor operation) square cycles. For example, a 4 X 4 matrix will have 2 cycles. The first cycle is formed by its 1st row, last column, last row and 1st column. The second cycle is formed by 2nd row, second-last column, second-last row and 2nd column. You just need to go the last (innermost) cycle and print the element. For N = odd, the result will arr[N//2][N//2]. For N = even, the result will be arr[N- N//2][N//2].

- Rohan Vardhan June 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is no need to traverse the entire matrix. This problem can be solved using simple indexing.

- Rohan Vardhan June 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Write c program

- Rising star March 09, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- PeyarTheriyaa March 13, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Minh Tran March 17, 2019 | Flag


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