employer cover photo
employer logo

VMware Interview Question

Given a two dimensional array of characters, provide an implementation which would print out the characters in the array in a spiral manner.

Interview Answers

Anonymous

Oct 20, 2011

public class TwoDimArraySpiral { public static void PrintInSpiral(int[][] num, int length) { int size = length-1; for (int i = 0; i = i; k--){ System.out.println(num[size-i][k]); } for (int j = size - 1 - i; j > i; j--){ System.out.println(num[j][i]); } } } public static void main(String[] args) { int[][] this2DArray = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 }, { 19, 20, 21, 22, 23, 24 }, { 25, 26, 27, 28, 29, 30 }, { 31, 32, 33, 34, 35, 36 }, }; System.out.println(this2DArray.length); PrintInSpiral(this2DArray, this2DArray.length); } }

Anonymous

Jun 14, 2012

void printSpiralArray() { int a[5][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19,20}, {21,22,23,24,25} }; int i = 0, j = 0, k = 0; printf("Original array\n"); for (i = 0; i = 0) { printf("%d ", *(a[i] + k)); k--; } printf("\n"); } else { for (j = 0; j < 5; j++) { printf("%d ", a[i][j]); } printf("\n"); k = j - 1; } } }