Total Number of Perfect Squares (Hacker Rank Interview Question)

Problem: Finding the number of perfect squares between two integer range.

Input: first integer and last integer ->  int startInteger, int endInteger

Output: total number of perfect squares including this number -> int count

The worst approach would be to loop over each element and check whether it is an integer.

The best approach would be to determine the floor of the root of first integer and ceil of the root of the last integer.

So let’s take an example of 10, 100.

The expected answer would be: 7

So first compute the floor of the square root of 10 that would be 3.

And then compute the ceil of the square root of 100 that would be 10.

10 – 3 = 7 will be the output. It will be O(1).

You can have below code from GIT.


The output of above code is as below:

You can have above code from GIT.


Leave a Reply

Your email address will not be published. Required fields are marked *