2015年7月18日星期六

block Objective C 用法

首先,block是objective C的object, 可以装在NSArray和NSDictionary里面。

我们可以define一个block like this

void (^simpleBlock)(void);
block is just like a variable, and you can provide a value to it later. block就像一个class的变量
simpleBlock = ^{
        NSLog(@"This is a block");
    };
我们也可以定义像这样,就如同一个局部变量
void (^simpleBlock)(void) = ^{
        NSLog(@"This is a block");
    }

Block也可以带有返回值和参数

  double (^multiplyTwoValues)(double, double) =
                              ^(double firstValue, double secondValue) {
                                  return firstValue * secondValue;
                              };

    double result = multiplyTwoValues(2,4);

    NSLog(@"The result is %f", result);


onsite
F:1.find bad version, 比如isgood(version 1) = true, isgood(version 30) =
false, 找出第一个出错的version
   2.BST inorder tranverse
   3. 把string转化成floating number(stof)
behavior question的最后烙印来了一道按列打印tree,follow up是不用hashmap存
node的水平距离,用vector存,如何做,onepass,不准先求树的width
   4. system design: 每个record有个很大field,比如年龄,性别,爱好等。给一个
field的组合,比如小于25岁,爱好体育,query满足这些组合条件的用户个数

L:
1.max point on line/ (如何不是整数坐标如何处理,需要改写hashmap的compare)
2.special container add/remove/removeRandom at O(1): array + hashmap

3.k-way sort given a stream iterator, vector,
4.product of other elements; 考虑1个0 和2个0 的情况
5.实现movemem( void* src, void* dest)

6.system design: tiny url
7.host manager那轮最后问了一个,如何在不影响功能的情况下,把一个data center
的数据复制到另外一个新的data center去。

G:
1. find all rotation symmetric numbers less than N digits,  16891 -> 16891,
2. give integer, 12345, 返回 32154
    give a target  string and list of strings, find the longest string that
has target as prefix, follow up, stream of target string, 用trie,每个节点保
留最长string信息。
3. integer array add one
    rotation abc->bcd->cde, give a list of strings, group them if them are
rotations.
居然给我laptop,然后直接上面写,然后debug通过,给test case通过

4. given grid of colors, coordinate of a point and its color, find the
perimeter of the region that has the same color of that point.
    print all morse code given the length constraints, short “*” takes one
, long “——“takes two. (find a bug in the code) 就是排列组合的典型题
5. design: chromecast, how to know which app can be supported? There is a
cloud that can give the information to the chrome cast, appID, deviceID,
cache design. 


No offer。发面经供大家参考,5轮
1:(1):写一个bool Palindrome(string s),就是测s是否是Palindrome。
      (2):已知bool Palindrome(string s)方程,写一个 int howmanyPalindrome
(string s), 输入s,返回s中包含多少个Palindrome的单词。 例如abbbac返回10,有a
,b,b,b,a,c,bb, bbb, bb, abbba.

2: 给一个树root的pointer,树包含多个分支,树结构要自己创造。求一条最长[连续]
路径。
例如(括号对应上面node)  [修改:还有条件是 连续]
   树:                     2
                 |            |            |                |
                5            7          3                 6
         (|       | )(   | )   (|)         (|       |)
            6       3         2          4             5       8
                                 |
                                  3

返回3因为 (2-3-4) 这条线。优化要求时间O(n)


3.时间区间合并问题,leetcode上有相似题,关键词interval

4.(1)一个sorted array,如果其中有一个数重复超过array里面总数的1/4 return
true。就是说{2,2,3,4} return true
{1,1,2,3,4,5,6,7} return false。
(2)优化第一部分用O(log2(N)) 时间复杂度
(3)完全平方解集,做一个:int minsol(int i)。
比如1=1^2 所以minsol(1)返回1,
2=1^2+1^2 返回2,
4=2^2或者4个1^2,1比4小, 返回1,
12=2^2+2^2+2^2或者3^2+3个1^2返回3.

5.有一个游戏,他说是fishing game,给一个数组vector Basket, 比如里面元素
是{2,3,5,1,3,4,7}
有A,B 2个player,规定只能从Basket2端选数字,意思就是A开始的话一开始A只能选2
或者7,然后B选,同样只能2端选。所以比如一开始A选了7,B只能从2和4中选。问给定
数组A能取的最大值。B的策略已知,是greedy,就是总会取最大的那个数。
写一个 int maxA(vector& Basket);


 一个BST, 给你一个区间, 返回结点值全部在区间内的子树的结点个数.
比如:
2
1 3
区间 [1,2], 只有子树1,
返回结点个数1 .

2015年7月2日星期四

2015年6月27日星期六

java 使用心得

String.split()
Once you want to plit '.', you have to use it like below:
String.split("\\.")

 PriorityQueue que= new PriorityQueue(len,new Comparator(){
                public int compare(ListNode a,ListNode b){
                if(a.val < b.val)
                    return -1;
                if(a.val > b.val)
                    return 1;
                return 0;
                }
        }); 


How to sort an ArrayList.
Collections.sort(arrays, new Comparator(){
     public int compare(a, b){
     }
})

2015年5月17日星期日

Number Validation

We start with trimming.
  • If we see [0-9] we reset the number flags.
  • We can only see . if we didn't see e or ..  E后面不能有小数点
  • We can only see e if we didn't see e but we did see a number. We reset numberAfterE flag.
  • We can only see + and - in the beginning and after an e
  • any other character break the validation.

2015年4月19日星期日

看leetcode一个题很好的解答 Simplify Path

其实理解题意比较困难,因很多corner case都不清楚
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
题目就这些case,其实没有说清楚是遇到"/../"其实只退一层,遇到"."也不退,只是不显示这一层而已,用stack就可以解决这题
用"/"可以把string 分成很多份,如果最后是空的,就返回"/"
public String simplifyPath(String path) {
        String[] res = path.split("/");
        Stack st = new Stack();
        for(String s:res){
            if(s.equals(".")==true||s.equals("")==true){
                ;
            }
            else if(s.equals("..") == true){
                if(st.empty() == false)
                    st.pop();
            }
            else{
                st.push(s);
            }
        }
        String tes = "";
        while(st.empty() == false){
            tes = "/" + st.pop() + tes;
        }
        if(tes.length() == 0)
            tes = "/";
        return tes;
}

2015年1月26日星期一

转 一个system design的帖子

发信人: flamingos (flamingos), 信区: JobHunting
标  题: 我的System Design总结
发信站: BBS 未名空间站 (Mon Sep  8 02:49:55 2014, 美东)

我的面试也结束了 因为知道FLAG这类公司都会问到System Design的问题 所以这次面
试着重准备了一下 在这里分享给大家 如果有不对或者需要补充的地方 大家可以留言

这里说的System Design和OO Design不同 System Design在FLAG以及很多大公司中主要
是design scalable distributed systems 这里只讨论如何准备这种题目

== 入门 ==
对于0基础的同学们 下面的资料可以按顺序开始看
1. http://www.hiredintech.com/app#system-design
这是一个专门准备面试的网站 你只用关心system design部分 有很多的link后面会重
复提到 建议看完至少一遍

2. https://www.youtube.com/watch?v=-W9F__D3oY4
非常非常好的入门资料 建议看3遍以上!
这是1里面提到的资料 是Harvard web app课的最后一节 讲scalability 里面会讲到很
多基础概念比如Vertical scaling, Horizontal scaling, Caching, Load balancing,
Database replication, Database partitioning 还会提到很多基本思想比如avoid 
single point of failure
再强调一遍 非常好的资料!

3. http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones
1里面提到的 Scalability for Dummies 还算不错 可以看一遍 知道基本思想

结束语:当你结束这一部分的学习的时候 你已经比50%的candidate知道的多了(因为很
多人都不准备 或者不知道怎么准备system design) 恭喜:)

== 进阶 ==
这一部分的资料更加零散 每个看的可能不一样 但是你每多看一篇文章或者一个视频 
你就比别人强一点
这部分你会遇到很多新名词 我的建议是每当你遇到一个不懂的概念时 多google一下 
看看这个概念或者技术是什么意思 优点和缺点各是什么 什么时候用 这些你都知道以
后 你就可以把他运用到面试中 让面试官刮目相看了

4. http://highscalability.com/blog/2009/8/6/an-unorthodox-approach-to-database-design-the-coming-of-the.html
Database Sharding是一个很重要的概念 建议看一看

5. http://highscalability.com/all-time-favorites/
这个里面会讲到很多非常流行的网站架构是如何实现的 比如Twitter, Youtube, 
Pinterest, Google等等 我的建议是看5-6个 然后你应该已经建立起了一些基本的意识
还有知道了某些技术和产品的作用和mapping 比如说到cache你会想到memcached和
Redis 说到
load balancer你会想到 Amazon ELB, F5一类的

6. http://www.infoq.com/
5里面很多的文章都会有链接 其中有很多会指向这个网站 这里面有很多的tech talk 
很不错 可以看看

7. https://www.facebook.com/Engineering/notes
Facebook非常好的技术日志 会讲很多facebook的feature怎么实现的 比如facebook 
message:https://www.facebook.com/notes/facebook-engineering/the-underlying-
technology-of-messages/454991608919 建议看看 尤其是准备面facebook的同学
这有一个facebook talk讲storage的https://www.youtube.com/watch?v=5RfFhMwRAic

8. 一些国内网站上的资料
http://blog.csdn.net/sigh1988/article/details/9790337
http://blog.csdn.net/v_july_v/article/details/6279498

9. 最后一些概念很有用 都是我再看这些资料的时候发现的 如果你没有遇到或者查过 
建议查查
Distributed Hash Table
Eventual Consistency vs Strong Consistency
Read Heavy vs Write Heavy
Consistent Hashing
Sticky Sessions
Structured Data(uses DynamoDB) vs Unstructured Data(uses S3)http://smartdatacollective.com/michelenemschoff/206391/quick-guide-structured-and-unstructured-data http://stackoverflow.com/questions/18678315/amazon-s3-or-dynamodb

10 给有兴趣深入研究的人看的
Mining Massive Datasets --讲很多big data和data mining的东西
Big Data: Principles and best practices of scalable realtime data systems --
twitter的前员工讲述如何处理实时数据

10 凌乱的资料 随便看看吧
http://highscalability.com/blog/2013/10/28/design-decisions-for
== 小结==
看多了以后 你的最终目标应该是心里有了一个大框架 一个基本的distributed system
是怎么搭起来的 然后心里有很多if condition 如果要是满足这个条件 我应该用什么
技术 比如如果read heavy那么用cache会提升performance之类的 同时知道应该避免什
么东西 比如避免single point of failure 再比如时间和空间的tradeoff在read 
heavy的时候应该倾向于时间 Write heavy的时候倾向于空间等等

你总结出来的和我总结出来的大框架和if conditions肯定不完全一样 但因为system 
design本来就是一个open ended question 所以不用害怕 能够自圆其说 就不会有问题

最后 本文纯属抛砖引玉 如果有大牛发现有错误或者有补充 欢迎留言 大家一起讨论