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
Okay
Got a brute force solution
•Coding:bug-less, clean, readable, reusable and maintainable code
So and So
Completed the code with several bugs
•Communication:clarity of your answers and line of reasoning
Doubted
I mostly didn't understand my peer
•Working Together:peer's motivation to be your colleague
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

Pramp mock interview (4th practice): Matrix Spiral Print的更多相关文章

  1. Pramp - mock interview experience

    Pramp - mock interview experience   February 23, 2016 Read the article today from hackerRank blog on ...

  2. leetcode & Mock Interview

    leetcode & Mock Interview https://leetcode.com/interview/ xgqfrms 2012-2020 www.cnblogs.com 发布文章 ...

  3. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  4. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  5. Eric Chen Mock Interview

    Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...

  6. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  7. (Forward)5 Public Speaking Tips That'll Prepare You for Any Interview

    Landing a job interview is incredibly exciting –- and often terrifying. But fear not. There are clev ...

  8. 使用 Python Mock 类进行单元测试

    数据类型.模型或节点——这些都只是mock对象可承担的角色.但mock在单元测试中扮演一个什么角色呢? 有时,你需要为单元测试的初始设置准备一些“其他”的代码资源.但这些资源兴许会不可用,不稳定,或者 ...

  9. Palindromic Matrix

    Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. JavaScript权威指南 - 函数

    函数本身就是一段JavaScript代码,定义一次但可能被调用任意次.如果函数挂载在一个对象上,作为对象的一个属性,通常这种函数被称作对象的方法.用于初始化一个新创建的对象的函数被称作构造函数. 相对 ...

  2. 使用redis构建可靠分布式锁

    关于分布式锁的概念,具体实现方式,直接参阅下面两个帖子,这里就不多介绍了. 分布式锁的多种实现方式 分布式锁总结 对于分布式锁的几种实现方式的优劣,这里再列举下 1. 数据库实现方式 优点:易理解 缺 ...

  3. 【深入浅出jQuery】源码浅析--整体架构

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  4. 【Win 10 应用开发】启动远程设备上的应用

    这个功能必须在“红石-1”(build 14393)以上的系统版中才能使用,运行在一台设备上的应用,可以通过URI来启动另一台设备上的应用.激活远程应用需要以下前提: 系统必须是build 14393 ...

  5. Android数据加密之MD5加密

    前言: 项目中无论是密码的存储或者说判断文件是否是同一文件,都会用到MD5算法,今天来总结一下MD5加密算法. 什么是MD5加密? MD5英文全称“Message-Digest Algorithm 5 ...

  6. SQL Server 2016白皮书

    随着SQL Server 2016正式版发布日临近,相关主要特性通过以下预览学习: Introducing Microsoft SQL Server 2016 e-bookSQL Server 201 ...

  7. SpringMVC视图解析器

    SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视 图解析器.当我们对SpringMVC控制的资源发起 ...

  8. webform:图片水印、验证码制作

    一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...

  9. [转]ThinkPHP中实例化对象M()和D()的区别,select和find的区别

    1.ThinkPHP中实例化对象M()和D()的区别 在实例化的过程中,经常使用D方法和M方法,这两个方法的区别在于M方法实例化模型无需用户为每个数据表定义模型类,如果D方法没有找到定义的模型类,则会 ...

  10. java中判断字符串是否为只包含数字

    1.用Java自带的函数 public static boolean isNumeric(String str){ for(int i=str.length();--i>=0;){ if (!C ...