C/Algorithm/Data Structure/Compiler Related Questions (2)

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 174 收藏 0 点赞 0 评论 0

Q:  Difference between run time binding and compile time binding
A:  compile time bind(early binding) includes:  function overloading, operator overloading.
run-time binding(late binding): polymorphism(virtual function), i.e:  base class pointer(or reference) is allocated a pointer(or reference) of derived class, the actual func call is only determined during runtime through virtual table entry(and vfptr).

Notes about (pure) Virtual function: The two most salient features of pure virtual functions are that they must be redeclared by any concrete class that inherits them, and they typically have no definition in abstract classes.
•    The purpose of declaring a pure virtual function is to have derived classes inherit a function interface only
•    The purpose of declaring a simple virtual function is to have derived classes inherit a function interface as well as a default implementation.

Some sample code:
class B {
public:
void mf();

};

class D: public B {
public:
void mf();             // hides B::mf; see Item 50

};

D x;                     // x is an object of type D
B *pB = &x;              // get pointer to x
*pD = &x;                // get pointer to x
pB->mf();                // call mf through pointer
pD->mf();                // call mf through pointer
//Note: pB->mf() binds to B::mf()
//      pD->mf() binds to D::mf()
//However, if B::mf() is declared virtual, “dynmica binding” will come into play, which will have both calls to D::mf()!!!

Q: Why does C/C++ give better run-time performance than Java?
A:  Java bytecode is interpreted instead of compiled(as of C/C++), platform independence askes Java virtual machine to “interpret” Java bytecode(*.class) again on a particular computer architecture, thus execute more machine-language instructions.

Q:  What errors are caught at compile time vs link time?
A:   compile time: syntactical & semantical errors are caught.
link time:  dependcncy errors (i.e: resolval of function calls or errors in including other modules)

Q: C or C++ conditional compilation
A:
#ifdef/ifndef
#else/elif
#error “__FILE …..”   # print out diagnostic msg.
#endif

Q: What is the value of “a” after this?  (Similar question asked by Fortinet during onsite coding interview)
int (*a) [10];
a++;
A: if size of int is 4 bytes, a++ will points to somewhere about 4*10 bytes after intially set: 0x0 (possibly, not 100% sure, only static variable will be allocated virtual addr: 0x0), thus value of “a” could be: 40.

Q:  Given a struct:
struct s {
int a;
char b;
int c;
char d;
};
What’s your take on the size of s?  (Yahoo! Onsite)
A:  If you answer 10 and you think you are alright, you are wrecked, it’s 16.
What about  struct s { int a; int c; char b; char d; }; ?  It’s 12 bytes dude.
Why?  Each char will occupy a full word(4-bytes)
When a very very long link-list with element the size of struct s, sequence of definition matters!

Q:  Tell me which is faster? Strcpy() or memcpy()?  (Netli onsite)
A:   memcpy() is way faster than strcpy(). Why?  Memcpy() copies by word boundry, that is 4-byte a time.

Q: Is it possible to keep 2 stacks in a single array, if one grows from position one of the array, and the other grows from the last position. Write a procedure PUSH(x,s) that pushes element x onto stack S, where S is one or the other of these two stacks. Include all necessary error checks
A:     The array shud be visible to both stacks (and therefore be global, or should provide equivalent effect). Also the stack tops shud be taken care of. one top will decrement everytime the push method is called and one would be incremented. As soon as s1.top==s2.top we shud throw an exception.

Q:  fibonacci 0 1 1 2 3 5 8 13 21 ….
A:  int fib( int n ){
if (n==1) return 1;
else if(n==0) return 0;
else return fib(n-1) + fib(n-2);
}

Q:  sort a link-list . (single link-list)
A:  Mergesort, quick-sort(? for array?), heapsort(),  LBucketSort …

void LBucketSort(PNODE phead)
{
PNODE pstep, pnode;
int t;
if(phead == NULL) return(NULL);
pnode = phead;
while (pnode)
{
pstep = pnode->next;
while (pstep)
{
if (pnode->d > pstep->d)
{
t = pnode->d;
pnode->d = pstep->d;
pstep->d = t;
}
pstep = pstep->next;
}
pnode = pnode->next;
}
} //-END-

================================ Sort and Searching in-depth review ================================

Note:
Sorting:
for arrays:  bubble sort, linear insertion, quicksort, shell sort, heap sort, …
for other data strucutures(list..):  merge sort, quicksort for lists, bucket sort, radix sort, treesort,

Merging:
list merging:    O(N), merge_2_lists(list *a, list *b); while() loop one a or b once is enough.
http://www.dcc.uchile.cl/~rbaeza/handbook/algs/4/431.merge.c.html
array merging:

评论列表
文章目录