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 […]

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:

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 […]