Handshakes question
At a party, everyone shook hands with everybody else. There were 66 handshakes. How many people were there at the party?
Cheating Husband puzzle
A certain town has 100 married couples (husband and wife only). Everyone in the town lives by the following rule: If a husband cheats on his wife, the husband is executed (killed) as soon as his wife get to know out about him (He may enjoy only as long as his wife does not get to know about […]
Order of evaluation of operands
What will be the output of the below code in C or C++ language? int x = 0; int a() { x = 10; return 2; } int b() { x = 20; return 3; } main() { int y = a() + b(); printf(“%d”, x); }
Difference between struct and class in C++
In C++, a struct can have functions also, the way we have in a class. For example, the below definition of Node in C++ is perfectly valid. struct Node { private: int data; Node *link; public: // Default Constructor Node():data(0), link(NULL) { cout<< ” Default Constructor”; } // Single Argument Constructor Node(int d):data(d), link(NULL) { cout<< ” Single […]
Dropping egg puzzle
You have two identical eggs and access to a 100 floor building. You don’t know how strong the eggs are. Eggs can be really strong (may not break if dropped from 100th floor) or fragile (break if dropped from first floor). You want to find out the highest floor from where an egg can be […]
Open Close Principle
The Open Close Principle in Programming says “Software entities like Classes, Functions, Modules should be open for extension, but closed for modification.” It encourages programmers to write code in such a way that new functionality can be added without changing the existing code. i.e If we are writing a class, then we may later, extend […]
Remove duplicates from a linked list
Given a linked list, write code to remove duplicates from the list. Input : 1->3->4->4->6->6->6->7 Output: 1->3->4->6->7 Input : 4->1->4->4 Output: 4->1
Merging two Linked List
Given two linked list with Node values sorted in ascending order. Write function that accepts these two linked lists, merge them and return pointer to merged list. In the process Original lists should be deleted (the pointer to head of original lists should be set to NULL)
Find number occurring odd number of times in an array
Given an array of positive integers in which each number is repeated even number of times, except one which is repeated odd number of times. Write an O(n) time algorithm to find the Number repeating odd number of times. For Example, If the input array is: {1, 2, 9, 2, 8, 1, 9, 2, 8, […]
Print all nodes at distance k from root of a Binary Tree
Given a Binary Tree and a positive integer ‘k’, write code which will print all the nodes which are at distance ‘k’ from the root. For example: For Binary Tree on the right side, Following are the nodes which should get printed for the below values of ‘k’ k output — ——- 0 10 1 […]