Monday, July 25, 2011

Programming interview questions

Hi,
As a software developer we would have came across so many challenges in our day to day work. It would be very helpful if we have the following attitude while solving any puzzle:

1. Be open for challenge
2. Try to synthesize the problem into smaller chunks
3. Write down the algorithm for the solution
4. Think out of the box
5. Don't restrict your capabilities
6. Be Honest in finding the solution

definitely the above approach will give you the cake walk solution against any terrible problem.
Okay let me give you some interesting programming questions I have come across:

Question 1: How could you determine if a linked list contains a cycle in it, and, at what node the cycle starts?
Answer 1:

NODE *p1 = *p2 = HEAD;
 do {  p1 = p1->next;  if(p2->next) p2 = p2->next->next; 
} while (p1 != p2);
if p1 and p2 is NULL then it is acyclic otherwise it is cyclic.

Q2: How will you reverse the singly linked list?

Q3: Write a program to remove duplicates in array?

Q4: How will you reverse the doubly linked list without swapping values?

Q5: How can you reset a bit from the given position of given number?

Q6. Insert a node in sorted doubly linked list

Q7. Construct a BST(Binary Search Tree) for the node 3, 1, 2

Q8. Write code to search a given element in BST

Q9. Write a code to find maximum depth of the BST

Q10. What are all the difference between STL map and set. Can I conver a map into set?

Q11. Why hash table and where it is used?

Cheers,
Rajan

Thursday, July 21, 2011

C++ interview questions

Everyone who works in software industry as developer or tester would have come across interview in C++ - The most powerful Object Oriented Programming Language. I would like to list some of the interesting C++ questions in this column which I have come across.

1. What is the size of below structures and how the size is calculated?
a. struct st{
int i;
char c;
double d;
};

b. struct st{ };


2. When will you use "static global" variable in a file?

3. Where initialized static and uninitialized extern variable stored in application memory layout?

4. Let's say your application is crashed before hitting main()'s breakpoint, what will be the possible reasons for crash?

5. What is the significance of using "const obj&" in copy constructor? Whyn't can I pass object in other ways?

6. What is the size of below structures and how the size is calculated?
a. class A{
virtual vfuc1() = 0;
};

class A1{
virtual void vfunc2(){printf("I'm class A1's vfunc2\n");}
};

b. class B : class A, class A1 {
virtual vfunc1() {printf("I'm class B's vfunc1\n");}
virtual vfunc2() {printf("I'm class B's vfunc2\n");}
};

7. How many vtptr(virtual table pointer) and vtable(virtual function table) would have been created for below definition?
A1 array[10]; // refer A1's declaration in Q8.

8. How virtual key in virtual destructor take care of calling derived class dtor(destructor) first then base dtor when the derived class object is deleted through base class pointer?

9. What is the size of below class B and why?
class A{ public: virtual void vfuc(){} };
class B: virtual class A {};

10. Give me some example when ctor has to be added explicitly for a class?

11. Give me some scenario why you need virtual functions?

12. What is the word 'virtual' significance in virtual base class?
say: class B: virtual class A {};

13. Do you achieve function overriding without virtual keyword?

14. What is deep-copy and how will you achieve it?

15. When will you implement your own Copy Ctor and assignment operator?

16. When copy ctor and assignment operator get called below?
class Fred {}
Fred obj1, obj2, obj3;
obj3 = obj1;
Fred obj3 = obj2;
Fred obj5(obj2);
Fred obj9();

18. When copy ctor will be called by compiler?

17. Why const &Ref is the parameter for copy ctor, why not const obj?


18. How to avoid my objects getting copied?

19. How can you avoid my objects getting copied with my object?

20. How do you know whether the given class is given for inheritance or not?

21. What are the things you consider while designing your class?

22. How do you decide whether your ctor has to be inline or not?

23. How do you avoid your object creation on stack?

24. Can your destructor be pure virtual to make your class ABC. If yes then does dtor should have body or not?

25. What is function object?

26. What are the differences between set and map?

27. Can I convert map into set?

28. What is association?

29. What is the difference between aggregation and composition?

30. why function overloading is not done using different return types in C++?

- to be continued




Thursday, July 7, 2011

Can I write overloaded function inside my DLL?

You cant do it.....GetProcAddress expects C linkage...no name decoration...therefore no overloading.......either use 2 function names or link via a lib file

Yes you can export objects, but it's not straight forward.

Normally, you export a function to create an instance of your object and return a pointer to it. You also need to export a function to destroy it.

In addition, if you want compatability between compilers, and/or between different versions of your DLL, you need to use pure virtual base classes and export pointers to these.

Here's some info:

www.bcbdev.com/articles/vcdll2.htm