Interview FAQ III
1) Write an *unordered* tree into disk and reconstruct same tree.
Important point to note here is to replace the pointer information left node, right node etc with offset in file where left node, right node are placed.
Eg: Node a :
val 5
left 0x30001244 [node b]
right 0x30001264 [node c]
where node b and node c are placed in 0x30001244 and 0x30001264. When node a, node b, node c are written into a file at offsets 0, 16 and 32 then replace node as below when dumping iit into the disk
Eg: Node a :
val 5
left 16
right 32
2) Difference between process and thread. Process has its own address space, context switching is costlier.
Threads are light weight, they share the address space with main thread. Context switching is light weight complete register set many not be loaded and stored.
3) Using variable arguments
#include stdarg.h
#include stdio.h
/* Here n is the number of arguments */
void varArg(int n, ...)
{
va_list p;
va_start(p, n);
printf("count = %d: ", n);
while (n-- > 0) {
int i = va_arg(p, int);
printf("%d ", i);
}
printf("\n");
va_end(p);
}
int main()
{
varArg(1, 1);
varArg(3, 1, 2, 3);
return 0;
}
4) Function that accepts a function pointer
void PassPtr(int (*pt2Func)(float, char, char))
5) Function pointer that accepts a function pointer
void (*PassPtr) (int (*pt2Func)(float, char, char))
6) Write a recursion program to print elements in a linked list
printNode ( vNode *n)
{
if ( n == NULL)
return;
else
{
printf ("Value of node is %d\n", n->val);
printNode (n->next);
}
}
7) Difference between mutex and semaphore.
Mutex: Mutually exclusive. It is usually used to protect a critical region so that one thread uses the resources.
Semaphore: Mechanism to share more than one resource. For eg: if you have 5 resources you can use a counting semaphore with sem value 5. This makes sure that only 5 concurrent threads can access the semaphore.
In Vxworks mutex is a different from a binary semaphore as mutex can be released
only by the thread that owns the mutex. However binary semaphore can be released
by threads that dont own it.
8) Swapping two variables
a = a+b;
b= a-b;
b=a-b;
9) Program for counting number of bits set in a variable.
int count =0;
while ( (a= a>> 1 ))
{
if ( a & 0x1)
count++;
}
Labels: interview faq