Print characters coming in one of the 2 strings (and not in both)

Given two strings, write a function which will print characters coming on only one of the two strings (and not both). The character may be repeated in that string. For example: String-1 String-2 Output ——— ———- ————- AFW BGF AWBG AFAAW BGFFB AWBG BGF AWBG AFW BGH AFWBGH AFW BGF AWBG

Which of the two comparison is better

While comparing a variable with a constant (literal) in C/C++ language, which of the two ways of comparison should we use ? if( x == 2 ) … if( ptr == NULL ) … Or if( 2 == x ) … if( NULL == ptr ) … In the first comparison we are writing variable […]

Disallowing object creation on heap

Object of a class can be created dynamically on heap or statically on Stack or Data Area. If we have a class MyClass, then the below objects will be created on heap. // Operator: New MyClass *ptr1 = new MyClass(); // Crating object of MyClass on heap. // Operator: New-Array MyClass *ptr2 = new MyClass[10]; // […]

Allowing only dynamic object creation on heap

Write a class, such that user can create its object on heap (using new) but user should not be able to create its object on stack or data area. i.e if the class is MyClass, then MyClass stackObj; // Should give compile time error. MyClass *heapobj = new MyClass; // This should be OK, & […]

Build binary tree from ancestor matrics

You are given the Ancestor matrix of a Binary tree, write an Algorithm to construct the corresponding tree. For example, the below tree: Will have the following ancestor Matrix The order of rows in the above matrix is not defined. I have kept it in the ascending order because the data in our nodes is […]

Print both diagonals of a matrix

Given a matrix of order N*N, write code to print both the diagonals of that matrix. For example: the matrix and its 2 diagonals are given below: In the above diagram, I have colored the elements in first diagonal as red and elements in 2nd diagonal as green.

Print elements of a matrix in diagonal order

Given a square matrix of order N*N, write code to print all the elements in the order of their diagonal. For example, in the below matrix, the elements should be printed in the marked (in red) order, and the final output should be as shown below:

Islands in a two dimensional array

Each element in the array is connected to eight other elements (toward top, down, left and right). For example, the zero below is connected to all the ones 1 1 1 1 0 1 1 1 1 Given a two-dimensional array of 0’s and 1’s. Find the number of islands where an island is a […]

Hamming distance

Write code to calculate hamming distance between two strings. Hamming distance between two strings of equal length is equal to the total number of positions at which corresponding characters in the two strings are different. String-1 String-2 Hamming distance ———— ———— —————- TONED ROSES 3 KAMAL RAWAT 3 203 233 1 RITAM BHARA 5 OK […]

using delete operator on this

Can we use delete operator on this inside a class member function in C++, something like below: class TestClass { public: void myFunc() { delete this; } };