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. 设计爬虫Hawk背后的故事

    本文写于圣诞节北京下午慵懒的午后.本文偏技术向,不过应该大部分人能看懂. 五年之痒 2016年,能记入个人年终总结的事情没几件,其中一个便是开源了Hawk.我花不少时间优化和推广它,得到的评价还算比较 ...

  2. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 1.实施前准备工作 1.1 服务器安装操 ...

  3. ASP.NET Aries 入门开发教程3:开发一个列表页面及操控查询区

    前言: Aries框架毕竟是开发框架,所以重点还是要写代码的,这样开发人员才不会失业,哈. 步骤1:新建html 建一个Html,主要有三步: 1:引入Aries.Loader.js 2:弄一个tab ...

  4. MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信

    MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...

  5. js中参数不对应问题

    因为js是一种弱类型的编程语言,对数据类型的要求没有其他编程语言的要求严格,所以在定义函数的时候不需要像java和C#一样对其传入参数的类型进行定义.那么传入参数的个数有没有影响呢?今天小猪就做了个实 ...

  6. ASP.NET MVC5+EF6+EasyUI 后台管理系统 (源码购买说明)

    系列目录 升级日志 !!!重大版本更新:于2016-12-20日完成了系统的结构重构并合并简化了T4(这是一次重要的更新,不需要修改现有功能的代码),代码总行数比上个版本又少了1/3.更新了代码生成器 ...

  7. 【NLP】十分钟快览自然语言处理学习总结

    十分钟学习自然语言处理概述 作者:白宁超 2016年9月23日00:24:12 摘要:近来自然语言处理行业发展朝气蓬勃,市场应用广泛.笔者学习以来写了不少文章,文章深度层次不一,今天因为某种需要,将文 ...

  8. JavaScript 常量定义

    相信同学们在看见这个标题的时候就一脸懵逼了,什么?JS能常量定义?别逗我好吗?确切的说,JS当中确实没有常量(ES6中好像有了常量定义的关键字),但是深入一下我们可以发现JS很多不为人知的性质,好好利 ...

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

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

  10. 记:MySQL 5.7.3.0 安装 全程截图

    前言: 下一个班快讲MySQL数据库了,正好把服务器里面的MySQL卸了重装了一下. 截个图,作为笔记.也正好留给需要的朋友们. 目录: 下载软件 运行安装程序 安装程序欢迎界面 许可协议 查找更新 ...