Out of memory condition in C++ for new operator
How will you handle a situation if new operator is not able to allocate memory on heap to your program? In C language malloc returns NULL, if it is not able to allocate memory, so the check can be as below: int *ptr = malloc(sizeof(int)); if (ptr == NULL) ; // malloc not able to […]
Print ancestors of a node in a Binary Tree Node
Given a Binary tree and an integer, print all the ancestors of the node. For example: If the given tree is as below: Input: 5 Output: 10 Input: 40 Output: 30 10 Input: 1 Output: 4 5 10
Reversing words in a String
Given a String, reverse the string but do not reverse the words. For example, if Input : “MCN Professionals is now Ritambhara” Output: “Ritambhara now is Professionals MCN” Your function should not take more that O(n) time in the worst case.
Level-wise traversal of a binary tree
Write an Algorithm to print the elements of a binary tree in level-wise order. For example if the Binary tree is as given below: Then the traversal should be in the following order:
sorting 1 billion numbers
Which sorting algorithm will you use to sort 1 billion numbers.
camel & banana puzzle
A Camel has to transport 3000 bananas from point A to point B, both 1000km apart. There are few constraints There is only one Camel. Camel can not carry more than 1000 bananas at one time. Camel eats one banana every kilometer when he travels. What is the maximum number of bananas which can be […]
Output of a C function
Given the following structure of Node of the Linked List: struct Node{ int data; Node *link; }; What is the purpose of the below function void myFun(Node* head) { if(NULL == head){ return; } printf(“%d “, head->data); if(head->link != NULL ) myFun(head->link->link); printf(“%d “, head->data); }
Check if number is a power of 2
How will you check if a number if a power of two or not? For example: 4, 8, 1024 etc are powers of two. But 6, 40, 95 etc are not
Search in a matrix sorted on rows & columns
Given a matrix whose each row and column is sorted as shown below 10 20 30 40 15 25 35 45 27 29 37 48 32 33 39 50 Write an algorithm that will search for the matrix for a value (say 37) and print the index of position where it finds the value. If […]
value of i++ + ++i or ++i + i ++
If the initial value of i is 2 What is the value of expression i++ + i++?