Groupon Interview Question
Senior Software Development EngineersCountry: India
Interview Type: Phone Interview
Here is my program in Java:
/*
* This program is written to print the spiral of a given rectangular matrix in clockwise manner.
* This is done by maintaining two pointer for rows and two pointers for columns starting from top to bottom for rows and left to right for columns.
* The Big-Oh of this program is O(n ^ 2)
*
* @author Prabhash Rathore
* @date Jan 25, 2014
*
*/
public class SpiralMatrix {
private static final char[][] a = {{'1', '2', '3', '4', '5'}, {'6', '8', '9', 'a', 'b'}, {'c', 'd', 'e', 'f', 'g'}, {'h', 'i', 'j', 'k', 'l'}};
public static void printSpriralMatrix(char[][] matrix, int rowLength, int colLength) {
int topRowPointer = 0; // top pointer to row position
int leftColPointer = 0; // left pointer to column position
int bottomRowPointer = rowLength - 1; // bottom pointer to row position
int rightColPointer = colLength - 1; // right pointer to column position
System.out.println("Here is the Spiral Matrix: ");
while((topRowPointer < rowLength/2) && (leftColPointer < colLength/2)) {
// loop variables
int i = 0;
int j = 0;
for(i = topRowPointer, j = leftColPointer; j < rightColPointer; j++)
System.out.println(matrix[i][j]);
for(i = topRowPointer, j = rightColPointer; i < bottomRowPointer; i++)
System.out.println(matrix[i][j]);
for(i = bottomRowPointer, j = rightColPointer; j > leftColPointer; j--)
System.out.println(matrix[i][j]);
for(i = bottomRowPointer, j = leftColPointer; i > topRowPointer; i--)
System.out.println(matrix[i][j]);
topRowPointer++;
leftColPointer++;
bottomRowPointer--;
rightColPointer--;
}
}
public static void main(String[] args) {
int rowSize = a.length;
System.out.println("Row size is: " + rowSize);
int colSize = a[0].length; //assuming given matrix is a rectangular matrix
System.out.println("Column size is: " + colSize);
// print the input matrix
System.out.println("Here are the elements in 2D Matrix:");
for(int i = 0; i < rowSize; i++) {
for(int j = 0; j < colSize; j++)
System.out.println(a[i][j]);
}
printSpriralMatrix(a, rowSize, colSize);
}
}
class SpiralMove2DArray
{
int[,] array;
int[,] traversed;
const int ArrayBound = 4;
enum direction
{
east, south, west, north
}
direction MoveDir;
int xIndex, yIndex;
public SpiralMove2DArray()
{
array = new int[ArrayBound, ArrayBound];
traversed = new int[ArrayBound, ArrayBound];
MoveDir = direction.east;
xIndex = 0;
yIndex = 0;
int k = 0;
for (int i = 0; i < ArrayBound; i++)
{
for (int j = 0; j < ArrayBound; j++)
{
array[i, j] = k;
k++;
}
}
PrintValue();
while (CanTraverse())
{
switch (MoveDir)
{
case direction.south:
if (CanMoveSouth())
{
xIndex += 1;
PrintValue();
}
else
{
MoveDir = direction.west;
}
break;
case direction.east:
if (CanMoveEast())
{
yIndex += 1;
PrintValue();
}
else
{
MoveDir = direction.south;
}
break;
case direction.north:
if (CanMoveNorth())
{
xIndex -= 1;
PrintValue();
}
else
{
MoveDir = direction.east;
}
break;
case direction.west:
if (CanMoveWest())
{
yIndex -= 1;
PrintValue();
}
else
{
MoveDir = direction.north;
}
break;
default:
break;
}
}
}
private void PrintValue()
{
Debug.WriteLine(array[xIndex, yIndex]);
traversed[xIndex, yIndex] = 1;
}
private bool CanTraverse()
{
if (CanMoveEast() || CanMoveNorth() || CanMoveSouth() || CanMoveWest())
{
return true;
}
return false;
}
private bool CanMoveSouth()
{
if ((xIndex == ArrayBound - 1) || (traversed[xIndex + 1, yIndex] == 1))
return false;
return true;
}
private bool CanMoveEast()
{
if ((yIndex == ArrayBound - 1) || (traversed[xIndex, yIndex + 1] == 1))
return false;
return true;
}
private bool CanMoveNorth()
{
if ((xIndex == 0) || (traversed[xIndex - 1, yIndex] == 1))
return false;
return true;
}
private bool CanMoveWest()
{
if ((yIndex == 0) || (traversed[xIndex, yIndex - 1] == 1))
return false;
return true;
}
}
Print up, then right, then down, then left, till up > down or left > right. Following is code in C.
- uuuouou January 13, 2014