Saturday, October 18, 2008

Multi-threading vs. Multi-processing

When to use and when not to use multi-threading and multi-processing? Which is better and when?

*Multi-threading is most beneficial in the context of large applications,where a collection of cooperating programs are each to be executed by a separate thread.

*Another important benefit of multi-threading is when using a multiprocessor system ,where it is possible to exploit parallelism using threads.

Process switching is generally more expensive than thread context switching and creating and destroying threads is much cheaper than process creation and destruction.

What are threads and why use multithreading? Things which we have to be careful with when using threading.

1. concurrency
2. problems:
thread interference: you dont want two threads to interleave while accessing shared variables .. inconsistencies arise
deadlock: don't want to be stuck in a situation where each of the thread is waiting for the other thread to release a lock .. none of the thread can make any progress ..
starvation: don't want a thread to starve for resources being consumed by the other thread .. long and 'greedy' synchronized methods lead to this ..
livelovk: pretty much like deadlock but the threads are not blocked waiting for each other but they are continuously responding to each other requests ... but none of them is able to make any progress ...

MS interview problem(zz)

I was asked how to select M numbers from N existing numbers randomly and fairly (N>M). Example, assume that there are 10 numbers, you need to select 3 numbers from them randomly.

Fibonacci number(zz)

code a function that returns the nth fibnocci number... coded it the most efficient way by using 3 variable... Then the interviewer wrote the recursive form and asked me what are the pros and cons of your(iterative) approach and recursive approach.

Hint:
Definition:

F(n) = 0 (n=0); 1 (n=1); F(n-1)+F(n-2) (n>1)

Cons of recursive approach
1. The OS stack which keep track of function calls may overflow if n is very large

2. In recursive approach if we want to solve F(N) then we need to solve F(N-1) and F(N-2)and in order to solve F(N-1) we have to solve F(N-2) AND F(N-3). So we are solving F(N-2) two times once while solving F(N) and second while solving F(N-1). This is highly inefficient.

请教一道面试题(zz)

发信人: TracyZDX (TracyZDX), 信区: JobHunting
标 题: 请教一道面试题
发信站: BBS 未名空间站 (Fri Oct 17 18:21:15 2008)

一个SORTED ARRAY,例如 A[]={0, -10, 20, 30, 40, 50, -60, 70, 80, 90, 100}(
注意是按绝对值来排序),一个数NUM,求所有相加等于NUM的组合,例如NUM=40,答
案是{0,40},{-10, 50},{-60, 100}。要求O(N)。感觉要是按大小来排序还好办,但
按绝对值来排序我就糊涂了,请高手指教。

算法题目请教(zz)

发信人: skydoor (海阔天空), 信区: JobHunting
标 题: 算法题目请教
发信站: BBS 未名空间站 (Sat Oct 18 14:48:00 2008)

一个polygon,是convex,给一个点,如何确定这个点是否在这个polygon里面?

polygon的顶点有n个,要求算法复杂度是logn

Friday, October 17, 2008

请教算法题目(zz)

发信人: skydoor (海阔天空), 信区: JobHunting
标 题: 请教算法题目
发信站: BBS 未名空间站 (Fri Oct 17 17:46:36 2008)

平面里面n个点,请问,如果找出靠的最近的两个点,要求nlogn.
--

Solution: http://www.cs.mcgill.ca/~cs251/ClosestPair/index.html

Microsoft on-campus interview,以及前几天面试犯的一个错误(zz)

发信人: chhuang (hch), 信区: JobHunting
标 题: Microsoft on-campus interview,以及前几天面试犯的一个错误
发信站: BBS 未名空间站 (Fri Oct 17 10:41:08 2008)

先说MS,刚面完。
半小时,问了2个题:
1. 给一个乱序int数组int[] array,怎么找duplicated elements。
当时我说的是先quick sort,然后过一遍找array[i] = array[i+1]的,然后写代码,
接着被问了为什么用quick sort,时间复杂度,后来想想似乎只要过一遍,然后每个元
素放到hash table里比较就可以了,但当时他没接着问有没有改进方法,我也就没有继
续说下去,这个请教一下大家,有没有好方法。

代码是用Perl写的,没有用C++因为很久不用了有点手生,interviewer似乎不是很满
意,说了他不熟Perl。

2. 然后是估计题,估计美国每天有多少小孩出生,这题以前应该有讨论,可惜没看过
,所以就当场估计了,被提示以后算出来了, 10,000 per day.

最后还剩5分钟,随便打发走了。

总体感觉题不难,但是聊的不投机,在介绍完自己的几个projects以后就能明显感觉到
,他都是盯着我的简历说,都不怎么看我,我感觉一个原因可能是我的项目经验和他的
没有共同点,然后一点可能是写代码的时候不应该用Perl,准备move on了。

-----------------------------------------------------------

再说说前几天面一个Financial firm的IT position时候犯的大错误。当时聊的应该还好,
问了这几个问题:
1. Hash table vs. Heap, 说区别和时间复杂度
2. 设计Web browser的forward 和backward的功能,用什么数据结构,我说用link
list,不知道大家怎么看。
3. 2个简单的概率题,第二题是random walk的,一个醉汉走路,1/3的概率往后走,2/
3的概率往前走。
路径是0=1=2=3, 开始位置是1,走到0,3就停了,问走到3的概率是多少,当时我说把
初始概率矩阵init写出来,转置矩阵matrix写出来,然后用matlab init*matrix^20求
stationary distribution就行了,现在想想这个太偷懒了,应该按stationary
distribution的定义算一遍的,不过当时他也没说什么,就过了。

然后他问我周末有时间吗?他们的安排是请job candidate去onsite,星期五去公司玩
一天,星期六再面试,我当时也不知道哪根神经错了,想起来周五有别的面试,星期6
老板meeting就说我再考虑一下,然后回去回信的时候说上午有一个面试,面完了就没
事了,结果别人就不理我了,刚才收到拒信。。。

后来自己考虑了一下,有2个错误,第一当时应该很热情的说没问题,第二回信的时候
不应该提其他面试的事情。不知道大家怎么看,谢谢。

------------------------------------------------
说了这么多,也希望有人能给我点指点,还有没有可以改进的地方,面试题或着处理面试的方式的都可以,谢谢!