AfterAcademy Tech
•
29 Feb 2020

Difficulty: Medium
Asked in: Amazon, Microsoft, Facebook
Given an integer, your task is to find the square root of the integer. For an integer x to be the square root of the given integer N, x*x must be equal to N.
For example: →
Input: 16
Output: 4
Explanation: 4x4 is equal to 16. Thus 4 is the square root of 16.
Similarly,
Input: 25
Output: 5
Possible follow-up questions to ask the interviewer: →
We are going to see two different approach to solve this problem. The first one is a very simple while the second one is an efficient solution to come up with.
Solution idea
The idea here is to use simple iteration and to iterate through all the integers starting from 1 until we get the actual square root of the given integer. For each element i in the iteration, we will check if if i*i is equal to N(the given integer) or not. If not then i*i must be smaller than N. So, we will keep increasing i till we get the square root.
Solution steps
Pseudo-code
int getSquareRoot(int N)
{
if(N==1 or N==0)
return N //Square root of 1 is 1 and of 0 is 0
int i = 1
int square = 1
while(square is < or equal to N)
{
i+=1
square = i*i
}
return i-1
}
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(1)
Critical ideas to think!
Solution idea
The approach here is to stop checking for each and every possible element, instead we can ignore elements which are not going to yield the output based on an algorithm called as Binary Search. We will find a middle element by taking start as 1 and end as N. We will check the value of the square of the middle element. If the value is less than N, we will make start as middle element+1, else if the value is greater we will make end element as middle element-1. Also, we need to save the value of middle element in the first case as if the given integer is not a perfect square that can be a possible answer. In the case when the value is equal to N, we can output the value of middle element as it is the required square root.
Solution steps
Pseudo-code
int getSquareRoot(int N)
{
if(N == 0 or N ==1)
return N
int start = 1
int end = N
int squareRoot = 1
while(start <= end)
{
int mid = (start+end)/2
if((mid*mid) == N)
return mid
if((mid*mid) < N)
{
start = mid+1
squareRoot = mid
}
else
end = mid-1
}
return squareRoot
}
Complexity Analysis
Time Complexity: O(LogN)
Space Complexity: O(1)

Happy Coding! Enjoy Algorithms!!
AfterAcademy Tech
Given an array, find the smallest missing positive integer.

AfterAcademy Tech
Given two integer arrays A[] and B[] of size m and n, respectively. We need to find the intersection of these two arrays. The intersection of two arrays is a list of distinct numbers which are present in both the arrays. The numbers in the intersection can be in any order.

AfterAcademy Tech
Write a program that, Given an array of n integers and given a number K, determines whether or not there exist two elements whose sum is exactly K.

AfterAcademy Tech
You are given an array A[] consisting of n elements. You need to find and return the number which appears more than n/2 times.
