Tuesday, October 28, 2008

Careercup - Microsoft

Microsoft » Algorithm

Given a graph (any type - Directed acyclic graph or undirected graphs with loops), find a minimal set of vertices which affect all the edges of the graph.

An edge is affected if the edge is either originating or terminating from that vertex.



Microsoft » Algorithm » Software Engineer / Developer

Develop an algorithm for printing different permutations of the string. List out both recursive and non recursive solution.

Monday, October 27, 2008

Careercup - Microsoft

Microsoft » Algorithm Coding » Software Engineer / Developer

Develop an algorithm to find out all valid combinations of n brackets. Like for n =3 possible combinations can be ((())) or ()()() or (()()) and so on.
Read Question »
- vodangkhoa on January 08, 2007 (29) |Edit |Edit | Question #2103 (Report Dup)

void GetCombination(int total, string existResult, int leftUsed, int rightUsed)
{
if (leftUsed == total && rightUsed == total)
{
Console.WriteLine(existResult);
}

if (total - leftUsed > 0)
{
GetCombination(total, existResult + "(", leftUsed + 1, rightUsed);
}

if (total - rightUsed > 0 && leftUsed > rightUsed)
{
GetCombination(total, existResult + ")", leftUsed, rightUsed + 1);
}
}

微软面试题求解

算法题
A为整数数组,长度N,求最大的A[i],使得A[i]=A[x]+A[y].
0 < = i,x,y < N
算法的复杂度是 O(nlog(n)) or O(n^2)

一道google的面试题(zz)

输入a_1, a_2, ..., a_n, b_1, b_2, ..., b_n,如何在O(n)的时间,用O(1)的空间,
将这个序列顺序改为a_1, b_1, ..., a_n, b_n。

微软电话面试(zz)

有整形数组 {1,1,2,3,3,5} ,去掉重复数结果是{1,2,3,5}并返回个数4。

微软面试题(zz)

有一个整数数组,请求出两两之差绝对值最小的值,记住,只要得出最小值即可,不需要求出是哪两个数。

08年千橡的笔试题目(zz)

现在校内网的一千万个用户的名字组成的一个文件,其中每行为一个名字,其中很多的名字是重复多次出现。一个名字重复次数越高,说明最常见,设计一个算法,把最常见的100个名字及重复次数以降序列出。

Access Control: Protected

Key Concept: Class Design and Protected Members
In the absence of inheritance, a class has two kinds of users: members of the class itself and the users of that class. This separation between kinds of users is reflected in the division of the class into private and public access levels. Users may access only the public interface; class members and friends may access both the public and private members.

Under inheritance, there is now a third kind of user of a class: programmers who will define new classes that are derived from the class. The provider of a derived class often (but not always) needs access to the (ordinarily private) base-class implementation. To allow that access while still preventing general access to the implementation, an additional access label, protected, is provided. The data and function members in a protected section of a class remain inaccessible to the general program, yet are accessible to the derived class. Anything placed within a private section of the base class is accessible only to the class itself and its friends. The private members are not accessible to the derived classes.

When designing a class to serve as a base class, the criteria for designating a member as public do not change: It is still the case that interface functions should be public and data generally should not be public. A class designed to be inherited from must decide which parts of the implementation to declare as protected and which should be private. A member should be made private if we wish to prevent subsequently derived classes from having access to that member. A member should be made protected if it provides an operation or data that a derived class will need to use in its implementation. In other words, the interface to the derived type is the combination of both the protected and public members.

Sunday, October 26, 2008

面试问题(zz)

一个数组所有数均为正,找出和最大的子集,要求没有任何两个数是相邻的
例如:
i) 3 2 7 10 结果是 13 ( 3 and 10)

ii) 3 2 5 10 7 结果是 15 (sum of 3, 5 and 7)

Hint: 对数组 a[i], i = 1, 2, 3, ..., N , 结果是 f[N]
f[1] = a[1]
f[2] = max (a[1], a[2])
f[n + 2] = max(f[n + 1], f[n] + a[n + 2])

四道微软面试算法题(zz)

(1)
一个整数数列,元素取值可能是0~65535中的任意一个数,相同数值不会重复出现。0是例外,可以反复出现。
请设计一个算法,当你从该数列中随意选取5个数值,判断这5个数值是否连续相邻。
注意:
- 5个数值允许是乱序的。比如: 8 7 5 0 6
- 0可以通配任意数值。比如:8 7 5 0 6 中的0可以通配成9或者4
- 0可以多次出现。
- 复杂度如果是O(n2)则不得分。
Hint:
取出来的最大的数-最小的数 = 5- 0 的个数;

(2)
设计一个算法,找出二叉树上任意两个结点的最近共同父结点。
复杂度如果是O(n2)则不得分。
Hint: find the levels of the two nodes. Seek the lower one's ancestor A whose level is the same as the higher one B. Then, let both A and B walk toward the root. They will meet at the lowest common ancestor.

(3)
一棵排序二叉树,令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。
复杂度如果是O(n2)则不得分。

(4)
一个整数数列,元素取值可能是1~N(N是一个较大的正整数)中的任意一个数,相同数值不会重复出现。设计一个算法,找出数列中符合条件的数对的个数,满足数对中两数的和等于N+1。
复杂度最好是O(n),如果是O(n2)则不得分。

Endianness

Big-endian:
Most Significant Byte is stored at the lowest address.

Little-endian:
Least Significant Byte is stored at the lowest address

Networks use big-endian, so called network order.

学习数据结构(zz)

一篇以前的算法版的版主,大牛starfish的学习数据结构的文章

发信人: starfish (金盆洗手,退隐江湖), 信区: Algorithm
标 题: 算法与数据结构的一些学习体会
发信站: 南京大学小百合站 (Sat Apr 5 16:04:19 2003)

计算机,顾名思义,是用来计算的机器,它本身并不具有思维能力,将它叫做电脑实在是
言过其实。无论多么复杂的程序,所从事的都是比较、赋值、循环等基本的运算。我们要
编程序解决实际问题,就是要将实际问题的解题步骤用这些基本运算描述出来,这种描述
就是算法。


如何才能将实际问题的解题步骤描述出来呢?这就要建立数学模型。有些问题的数学模型
非常明显,有些问题则比较含蓄。比如说,岗哨设置问题,这个问题可以建立明显的数学
模型,也就是图论模型。有了这个模型,就可以用图论中的相关定理来解决此问题,计算
机所起的作用不过是按照算法计算结果。对于像走迷宫之类的问题,看上去没有明显的数
学模型,只能用回溯法搜索,但实际上这种搜索法本身就是一个数学模型,这个模型叫做
状态空间模型,即在某一个状态空间(状态的集合)内,从某个初始状态开始,通过某种
规则或运算,转移到目标状态。如果状态A可以通过状态转移规则转移到状态B,则从A到B
连一条有向边,这样整个状态空间构成一个网络,只要找到从初始状态到目标状态的路径
即可,于是搜索法又转化为图论模型。也许对有些问题建立严格的数学模型并不能简化问
题的求解,但是可以通过该模型将特殊的问题转化为一般的问题,至少具有理论分析(比
如复杂性分析)上的意义。


要建立数学模型,首先要将问题中的对象以及它们之间的相互关系用数学语言描述,这就
涉及到对象如何表示的问题,也就是采用什么样的抽象数据类型(ADT)来描述对象。所采用
的ADT应该能够方便清晰地表现出对象之间的相互关系(包括显式的和隐式的)。然后在A
DT上定义一些运算(可以暂时不关心运算的实现),将问题的解决步骤转化为ADT的运算的
序列。至于ADT的运算,可以转化为更小的子ADT运算来实现。转化到最后,分解到最小了
,就必须考虑抽象数据类型ADT如何存储在计算机的存储器中,即数据的描述方法。最常见
的数据描述方法有:公式化描述、链接描述、间接寻址和模拟指针。


1。公式化描述借助数学公式来确定元素表中的每个元素分别存储在何处 (存储器地址)。
最简单的情形就是把所有元素依次连续存储在一片连续的存储空间中,这时采用的公式是
location(i)=location(1)+(i-1),其中location(i)表示第i个元素的地址,并假设每个元
素占用一个内存单元。这也就是通常所说的连续线性表,即数组。

2。在链接描述中,元素表中的每个元素可以存储在存储器的不同区域中,每个元素都包含
一个指向下一个元素的指针(即内存中的地址)。

3。在间接寻址方式中,元素表中的每个元素也可以存储在存储器的不同区域中,不同的是
,此时必须保存一张表,该表的第i项指向元素表中的第i个元素,所以这张表是一个用来
存储元素地址的表。

4。模拟指针非常类似于链接描述,区别在于它用整数代替了指针,整数所扮演的角色与指
针所扮演的角色完全相同。如果所有模拟指针分配的内存单元是一片连续的内存地址,则
称其为游标描述或静态链表描述。


所有的基本ADT,比如说图、树、线性表等,最后都要用以上四种描述方法中的一种或几种
描述方法存储在存储器中。至于选择哪一种描述方法,要根据ADT上定义的运算来确定,总
的原则是使运算易于实现并且占用的资源尽量少。对于某些复杂的ADT,比如堆、优先队列
等,则是用基本ADT来实现。因此ADT呈现出一种层次继承关系。


在构造算法的过程中还有一个难点,就是如何将解决问题的步骤用ADT的运算描述。这个过
程没有固定的方法,只能靠经验的积累和一时的灵感。但是一般可以采取以下几个策略:
递归策略,分治策略,贪心策略,动态规划策略,搜索策略,随机策略,模拟策略等。这
些算法设计策略只是一种算法设计思想,并非固定的法则。必须分清楚各种策略的适用条
件和相互间的联系区别,在考虑问题时才能够正确地选择算法策略。


另外,熟悉常用抽象数据类型的各种实现方法和一些常用算法也很大帮助。因为许多问题
可以转化为类似的经典问题,或者可以分解为若干子问题,而这些子问题可以用经典的算
法和抽象数据类型解决。其中,最常用的算法策略应该是搜索策略,几乎所有的问题都可
以用此策略解决(但是未必可行)。这种策略充分利用了计算机计算速度快的特性,在许
多情况下可以在合理的时间内找到问题的最优解。但是,对于有些问题,虽然可以用搜索
法解决,但是时间复杂性无法忍受,所以不可以滥用搜索法,在使用搜索法之前要先估计
时间复杂性。即使使用了搜索法,也要根据实际情况进行优化,通常采用启发式搜索、A算
法、A*算法或界限剪枝法,目的是为了减少搜索的时间复杂性。还有一些看上去只能用搜
索法或穷举法的问题可以考虑用动态规划法和贪心法。值得注意的是,对于有些问题,比
如NP-完全问题,用任何算法都无法在多项式时间内解决,这时可以考虑寻找近似解法,得
到较优解或最优解的上下界,一般是采用贪心算法策略构造近似解法。


以上是我的学习体会,下面说说我对学习算法和数据结构这门课的建议。


对于编程的初学者,可以先通过简单的排序算法了解最简单的ADT线性表的常用操作;然后
要重点掌握递归技术,包括递归和递推的相互转换。递归技术非常重要,可以通过递归技术
了解ADT栈的操作;接着学习搜索法的初步——回溯法,研究经典问题八皇后问题和走迷宫
问题,通过这些经典问题了解深度优先搜索法(DFS)和宽度优先搜索法(BFS)以及ADT栈、A
DT队列的操作,要学会利用人工设置堆栈模拟递归;接着可以学习分治法、贪心法这两种
常用的策略,并应用到排序、搜索等简单的算法中;这时再开始学习图和树这两种抽象数
据类型就应该没有什么难度了。在学习ADT图和ADT树时,要注意结合离散数学中的图论理
论知识和搜索法中的DFS,BFS方法,要学会将实际问题转化为图论模型;再下去可以学习
各种搜索法的优化算法,启发式搜索、A算法、A*算法或界限剪枝法等;然后是网络流算法
,要注意模型的建立;最后学习最优化问题的解法,包括线性规划、动态规划、非线性规
划等算法策略,这部分内容主要侧重模型的建立和分析,算法本身并没有难度。这样基本
的算法就学习完了。再深入一点可以学习问题的计算复杂性,计算模型,并行算法,神经
网络以及各个领域中的具体算法。

还有http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=1854

或许这里的给出的内容对于您还说需要一定的时间领悟和学习,这里也提供了一些代码.请注意,想成为程序设计的高手,虽然天赋很重要,但是勤奋和汗水是不可少的.

推荐几个比较好的算法和数据结构学习的网站:
http://algorithm.lzu.edu.cn/
http://algorithm.myrice.com/
http://www.cpascal.com/
http://gz6hs.net/lzoi/index.html
http://www.itisonline.org/
http://218.5.5.86/drs/
http://202.109.195.141/chenyan/noi/noi.htm
http://www.periodicals.com.cn/qikan/index.j-sp
http://218.5.5.197/~drs/
http://www.bgy.gd.cn/gdoi/oszl/index.htm
http://noi.stedu.net/index0.asp
http://vip.6to23.com/dcyu/ //dcyu的主页 :)
http://www.cfcs.com.cn/fjas/index.htm //信息学的好站点,有很多学习资源
http://www.cfcs.com.cn/fjas/index.htm //福建信息学奥赛站点,内容还不多:)

Saturday, October 25, 2008

传奇编程高手(zz)

传奇编程高手:
Bill Joy
John Carmack
David Cutler
Donald E. Knuth
Ken Thompson
Rob Pike
Dennis M. Ritchie
Edsger Wybe Dijkstra
Anders Hejlsberg

Bill Joy
Bill Joy, 前任Sun的首席科学家,当年在Berkeley时主持开发了最早版本的BSD。他还是vi和csh的作者。当然,Csh Programming Considered Harmful 是另一个话题乐。据说他想看看自己能不能写个操作系统,就在三天里写了个自己的Unix, 也就是BSD的前身。当然是传说了,但足见他的功力。另一个传说是,1980年初的时候,DARPA让BBN在Berkley Unix里加上BBN开发的TCP/IP代码。但当时还是研究生的B伯伯怒了,拒绝把BBNTCP/IP加入BSD,因为他觉得BBN的TCP/IP写得不好。于是B伯伯出手了,端的是一箭封喉,很快就写出了高性能的伯克利版TCP/IP。当时BBN和DARPA签了巨额合同开发TCP/IPStack,谁知他们的代码还不如一个研究生的好。于是他们开会。只见当时B伯伯穿个T-shirt出现在会议室(当时穿T-shirt不象现在,还是相当散漫的哈)。只见BBN问:你怎么写出来的?而B伯伯答:简单,你读协议,然后编程就行了。最令偶晕倒的是,B伯伯硕士毕业后决定到工业界发展,于是就到了当时只有一间办公室的Sun, 然后他就把Sparc设计出来乐。。。象这种软硬通吃的牛人,想不佩服都不行的说。据Bill Joy的同事说,一般开会的时候B伯伯总是拿一堆杂志漫不经心地读。但往往在关键之处,B伯伯发言,直切要害,提出漂亮的构想,让同事们彻底崩溃。对了,他还是Java Spec和JINI的主要作者之一。

John Carmack
John Carmack,id Software的founder和Lead Programmer。上个月和一个搞图形的师兄聊天,他竟然不知道John Carmack, 也让偶大大地晕了一把。不过也许搞研究的和搞实战的多少有些隔吧。想必喜欢第一人称射击游戏的都知道J哥哥。90年代初只要能在PC上搞个小动画都能让人惊叹一番的时候,J哥哥就推出了石破天惊的Castle Wolfstein, 然后再接再励,doom, doomII, Quake...每次都把3-D技术推到极致。J哥哥的简历上说自己的专长是"Exhaust 3-D technology",真是牛人之言不我欺的说。做J哥哥这样的人是很幸福的,因为各大图形卡厂家一有了新产品就要向他“进贡” ,不然如果他的游戏不支持哪种卡,哪种卡基本就会夭折乐。当初MS的Direct3D也得听取他的意见,修改了不少API。当然,J哥哥在结婚前十数年如一日地每天编程14小时以上,也是偶们凡人望尘莫及的。对了,J哥哥高中肆业(?),可以说是自学成才。不过呢,谁要用这个例子来为自己学习不好辩护,就大错特错了。那 Leonardo Da Vinci还是自学成才呢(人是私生子,不能上学)。普通人和天才还是有区别的。对了,其实偶们叫“达分奇”是相当不对的,因为Vinci是地名,而Da Vinci就是从Vinci来的人的意思。换句话说,Leonardo Da Vinci就是“从Vinci来的Leonardo”的意思。叫别人“Da Vinci”就不知所谓乐。嗯,扯远了,打住。 More about Carmack Here

David Cutler
David Cutler,VMS和Windows NT的首席设计师,去微软前号称硅谷最牛的kernel开发员。当初他和他的手下在微软一周内把一个具备基本功能的bootable kernel写出来,然后说:“who can't write an OS in a week?",也是牛气冲天的说。顺便说一句,D爷爷到NT3.5时,管理1500名开发员,自己还兼做设计和编程,不改coder本色啊。D爷爷天生脾气火爆,和人争论时喜欢双手猛击桌子以壮声势。:-) 日常交谈F-word不离口。他面试秘书时必问:"what do you think of the word '****'?" ,让无数美女刹羽而归。终于有一天,一个同样火爆的女面对这个问题脱口而出:"That's my favorite word"。于是她被录取乐,为D爷爷工作到NT3.5发布。

Donald E. Knuth
Don Knuth(Donald E. Knuth)。高爷爷其实用不着偶多说。学编程的不知道他就好像学物理的不知道牛顿,学数学的不知道欧拉,学音乐的不知道莫扎特,学Delphi的不知到 Anders Hejlsberg,或者学Linux不知道Linus Torvalds一样,不可原谅啊。:-)为了让文章完整,就再罗唆几句吧。高爷爷本科时就开始给行行色色的公司写各种稀奇古怪的编译器挣外快了。他卖给别人时收一两千美元,那些公司拿了code,加工一下卖出去就是上万上十万。不过也没见高爷爷不爽过,学者本色的说。想想那可是60年代初啊,高爷爷写编译器写多了,顺带就搞出了个Attribute Grammar和LR(k),大大地造福后人啊。至于高爷爷在CalTech的编程比赛(有Alan Kay得众多高高手参加)总是第一,写的Tex到86年就code freeze,还附带2^n美分奖励等等都是耳熟能详,偶就不饶舌乐。顺便说一下,高老大爷是无可争议的写作高手。他给Concrete Mathematics 写的前言可谓字字铿锵,堪为前言的典范。他的技术文章也是一绝,文风细致,解释精当,而且没有学究气,不失轻快跳脱。记得几年前读Concrete Mathematics,时不时开怀大笑,让老妈极其郁闷,觉得我nerdy到家,不可救药。其实呢,子非鱼,安知鱼之乐,更不知那完全是高爷爷的功劳。说到写作高手,不能不提Stephen A. Cook。他的文章当年就被我们的写作老师极力推荐,号称典雅文风的样本。库爷爷一头银发,身材颀长,总是面带谦和的微笑,颇有仙风道骨,正好和他的仙文相配的说。高爷爷其实还是开源运动的先驱。虽然他没有象Richard Stallman那样八方奔走,但他捐献了好多作品,都可以在网上看到,比如著名的Mathematical Writing,MMIXWare,The Tex Book等,更不用说足以让他流芳百世的Tex乐。

Ken Thompson
Ken Thompson,C语言前身B语言的作者,Unix的发明人之一(另一个是Dennis M. Riche老大,被尊为DMR),Belle(一个厉害的国际象棋程序)的作者之一, 操作系统Plan 9的主要作者(另一个是大牛人Rob Pike, 前不久被google挖走了)。Ken爷爷也算是计算机历史上开天辟地的人物了。1969年还是计算机史前时代,普通人都认为只有大型机才能运行通用的操作系统,小型机只有高山仰止的份儿。至于用高级语言来写操作系统,更是笑谈。Ken爷爷自然不是池中物,于是他和DMR怒了,在1969年到1970间用汇编在PDP-7上写出了UNIX的第一个版本。他们并不知道,一场轰轰烈烈的UNIX传奇由此拉开了序幕。Ken爷爷在1971年又把Unix用C重写,于是C在随后20年成就了不知多少豪杰的梦想和光荣。Ken爷爷还有段佳话:装了UNIX的PDP-11最早被安装在Bell Lab里供大家日常使用。很快大家就发现Ken爷爷总能进入他们的帐户,获得最高权限。Bell Lab里的科学家都心比天高,当然被搞得郁闷无比。于是有高手怒了,跳出来分析了UNIX代码,找到后门,修改代码,然后重新编译了整个UNIX。就在大家都以为“这个世界清净了”的时候,他们发现Ken爷爷还是轻而易举地拿到他们的帐户权限,百思不解后,只好继续郁闷。谁知道这一郁闷,就郁闷了14年,直到Ken爷爷道出个中缘由。原来,代码里的确有后门,但后门不在Unix代码里,而在编译Unix代码的C编译器里。每次C编译器编译UNIX的代码,就自动生成后门代码。而整个Bell Lab的人,都是用Ken爷爷的C编译器。

Rob Pike
Rob Pike, AT&T Bell Lab前Member of Technical Staff ,现在google研究操作系统。罗伯伯是Unix的先驱,是贝尔实验室最早和Ken Thompson以及Dennis M. Ritche开发Unix的猛人,UTF-8的设计人。他还在美国名嘴David Letterman的晚间节目上露了一小脸,一脸憨厚地帮一胖子吹牛搞怪。让偶佩服不已的是,罗伯伯还是1980年奥运会射箭的银牌得主。他也是个颇为厉害的业余天文学家,设计的珈玛射线望远镜差点被NASA用在航天飞机上。他还是两本经典,The Unix Programming Environment 和 The Practice of Programming 的作者之一。如果初学者想在编程方面精益求精,实在该好好读读这两本书。它们都有中文版的说。罗伯伯还写出了Unix下第一个基于位图的窗口系统,并且是著名的blit 终端的作者。当然了,罗伯伯还是号称锐意革新的操作系统,Plan9,的主要作者。可惜的是,Plan9并没有引起多少人的注意。罗伯伯一怒之下,写出了振聋发聩的雄文 Systems Software Research is Irrelevant,痛斥当下系统开发不思进取,固步自封的弊病。虽然这篇文章是罗伯伯含忿出手,颇有偏激之词,但确实道出了系统开发的无奈:开发周期越来越长,代价越来越大,用户被统一到少数几个系统上,结果越来越多的活动是测量和修补,而真正的革新越来越少。就在罗伯伯郁闷之极的时候,google 登门求贤来乐。如果说现在还有一家大众公司在不遗余力地把系统开发推向极致的话,也就是google 乐。随便看看google的成果就知道了。具有超强容错和负载平衡能力的分布式文件系统GFS (现在能够用100,000台廉价PC搭起一个巨型分布系统,并且高效便宜地进行管理的系统也不多哈),大规模机器学习系统(拼写检查,广告匹配,拼音搜寻。。。哪个都很牛的说),更不用说处理海量并行计算的各式google服务了。Rob在System Software Research is Irrelevant里萧瑟地说现在没有人再关心系统研究的前沿成果了。想不到他错了,因为google关心。google网络了大批功成名就的牛人,还有大量初生牛犊般博士做开发,显然不是没事耍酷,而是因为它们的开发总是试图吸取系统研究的最新成果。 想必Rob Pike在google很幸福。愿他做出更棒的系统。

Dennis M. Ritchie
Dennis M. Ritchie 既然Ken Thompson是我的偶像,新闻组上人称DMR的Dennis M. Ritchie自然也是,毕竟两人共同缔造了UNIX,而Dennis几乎独力把C搞大(当然,C的前身是B,而B是Ken Thompson一手做出来的)。J 两人1983年分享图灵奖,是有史以来少数几个因工程项目得奖的工程师(本来是唯一的一对儿,但Alan Kay才因为SmallTalk得奖,所以就成了唯二的乐) 。一个人一生能做出一个卓越的系统已经不易,DMR的C和UNIX长盛不衰近30年,至今生机勃勃,DMR此生可以无憾的说。D爷爷也算有家学渊源:他老爸在AT&T贝尔实验室工作了一辈子,并在电路设计方面卓有成就,还出了本颇有影响的书The Design of Switching Circuits,据说在交换理论和逻辑设计方面有独到的论述。当然,D爷爷和他老爸是不同时代的人:他老爸的研究成形于晶体管发明之前,而D爷爷的工作离了晶体管就玩儿不转乐。:-D不要看D爷爷搞出了C,其实他最爱的编程语言是Alef,在Plan9上运行,支持并行编程。Alef的语法和C相似,但数据类型和执行方式都和C大大不同。说到语言,D爷爷对后来人有非常中肯的建议:抱着学习的目的来开发你自己的语言,不要冀望于它被众人接受。这个建议不光对语言开发有用,也适用于其它大型系统的开发。别的不说,DMR后来领导自己的团队在1995年和1996分别推出了Plan9和 Inferno操作系统,又用多少人知道呢?其实,D爷爷当初也没想过C会风行世界。他开发C的初衷和 Eric S. Raymond在Cathedral and Bazaar里阐述的一样,就是要消除自己对现有工具的不爽之处。谁知D爷爷无心插柳,C竟然受到众多程序员的狂热拥戴,连D爷爷自己都大惑不解。在一次采访中D爷爷说大概那是因为C的抽象程度碰巧既满足了程序员的要求, 又容易实现。当然C一度是Unix上的通用语言也是原因。但不管怎么说,D爷爷对编程语言出色的审美意识奠定了C广为流传的基础。最后八卦一下。D爷爷的业余爱好和NBA大牛Karl Malone一样:开卡车。不过D爷爷更喜欢开NASCAR,而KM独爱巨无霸。J D爷爷自称心中不供偶像,如果一定要说一个,那就是Ken Thompson了。现在Ken爷爷退休当飞机教练去了,而D爷爷当了贝尔实验室系统开发部的头,整日忙于开支票。他俩合作20年,屡屡创造历史。这段令人神往的佳话,也就长留你我心中乐。P.S., 很多人都以为Brian W. Kernighan是C的作者。其实BWK只是写了那本经典K&R C。据D爷爷说,他,Ken, 和Kernighan三人中,Kernighan最能写文章,他次之,而Ken写得最少;但说到编程,Ken爷爷才是当之无愧的老大。

Edsger Wybe Dijkstra
Edsger Wybe Dijkstra 对,就是E.W. Dijkstra. 一提到EWD,很多人就会想起找最短路径的Dijkstra Algorithm,就好像一提到Sir. Tony Hoare,就想起Quick Sort一样。其实这些个算法不过是两个牛人在他们职业生涯中最琐碎的贡献。比如Dijkstra算法,无非是戴爷爷在1956年为了展示新计算机 ARMAC的计算能力,初试身手的成果,属于他的算法处女作。据戴爷爷自述,他搞出最短路径算法的时候连纸笔都没用。当时他和他老婆在阿姆斯特丹一家咖啡厅的阳台上晒太阳喝咖啡,突然就把这个算法想出来乐。而且当时的算法研究还比较原始,牛人们忙着用计算机搞数值计算,对离散算法不屑一顾。那时连一个象样的专注于离散算法的专业期刊都没有。戴爷爷于是推迟发表这个算法。直到1959年,他才把这个算法发表在Numerische Mathematik的创刊号上,权为捧场。:-) EWD在多个领域牛气冲天,端的是理论和编程两手硬的高手。只不过他的很多工作比较深刻,学校的老先生们觉得本科生接受不了,不给本科生讲而已。戴爷爷大概因为最短路径算法一战成名,于是有人请他参加另一台计算机X1的设计工作,并且把设计实时中断系统的任务派给了他。现在看来实时中断也许不算什么,但要知到,X1前根本就没有实时中断的概念。实现它简直就是一场豪赌。戴爷爷起初还不情愿,但经不住项目负责人Bram和Carel的轮番“吹捧”:我们知道实时中断让您工作变得非常困难,但象您这样的牛人肯定能做出来的说。结果戴爷爷被糖衣炮弹彻底击穿,接下了这个烫手山芋。两三年后,他不仅搞出了实时中断,还围绕这个写出了自己的博士论文,顺利戴上博士帽。让戴爷爷真正成名立万的还是在X1上开发的Algo60,最早的高级语言之一。戴爷爷没日没夜地工作了8个月,就搞出了Algo60,也因此获得了1972年的图灵奖。因为Algo60,戴爷爷发表了一篇石破天惊的文章:Recursive Programming,于是人们才知道,原来高级语言也可以高效地实现递归,原来从此以后,所有程序员都不可避免地和戴爷爷发明的一个词(应该说是概念)打交道:堆栈。而且Algo60还让戴爷爷深入地思考多道程序设计的问题,最终发明了每个系统程序员都绕不开的概念:semaphore。当然,戴爷爷总是把他发明的概念严格形式化,极具科学家本色的说。和这些成就想比,他提出的吃饭的哲学家问题,也就没什么好说的了。说来好笑,当时的大学(忘了哪所了)还是觉得戴爷爷没有受过正统的数学训练,也不是专门搞数值分析的,所以最后不太情愿地给了他一个教职。这种小挫折并不能妨碍象戴爷爷这样的牛人创造历史。他一边教数值分析(:-D) ,一边开始开发一个

问2个微软电话面试题目(zz)

发信人: wmbyhh (wmbyhh), 信区: Programming
标 题: 问2个微软电话面试题目
发信站: BBS 未名空间站 (Thu Jul 17 15:39:03 2008)

1、Suppose there is an interger array of size N, the value of each
element ranges from 1 to N-1, so there are at least two elements have
the same value. Find the fastest algorithm to detect the first
repeated element.
2、 int (*a) [10];
a++;
What is the value of a after this?
a是个指向地址的指针?还是a就是个地址?

Binary Search on 2D Array

How do you apply Binary Search on 2D array supposed you have 2D array with integers sorted both horizontally and vertically. If you find any occurrence of the value you are looking for you return true else false. What is the complexity?

For example the 2D array could look like the following

1 4 5 6
2 5 7 9

Friday, October 24, 2008

问2个微软电话面试题目(zz)

发信人: barry (大狗), 信区: Programming
标 题: [合集] 问2个微软电话面试题目
发信站: BBS 未名空间站 (Fri Oct 24 12:04:54 2008), 站内

☆─────────────────────────────────────☆
ilvch (From here to eternity) 于 (Wed Jul 16 03:02:30 2008) 提到:

在一个长字符串里,寻找包含所有N个关键词的最短子串(substring).

每个关键词在字符串里的出现位置已经存放在一个sorted integer array里.

比如N个关键词 W_1, W_2, ..., W_n, 它们出现的位置在 n个 int array里:

A_1: 1 3 7 25 78
A_2: 2 56 57 64
...
A_n: 32 35 72 91

(没有重复的整数)

求Imin 和 Imax, n个 array 每个至少在 [Imin, Imax] 区间有一个成员.

关于求解链表中环的起始位置问题(zz)

发信人: barry (大狗), 信区: Programming
标 题: [合集] 关于求解链表中环的起始位置问题
发信站: BBS 未名空间站 (Fri Oct 24 12:04:11 2008), 站内

一个链表中有环,如何求解这个环的起始位置。
网上已经有人给出答案,就是用2个指针,p=p->next, q=q->next->next,找到它们相
遇的位置,再求解这个环长度Y,从起点head到这个相遇位置长度L,
然后另2个指针,1个从head出发,1个从距离相遇位置Y-L%Y位置出发,直到这2个指针
相遇,此时就是环在链表中的起始地址。
现在我还是不懂得,这个L%Y是如何来的。
请指点一下。

怎么感觉大家说得那么复杂。
1, 先跳1跳2找到环中的某个节点
2,两指针指向环中的节点,一个不动,一个每次跳1,记录跳的次数,直到两个再次指
向同一个节点,跳的次数就是环的长度K
3,两指针指向头,一个移到距头的第K个节点,然后两个指针每次各跳1,直到两指针
指向同一个节点,这个节点就是环的头/尾

Thursday, October 23, 2008

Detect a Cycle in a linked list and Fix the cycle

http://tekpool.wordpress.com/2006/09/29/linked-list-detect-a-cycle-in-a-linked-list-and-fix-the-cycle/

Sorting Linked List

Merge sort (best)
Insertion sort (middle)
Selection sort (middle)
Bubble sort (worst)

A list of sorting algorithms: http://en.wikipedia.org/wiki/Sorting_algorithm#Selection_sort

summary of search/sort (zz)

Based upon what points we should choose proper search/sort algorithm for problem solution from all available algos (Quick, heap, selection, shell, insertion, Binary Tree, bubble sort etc.) ? To be specific, when should we use above algos ? examples will be appreciated.

http://www.careercup.com/question?id=57018

careercup algorithm (zz)

Design an algorithm to find the kth number divisible by only 3 or 5 or 7 i.e the only factors of these numbers should be 3,5 and 7.

http://careercup.com/question?id=57139

Monday, October 20, 2008

问一道面试题(判断是不是二叉树)(zz)

发信人: sheart (test), 信区: JobHunting
标 题: 问一道面试题(判断是不是二叉树)
发信站: BBS 未名空间站 (Mon Oct 20 17:30:28 2008)

一个类似二叉树的结构,但是有可能有环,比如:

a
/\
b c
/\ |
d e |
| |
|_|

(图排版是乱的。。就是a有两个子结点b和c,b有两个子结点d和e,但同时e有一个子结点是c)
当然这种情况下就不是二叉树了。
现在给出根结点,让判断是不是二叉树。

我说的是遍历这棵“树”,把访问到的结点加到一个list里面,每新加入一个都和现有
的所有结点比较,相同就输出false。有更快的算法吗?

Sunday, October 19, 2008

算法题, 排序(zz)

发信人: bbatucd (bb), 信区: Programming
标 题: 算法题, 排序(queue)
发信站: BBS 未名空间站 (Fri Oct 17 19:49:46 2008), 转信

given a unsorted queue, sort it via using another queue and some variable.

what if use stack instead of queue?

Saturday, October 18, 2008

Mark progress

Careercup: July 22, 2008 http://www.careercup.com/page?id=1331&n=3

Free vs. Realloc

free(): release the memory allocated to the object by malloc(),realloc() or calloc(), making it avaible for future allocation.
realloc(void* ptr,size_t size): expand or reduce the size of the allocated block of memory to size. when size=0, it is equal to free. when ptr is NULL, it works the same as malloc. when the size is larger than the old block, then newly allocated portion are unknown.

Declaration vs Definition

This is a topic that has created a lot of confusion in internet forums. Let me put it clear here. Reply me, if you don't understand after reading this.

Declaration and definition have different meanings in different contexts. Generally, a declaration describes to the compiler to bind an identifier with a specific data type. Compiler does NOT allocate storage when it sees a declaration.
A definition tells the compiler to allocate storage for the declared data type.

Function Declaration vs Definition
----------------------------------
Declaration of a function means telling to the compiler about the function's name, arguments and return type. This is done by the prototype of the function.
Eg:

int func(int x, float y) ();

Definition of a function means telling to the compiler about the details of a function and what it should do when the function is called. This is when, compiler allocates storage.

Eg:

int func(int x, float y) {
char response;
int value;
....
....
return value;
}

Variable Declaration vs Definition
----------------------------------
For automatic, static & register storage class variables, declaration and definition are the same. There is no difference between both terms. By that, I mean, the binding of identifier with data type and allocation of storage will happen at the same time.

But, when it comes to an extern variable, their meaning differ. For example, when you write
extern int i; /* extern declaration of i */
it WONT allocate storage. It tells to the compiler that a variable called "i" of integer type has already been defined (binding int data type with variable "i" and allocating space for it) in some other file. So, connect me to that memory. This is basically a linking process and is taken care by the linker.
Thus definition and declaration hold different meaning, when it comes to extern storage class variables.

Reverse Binary Tree

Node[] Reverse(Node root)

so for the tree below, out put shd be Array with 4 roots L1,L2,L3,L4

Input Tree:

R
C1 C2
L1 L2 L3 L4

Output tree: (Its No more binary tree, looks like a 4 linked list where L1,L2 share common Node C1 and R, and L3 L4 has common node C2 and R)

L1 L2 L3 L4
C1 C2
R

Problems from thecareerplus

http://www.thecareerplus.com/?page=resources&cat=150

MS interview problem(zz)

Hi Guys,
I went to the Microsoft written test held in EGL, Bangalore today, and the questions that were asked are as follows:

The pattern of the test was they had given 4 questions and we had to answer within 90 min.

So, the questions are:

1. You are given a TreeNode, and you have to write an algorithm which will return the no. of nodes in the tree.

2. Given an array of integers, {10203004567000}, you have to create a new array which will be like (12345670000000}, without using any other temporary array.

3. (A) Given two Link List on integers which are sorted, you have to merge those two link lists and create a new LinkList which is also sorted.

3. (B) Give few example link lists to test the above implemented Link Lists.

Reverse link

Reverse a linked list iteratively, do it first with single pointers and then do it again with double pointers. Now do it again recursively but not tail-recursive, and then do it again tail-recursively. What do you do if it has a loop?

MS interview problem(zz)

1) Write a function which determines wheather in given array there is a sequence of n or more elements, which have the same value. Example:
n = 3, input[] = { 1,1,1,2,3,4,5 } gives true;
n = 2, input[] = { 5,1,2,2,2,4,5 } gives true;
n = 3, input[] = { 5,2,2,3,2,5,5 } gives false;

2) Write a function which determines, wheather the array is a mirror - reflection - like. Example:

input[] = {1,2,3,2,1} gives true
input[] = {1,2,3,3,2,1} gives true.

Const vs. #define

Advantages of const over #define. When to use which of them?

hints:
const means the data can't be changed while define has no such meaning.
const is used by compiler and define is performed by pre-processor.
const has scope while define can be considerd global.
const variable can be seen from debugger while macro can be seen.
const guarantee the type which macro can't

Stack vs Heap Allocation

Where in memory are a program's static variables stored?

When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack.

The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap.

Hash Table vs. Binary Search Tree

When would you use a hash table over a Binary Search Tree?

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个错误,第一当时应该很热情的说没问题,第二回信的时候
不应该提其他面试的事情。不知道大家怎么看,谢谢。

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

Thursday, October 16, 2008

C++ interview question(zz)

发信人: chuncl (ray), 信区: JobHunting
标 题: C++ interview question
发信站: BBS 未名空间站 (Wed Oct 15 20:28:51 2008)

Comments are welcomed, thanks

1) What is difference of overridden and overload? ( I mentioned for
overload, the scope is usually the same, e.g., at same class scope, but the
interviewer said it could be different, e.g., in base class and derived
class, but that is hiding, not overload, isn’t it?)

2) How to achieve polymorphism? (I mentioned virtual function, he asked
more, then I mentioned RTTI, dynamic_cast then use if-else logic to check)

3) How to prohibit inheritance? (put cotor in private part of class?) What is the prohibition, e.g., compiler complain or running time error?

http://www.devx.com/cplus/10MinuteSolution/31881/0/page/3


4) why use dynamic_cast, it required at least virtual function defined?

dynamic_cast (expression)
reinterpret_cast (expression)
static_cast (expression)
const_cast (expression)

dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.

When a class is polymorphic, dynamic_cast performs a special checking during runtime to ensure that the expression yields a valid complete object of the requested class. dynamic_cast requires the Run-Time Type Information (RTTI) to keep track of dynamic types. Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using dynamic_cast to work properly.

static_cast can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is converted, but no safety check is performed during runtime to check if the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, the overhead of the type-safety checks of dynamic_cast is avoided.

reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.

const_cast This type of casting manipulates the constness of an object, either to be set or to be removed.

Wednesday, October 15, 2008

问个算法题(zz)

发信人: erniu (山顶洞人), 信区: JobHunting
标 题: 问个算法题
发信站: BBS 未名空间站 (Wed Oct 15 01:48:53 2008)

经常看到的一个问题是从N个数中找出最出最大/小的K个数.有人用Heap,有人用
Quicksort的变化.如果问题变为找最大/小的2个数,什么方法最优?



发信人: yoyojade (NewCop), 信区: JobHunting
标 题: Re: 问个算法题
发信站: BBS 未名空间站 (Wed Oct 15 08:34:41 2008)

this depends on,

1. if you have enough memory, save all data in the memory, you can use quick
select find the k biggest/smallest in O(n) time

2. If you don't have enough memory to save all data, use heap of size k, you
can find the biggest/smallest k number in O(nlogk)



发信人: businessp (bp), 信区: JobHunting
标 题: Re: 问个算法题
发信站: BBS 未名空间站 (Wed Oct 15 10:25:34 2008)

同时找到最大(小)和第二大(小)的数是floor(3/2n),CLRS上有讲解吧
--

Tuesday, October 14, 2008

问几个面试题目(zz)

发信人: grasssu (没有昵称), 信区: Programming
标 题: 问几个面试题目
发信站: BBS 未名空间站 (Mon Oct 13 16:10:34 2008)

1. In a plane, n points are given i.e. the input is (x1,y1), (x2,y2)... (xn,
yn). Now given these n points. Find the maximum number of collinear points.

Hint: use hough transform, pls google it, which is very basic method in image processing.

2. You're looking for a set of words in a file. Return the location of the
shortest excerpt containing all of these target words.

Sunday, October 12, 2008

面试算法题一问

发信人: PenguinLover (I love Penguin), 信区: JobHunting
标 题: 面试算法题一问
发信站: BBS 未名空间站 (Sun Oct 12 21:16:26 2008)

1)给你一个巨大的电话号码簿,每一个记录由名字和电话号码两部分组成,电话号码
簿按照名字的字母顺序排列。现在给你一个电话号码,问你如何设计算法使得在最快的
时间内找到电话号码的主人。
2)一个双精度浮点型数组,无序排列,有大概几百亿个元素(several tens of
billions of entries,我没理解错吧)。现在让你找出其中最大的一百个元素,要求
时间复杂度O(n)。我想到的是遍历一百次,但这肯定不是他想要的答案。
3)设计一个文本框,比如google search的那个框,你往里敲单词,比如computer。你
每敲一个字母,蹦出来的单词都会更新。比如,你敲c,会出来chicken, cat, concept
, combine,computer, 等等。你接着键入o,就只剩下concept和combine和computer了
。再键入m,就剩下combine和computer了......问这个如何实现。
4)C++里面的dynamic_cast如何实现,为什么dynamic_cast要求被cast的指针指向的类
至少要有一个virtual method?
5)virtual inheritance 为什么能够避免导出类中有多于一个的 virtual base class
的copy?
6)对于一个未知的binary tree,给你先序遍历和中序遍历的结果,要你重建这个二叉
树,用什么算法?如果其他组合呢?比如先序和后序?中序和后序?


=================================================================
发信人: sebqian (我是花栗鼠1), 信区: JobHunting
标 题: Re: 面试算法题一问
发信站: BBS 未名空间站 (Sun Oct 12 22:08:54 2008)

2) maxheap size 100, 最小的放root. 余下的数只和root比较。如果比较大,换root
, then adjust heap O(log100). 最后结果就是root.
4) 是不是要check virtual table?
5)提醒compiler对base class的initialization. 具体怎么做不知道。有没有那位能
说一下?
--

Friday, October 10, 2008

MS一道面试题(zz)

发信人: batixu (Ninny), 信区: JobHunting
标 题: MS一道面试题
发信站: BBS 未名空间站 (Fri Oct 10 14:27:32 2008)

Write a function that prints out all sets of consecutive integers that add
up to all and any numbers within a given range. For example, given a range
of 4-9, your function must print out the fact that 5=2+3, 6=1+2+3, 7=3+4, 9=
2+3+4, 9=4+5.

about pure virtual funciton

发信人: chuncl (ray), 信区: JobHunting
标 题: definition of pure virtual funciton
发信站: BBS 未名空间站 (Thu Oct 9 12:15:04 2008)

as we know that pure virtual method function typically have a declaration (
signature) and no definition (implementation).

but in an interview, the interviewer told me we CAN or compiler permit us
give definition of pure virtual function.

is it right? thanks

发信人: zlike (最终幻想), 信区: JobHunting
标 题: Re: definition of pure virtual funciton
发信站: BBS 未名空间站 (Thu Oct 9 15:05:05 2008)

usually for dtor(), it's going to be called by all inherited class, but you
also want to FORCE all inherit class to define its own dtor(). In this case,
you must give definition to base dtor() and make it pure virtual.

C++ interview question(zz)

发信人: chuncl (ray), 信区: JobHunting
标 题: C++ interview question?
发信站: BBS 未名空间站 (Fri Oct 10 00:54:39 2008)

I got some c++ interview question like this. Any idea?

1) in c++ what is the relationship and/or difference between virtual function and "delegation"?

2) What are the names of the standard narrow-character iostream objects?
Choice 1
cout, cin, cerr
Choice 2
cout, cin, cerr, stdlog
Choice 3
cout, cin, stderr, stdlog
Choice 4
cout, stdin, stderr
Choice 5
cout, cin, cerr, clog


thanks

Wednesday, October 8, 2008

C++ Primer

The key idea behind OOP is polymorphism. As we'll see, in C++, polymorphism applies only to references or pointers to types related by inheritance.

Inheritance lets us define classes that model relationships among types, sharing what is common and specializing only that which is inherently different.

Dynamic binding lets us write programs that use objects of any type in an inheritance hierarchy without caring about the objects' specific types.

Note: In C++, dynamic binding happens when a virtual function is called through a reference (or a pointer) to a base class. The fact that a reference (or pointer) might refer to either a base- or a derived-class object is the key to dynamic binding. Calls to virtual functions made through a reference (or pointer) are resolved at run time: The function that is called is the one defined by the actual type of the object to which the reference (or pointer) refers.


Virtual destructor It is worth noting that classes used as the root class of an inheritance hierarchy generally define a virtual destructor.

Virtual functions
1) Purpose: The purpose of the virtual keyword is to enable dynamic binding. By default, member functions are nonvirtual. Calls to nonvirtual functions are resolved at compile time.
2) Rule: A base class usually should define as virtual any function that a derived class will need to redefine.
3) Scope: Any nonstatic member function, other than a constructor, may be virtual. The virtual keyword appears only on the member-function declaration inside the class. The virtual keyword may not be used on a function definition that appears outside the class body.

Virtual function in derived class: Once a function is declared as virtual in a base class it remains virtual; nothing the derived classes do can change the fact that the function is virtual. When a derived class redefines a virtual, it may use the virtual keyword, but it is not required to do so.

Inheritance vs Composition:
Inheritance should reflect a so-called "Is A" relationship of the derived class to the base class. In our bookstore example, our base class represents the concept of a book sold at a stipulated price. Our Bulk_item is a kind of book, but one with a different pricing strategy.
Composition among types is a so-called "Has A" relationship. Our bookstore classes have a price and they have an ISBN. Types related by a "Has A" relationship imply membership. Thus, our bookstore classes are composed from members representing the price and the ISBN.

Operator Overloading and Friendship
To support:
cout << item1 + item2 << endl;

we need the following definition:
class Sales_item {
friend std::istream& operator>>
(std::istream&, Sales_item&);
friend std::ostream& operator<<
(std::ostream&, const Sales_item&);
public:
Sales_item& operator+=(const Sales_item&);
};
Sales_item operator+(const Sales_item&, const Sales_item&);

Tuesday, October 7, 2008

Inside the C++ Object Model

Although you can manipulate a base class object of an inheritance hierarchy either directly or indirectly, only the indirect manipulation of the object through a pointer or reference supports the polymorphism necessary for OO programming.

Two ways to manipulate a base class object of a hierarchy of classes:
1) directly through base class
BaseA a;
a.DoSomething()

2) Indirectly using a pointer or reference
DerivedB b;
BaseA& a = b;
BaseA* c = &b;

Only the latter supports the polymorphism necessary for OO programming.


In the OO paradigm, the programmer manipulates an unknown instance of a bounded but infinite set of types. The actual type of the object addressed is not resolved in principle until runtime at each particular point of execution. In C++, this is achieved only through the manipulation of objects through pointers and references. In contrast, in the ADT paradigm the programmer manipulates an instance of a fixed, singular type that is completely defined at the point of compilation.

Monday, October 6, 2008

google面试题(zz)

发信人: justcome (ANewOne), 信区: JobHunting
标 题: 问一道google面试题
发信站: BBS 未名空间站 (Mon Oct 6 17:54:07 2008), 转信

没有看懂答案是什么意思?能不能给解释一下

3. you need read a lot of records, you don't know how many records here
before you complete it. After you read all those records, you need select
one record randomly.In another words, every record has same chance to be
selected. The memory is limited, that means you cannot store all those
records at one time.


Changing your random number pick dynamically. Add a weight (1/n) to nth
new number, add a weight (n-1)/n to the random number you picked from (1 to
n-1) numbers. This can guaranteer the randomess without knowing how many
number ahead.
1. P(1)=1; 2. P(2)=1/2, P(1)=1*1/2; 3. P(3)=1/3, P(1)=P(2)=2/3*1/2=1/3…

Sunday, October 5, 2008

memory layouts of C++ classes (zz)

Newsgroups: comp.compilers
From: ssimmons@convex.com (Steve Simmons)
Keywords: C++, code, design, comment
Organization: CONVEX News Network, Engineering (cnn.eng), Richardson, Tx USA
References: 94-08-087
Date: Sun, 14 Aug 1994 15:48:29 GMT

> After walking the heap to reverse engineer the memory layout
> of a class in MFC, I figured this forum might provide a better
> answer. How does a C++ compiler build the memory layout of a class
> and a derived class?

First, memory layout is not defined by the language... It is an
implementation issue; however, a common approach is the following...

Given:

Class A{
field a;
}

Class B{
field b;
}

Class C{
field c;
}

No Inheritance:

For class A, the layout would be:

first field: pointer to virtual table A
second field: the field a.

Single Inheritance:

For B derived from A (class B : public A), the layout would be:

first field: pointer to virtual table for B... first
entries in this table are inherited/overriden
virtual functions of A.

second field: the field a.
third field: the field b.

Multiple Inheritance:

For C derived from both A and B (class C : public A, public B), the
layout would be:

first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.

second field: the field a.
third field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.

fourth field: the field a in B's class.
fifth field the field b
sixth field: the field c.

Virtual Inheritance:

As you can see in the above example, field A is replicated twice in
Class C. If you want only only one field A, you would need to
do the following:

class A
class B : public virtual A
class C : public virtual A, public B

The layout for C would be the following:

first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.

second field: pointer to field a.
third field: the field a. (points to second field)
fourth field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.

fifth field: pointer to field a (should point to second field)
sixth field: field a (never used, cheaper to allocate here as
opposed to doing a malloc).
seventh field the field b
eighth field: the field c.

> Also, what is the difference in memory layout
> of a regular C++ compiler and the memory layouts used by SOM compilers
> to provide binary consistency?

This would be interesting to hear... does someone out there know???

Thank you.

Steve Simmons

[What are SOM compilers? -John]

告别我的跳槽岁月(ZZ)

发信人: ctmj (James), 信区: JobHunting
标 题: 告别我的跳槽岁月
发信站: BBS 未名空间站 (Sun Oct 5 12:23:49 2008)

在新公司工作已经有一段时间了。一直想写点东西总结一下找工跳槽的得失成败。一是
对三个月的找工时日做个总结,二也希望对其他人有所帮助。

我的背景:
MBA毕业后在一家Consulting公司工作了快五年,决定换工作前刚拿到绿卡。

第一,确认自己的职业目标。个人感觉这一步是最难但又必须考虑清楚的,因为找工作
切记要专注。可能由于我是学MBA和做咨询的缘故,感觉上啥都可以做:Strategic
Development, Finance, Venture Capital etc. 但又很难说的很清晰、具体。刚开始
的时候我是见了什么职位sexy就投什么,深感效果不彰。但慢慢在找工作的过程当中对
自己以及就业市场有了比较客观的认识,也就逐渐确立了职业目标。其实这也就是个小
马过河的问题 --- 有些事情可以找很多人了解,但每个人的情形都不大一样,所以必
须自己试一试才知道。

第二,简历:多读、多写、多改。我写简历的思路大致是这样的 --- 首先拿自己以前
的简历好好看看,总结一下自己最近的一些工作经历。然后换位从Recruiter的角度来
想:什么样的经历和Skills会引起Recruiter的兴趣?如何包装自己的经历?谈到包装
,我觉得首先要诚实、有一说一。但也要学会组织语言去突出自己的优点、长项。我一
直认为每个人都有独特的背景和优点,关键是如何去更好的通过简历去展现自己。这个
问题往往不是一天两天能够回答的。所以说写简历是一个周而复始的过程,不可能once
and for all 的一次搞定。

第三,找工作的渠道,根据我的经历按有效程度排列如下:
第一, 猎头公司。可能由于我有工作经历的缘故,我的几个offer都是通过猎头找
到的。有的Recruiter相当不错,分析你的背景,寻找合适的机会,帮助准备面试。但
有的Recruiter也很Jerk,这种人就不理他就是了。
第二, 上网贴简历。网上贴简历有两个好处,一个是吸引好的猎头和你联系;其次
一些大公司也会主动在网上找人,有两家大公司就是这么和我联系上的。
第三, 同学、朋友帮忙。我觉得这点被很多人过分强调了。同学、朋友有相当作用
,尤其是在定位及准备的过程当中。但找工的时候很难碰上合适的机会。
第四, 上公司网站递简历。费时费力,往往填一个公司的网页要半到一个小时,而
且填完了感觉基本没人看。我一般是等拿到offer或者HR的老大一定要填的时候才填。
第五, 回应招聘网站(Monster, CareerBuilder etc.)的招聘启事。就我个人而
言,基本没啥用。

第四,如何准备面试:精心准备,勤于苦练。记得刚开始找工作的过程,一个Bank有个
挺不错的机会。我实际是在mentally没有ready的情况下就去匆匆面试了,结果就不言
而喻了。正所谓机会是给有准备的人的。后来这家Bank没有能挺过这次金融危机,但那
是后话了。

第五,Offer Negotiation.
最近market很不好,我实际上也没有太多的leverage去negotiate。所以只是选了一个
还不错的offer然后counter-offer了一次就从了。

现在金融市场很不好,各行各业也受到很大影响,找工作也变得很艰难了。而作为中国
学生又有身份、语言、背景各方面的障碍,找工之路会尤其艰辛。希望大家能够咬牙顶
住,希望就在前方。

分享一些我学到的面试技巧(ZZ)

发信人: dongyan (bird), 信区: JobHunting
标 题: 分享一些我学到的面试技巧
发信站: BBS 未名空间站 (Fri Oct 3 12:50:54 2008)

在本版潜水很久受益匪浅,现回报本版几个从Professional那学来的Interview技巧,
希望大家都能拿到心仪的offer:

1 What are the real reasons you left your job
回答问题要客观,不要带有感情色彩,回答原因后迅速转到自己正在寻找什么样的机会,比如我是laid off,那么我的回答就是: Due to the downsize of ** company, lots of positions were eliminated including mine. Now, I am looking for an opportunity in ****
Professional说不要用 My former employer,用自己former employer的名字,听起
来更客观,更职业。

2 Tell me about yourself.
在回答这个问题的时候,要偏重与该工作有关的经历。不要过长,控制在2分钟之内。刚开始,我不知道,还会说一些personality有关的故事。同样在别人问你的强处的时候,也该更多说一些与工作有关的强处。

3 讨论薪水。
一定要先appreciate offer,表示自己的interest,再谈要害。

4 简历的问题,帮我改简历的机构把我最开始的Objective去掉了。他们说没人care你想要什么工作,他们更care你会干什么。所以,我把Objective变成自己经验的一个总结。后面跟的是与工作有关的技能总结,就是大家常用的summary。

帮我改简历的机构还是把我的简历压缩成一页半了。但是我以前的老板说要写的详细,越细越好,也许不同的公司喜欢的风格不同吧。

5 Wrap up找工作
Professional说不到上班那一天就不要停止jobhunting(但是我挺懒的,拿到心仪的offer还在背景调查就不找了) 开始上班后,要给所有在找工作过程中帮过忙的人发一封信告知他们你的status并表示感谢。

码字很累,版主如果觉得文章还有意义,就发我几个包子吧^@^

改简历服务:www.lhh.com

他们给我改的简历结构就是

1 工作经验总结,more Like strength
2 skills
3 experience, not laundry list, mention your achievements.
4 education

Bloomberg的面试

1. stl, map,list啥的结构
2. 基于2叉树的遍历查找删除
3. C++继承
4. 一个bruteforce的算法
5. malloc/new的区别
6. new之后free有啥结果
7. 进程,线程的堆
8. pthread
9. virtual class
10. 全是函数的类的对象的size
11. hashmap做不了啥map能做的
12. os缺页之后硬件会trap
13.还有很多,记不得了,2轮面了将近2个小时,最后面完直接告诉我good luck,应该
没戏了