Please comment about code complexity, and coding style, I'm preparing for a code interview, so any comment would be appreciated.
Example:
Input
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Code:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace JobInterviewTests
{
//Spiral printing of 2D matrix.
[TestClass]
public class Spiral2Dmatrixprint
{
[TestMethod]
public void TestSpiralPrint()
{
int[,] matrix = new int[3, 3];
matrix[0, 0] = 1; matrix[0, 1] = 2; matrix[0, 2] = 3;
matrix[1, 0] = 8; matrix[1, 1] = 9; matrix[1, 2] = 4;
matrix[2, 0] = 7; matrix[2, 1] = 6; matrix[2, 2] = 5;
SpiralPrint(matrix);
}
private void SpiralPrint(int[,] matrix)
{
int colRight = 2;
int colLeft = 0;
int rowTop = 0;
int rowEnd = 2;
PrintRight(matrix, colLeft, colRight, rowTop, rowEnd);
}
private void PrintRight(int[,] matrix, int colLeft, int colRight, int rowTop, int rowEnd)
{
if (colRight == colLeft && rowTop == rowEnd)
{
System.Diagnostics.Debug.Write(matrix[colRight,rowTop]);
return;
}
for (int i = colLeft; i <= colRight; i++)
{
System.Diagnostics.Debug.Write(matrix[rowTop, i]);
}
PrintDown(matrix, colLeft, colRight, ++rowTop, rowEnd);
}
private void PrintDown(int[,] matrix, int colLeft, int colRight, int rowTop, int rowEnd)
{
if (colRight == colLeft && rowTop == rowEnd)
{
return;
}
for (int i = rowTop; i <= rowEnd; i++)
{
System.Diagnostics.Debug.Write(matrix[i, colRight]);
}
PrintLeft(matrix, colLeft, --colRight, rowTop, rowEnd);
}
private void PrintLeft(int[,] matrix, int colLeft, int colRight, int rowTop, int rowEnd)
{
if (colRight == colLeft && rowTop == rowEnd)
{
return;
}
for (int i = colRight; i >= colLeft; i--)
{
System.Diagnostics.Debug.Write(matrix[rowEnd, i]);
}
PrintUp(matrix, colLeft, colRight, rowTop, --rowEnd);
}
private void PrintUp(int[,] matrix, int colLeft, int colRight, int rowTop, int rowEnd)
{
if (colRight == colLeft && rowTop == rowEnd)
{
return;
}
for (int i = rowEnd; i >= rowTop; i--)
{
System.Diagnostics.Debug.Write(matrix[i, colLeft]);
}
PrintRight(matrix, ++colLeft, colRight, rowTop, rowEnd);
}
}
}