March 16, 2016
Problem statement:
Given a 2D array (matrix) named M, print all items of M in a spiral order, clockwise.
For example:
M = 1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
The clockwise spiral print is: 1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12
Julia worked on solution using brute force solution.
https://gist.github.com/jianminchen/aa7a35df305b05f5d90a
Evaluation from the mock interviewer:




























So, Julia worked on more to come out better idea, to avoid bugs, make coding interesting.
i
0 1 2
-------------->
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
The above case, LT = (0,0), LR = (0, 4), BR = (3, 4), BL = (3, 0)
https://gist.github.com/jianminchen/7d775438e2d0d316f77d
Actually, one more condition:
LT, BR, two pointers, make sure that M-1-i>=i, N-1-i>=i; in other words, left top pointer is above the bottom right pointer.
therefore, it should be i <= Math.Min((M-1)/2, (N-1)/2)
Weakness:
1. Jagged array initialization - take more than 5 minutes, look up internet;
2. Design has issue - only variable i, from 0 to (N+1)/2, <- original thought
should be: 0 to Math.Min((M-1)/2, (N-1)/2)
Debug the test case, and then, find the bug; it takes extra 10 minutes, run through several test cases, and then another 10 minutes.
3. Good design - extra checking - save time to debug, fix the bug. Always think about more checking.
Ref: 2015 June - Julia's practice
http://juliachencoding.blogspot.ca/2015/06/leetcode-sprial-array-printout.html