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