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. 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念

    一.前言     DDD(领域驱动设计)的一些介绍网上资料很多,这里就不继续描述了.自己使用领域驱动设计摸滚打爬也有2年多的时间,出于对知识的总结和分享,也是对自我理解的一个公开检验,介于博客园这个平 ...

  2. 在.NET Core 里使用 BouncyCastle 的DES加密算法

    .NET Core上面的DES等加密算法要等到1.2 才支持,我们可是急需这个算法的支持,文章<使用 JavaScriptService 在.NET Core 里实现DES加密算法>需要用 ...

  3. 【Machine Learning】决策树案例:基于python的商品购买能力预测系统

    决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本 ...

  4. 来自于微信小程序的一封简讯

    9月21晚间,微信向部分公众号发出公众平台-微信应用号(小程序)的内测邀请,向来较为低调的微信在这一晚没人再忽视它了. 来自个人博客:Damonare的个人博客 一夜之间火了的微信应用号你真的知道吗? ...

  5. 著名ERP厂商的SSO单点登录解决方案介绍一

          SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同一个用户 ...

  6. H3 BPM:为石化企业提供一个不一样的全停大修平台

    H3 BPM大型炼化企业装置全停检修管理平台(简称"全停大修")结合国际化的流程管理理念.成熟的系统技术架构.优秀的行业解决方案,为石油化工行业全停大修提供了卓越的信息化管理方案, ...

  7. MySQL 优化之 ICP (index condition pushdown:索引条件下推)

    ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行 ...

  8. Struts2.5需要的最少jar文件

    以Struts2.5.2为例 从官网上下载“struts-2.5.2-min-lib.zip”,里面有7个jar文件: commons-fileupload-1.3.2.jarcommons-io-2 ...

  9. 吸顶大法 -- UWP中的工具栏吸顶的实现方式之一

    如果一个页面中有很长的列表/内容,很多应用都会在用户向下滚动时隐藏页面的头,给用户留出更多的阅读空间,同时提供一个方便的吸顶工具栏,比如淘宝中的店铺页面. 下面是一个比较简单的实现,如果有同学有更好的 ...

  10. Lesson 24 It could be worse

    Text I entered the hotel manager's office and sat down. I had just lost £50 and I felt very upset. ' ...