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. 谈谈一些有趣的CSS题目(三)-- 层叠顺序与堆栈上下文知多少

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  2. pt-table-checksum

    pt-table-checksum是percona公司提供的一个用于在线比对主从数据一致性的工具. 实现原理 将一张大表分成多个chunk,每次针对一个chunk进行校验,同时将校验的结果通过REPL ...

  3. Angular企业级开发(4)-ngResource和REST介绍

    一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来 ...

  4. [译] C# 5.0 中的 Async 和 Await (整理中...)

    C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...

  5. 使用mybatis-generator在自动生成Model类和Mapper文件

    使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...

  6. CSS 3学习——文本效果和@font-face

    文本效果 关于文本效果,这里仅仅记录得到大多数浏览器支持的几个属性,分别是: text-overflow text-shadow word-break word-wrap text-overflow ...

  7. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  8. HTML+CSS 项目总结

    在过去的大概一个月的学习,基本掌握了HTML+CSS的用法和特性. 这个星期老师给我们布置了一个PC端的实战项目,并且要求在3-4天内完成,我不惜废寝忘食,在紧迫的时间内大致地完成了,但是有些效果不能 ...

  9. Android local.properties 文件读取

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...

  10. 腾讯云上免费部署HTTPS

    接上篇<腾讯云下安装 nodejs + 实现 Nginx 反向代理>,想从头一步到位的同学建议从上篇文章开始阅读.本文将继续介绍如何通过 Nginx 免费部署HTTPS. 留意下,这里的“ ...