Amazon Interview Question
Software DevelopersCountry: United States
Interview Type: Phone Interview
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
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
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))
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();
}
}
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();
}
}
}
""""
-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])
""""
-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])
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].
Ok mate seems like this is what you are looking for
based on the direction it keeps removing one row or column to get to the central element
- PeyarTheriyaa March 15, 2019