Pramp mock interview (4th practice): Matrix Spiral Print

时间:2023-03-08 20:54:49
Pramp mock interview (4th practice): Matrix Spiral Print

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:

Problem Solving:answering correctly, without much help or hints
Pramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral Print
Okay
Got a brute force solution
•Coding:bug-less, clean, readable, reusable and maintainable code
Pramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral Print
So and So
Completed the code with several bugs
•Communication:clarity of your answers and line of reasoning
Pramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral Print
Doubted
I mostly didn't understand my peer
•Working Together:peer's motivation to be your colleague
Pramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral PrintPramp mock interview (4th practice): Matrix Spiral Print
Maybe
If I have to
•Creativity:original or innovative thinking
- Not Applicable -
•Things you did well:
Asked clarifying questions to understand the problem Broke down the problem to solve

•Things you should work on:
explain the solution before actually start coding

So, Julia worked on more to come out better idea, to avoid bugs, make coding interesting. 
Come out better idea after the mock interview. 
Here is new solution: 
Actually, there is only one variable, which is i, from 0 to (N+1)/2, N is how many columns in the matrix
  So, the circle is a rectangle with points
N - how many columns
M - how many rows
Four corner: LT, LR, BR, BL
    LT  coordinates: (i, i)    
    LR coordinates: (i, N-1-i)  
   BR coordinates:  (M-1 -i, N-1-i)
   BL coordinates:  (M-1-i, i)
To test the correctness, just use i = 0; 
And then, you only need to design a function to iterate through 
private static void leftToRight(Coordinate[] A)
TopToDown, RightToLeft, and DownToUp 

i
            0     1       2
            -------------->

           1   2   3   4   5

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)

Julia, design the algorithm using one variable, and then simplify the algorithm, reduce the time to write and avoid bugs. 
 

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