Close

## Computing the power of a given number

Given a base and it’s exponent, how do we efficiently evaluate it? In other way, how do implement a pow() function which is typically provided by a library. For example 210 = 1024. The simplest method that comes to our mind is multiply the base with 1 in a loop for exponent times. double result…

## Counting the number of perfect squares in a given range

Given a range of numbers, how do we count the number of perfect squares? This problem is posted on Hackerrank as part of 20/20 Hack March 2014. If you do not want to read the solution and you want to solve the problem on your own, Click on the link. If you want to know…

## Deleting duplicate numbers from a sorted array

Given a sorted array of numbers, how to delete the duplicate numbers in it? We have to write a function to modify the existing array without using any extra space and return the new length. The solution to this problem is similar to that of “Deleting all instances of a particular number from a given…

## Google codejam 2014 – Qualification Round Problem – Magic trick

The following problem is from the Google code jam 2014 qualification round.  Head-over there to read the complete problem description.   The problem description is as follows. A magician starts by arranging 16 cards in a square grid: 4 rows of cards, with 4 cards in each row. Each card has a different number from…

## C++ STL Algorithms – Sort – Part-2

In the last post, the basic use of STL sort() method is explained. In this post, I will discuss some more options available with this method. We have seen in the previous post that sort takes the beginning and ending of the array as arguments. sort( array.begin(), array.end() ); In addition to those parameters, it…

## C++ STL Algorithms – Sort- Part-1

Sorting is one of the most widely used algorithmic primitive in programming.  C++ Standard Template Library (STL) provides an efficient implementation of the sort algorithm. It is always better to use this algorithm instead of writing our own implementation because of the following benefits. It’s performance would surely be better than your own implementation. It’s…

## Size of an empty object in C++

What is the size of an empty object in C++? This is one of the most frequently asked questions on forums. For example consider the following code snippet. class A { }; A aOb; cout << sizeof(aOb); The size of an empty object is not zero to ensure that the addresses of two different objects…

## Check if two binary trees are identical

Given two binary tress, how do we check if they are identical in structure, and  contents? For example the following two binary trees are identical. We can solve this problem using recursion effectively. The function checks if the root element is equal and use the same function to check if it’s left and right sub-trees…