Time and Speed puzzle
Two friends decided to get together and start riding bikes towards each other, both at a speed of 6 km/hr (starting at the same time). The live 36 km apart and hence will meet halfway. One of them have a pet pigeon and it starts flying the instant the friends start traveling. The pigeon flies […]
How to swap 2 variables witout using third
The usual method of swapping two numbers, using a temporaty variable is as shown below. // Swapping 2 int, X & Y using temporary variable t int t = X; X = Y; Y = t; How will you swap two numbers without using a temporary variable?
Allocating memory to n-dim Array on heap
Dynamic arrays are on heap. If an array is stored on heap, its address must be stored in pointer variables, else there will be no way to access the array. Let us assume the Array to hold integers. Write a code to allocate memory to a 2-dim array and then generalize it to allocate memory […]
Burning Rope puzzle
You have two identical ropes, which are non-uniform in composition. Each rope takes 60 minutes to burn (But non-uniformly, for example: 90% of the rope may burn in 5 min and rest 10% may take 55 min or any such composition). You don’t have any watch but a lighter. How will you calculate 45 minutes?
k'th Node from End
Given a linked list and a positive integer k, write a function which will return value of k’th node from the end of list. If Linked list is 2 -> 4 -> 6 -> 3 -> 7 and k=2, then output should be 3 (2nd node from end).
Divide & Conquer Approach to find a^n
If you want to compute a^n ( a raised to the power n), then the algorithm is simple: long power(int a, int n) { long prod = 1, i=0; for(; i<n; i++) prod = prod * a; return prod; } But the above function will take O(n) time. Write a divide & conquer algorithm which […]
N Doors Puzzle
There are N doors in a row, numbered from 1 to N. Initially all are closed. Then you make N passes by N doors. In first pass you toggle (open the door if it is closed and close it if it is opened) all the doors starting from the first door. In the second pass […]
Should we use user-defined type conversions
Yesterday I talked about what are user-defined conversions and how Single Argument constructor and overloaded type conversions operators act as type conversions from a Class (user-defined type) to any other type and vica-versa. Today’s questions is more like a design question, C++ provide us the functionality to define user-defined type conversions, but should is it […]
User-defined type conversions in C++ (Single argument constructor & implicit type conversion operators function)
There are 2 types of user-defined conversion functions in C++ – Single Argument constructor. – implicit type conversion operators. Explain them
difference between reference & pointers in C++
What is the difference between reference & pointers in C++? Is it safe to use pointers in places where we use reference & vice-versa?