Coding – Interview Questions
Assignment – 1
(Source: Geeksforgeeks.org)
1. Given an array Arr of size N, print second largest distinct element from an array.
(SAP Labs, Rockstand)
Example:
Input: arr[] = {12, 35, 1, 10, 34, 1}
Output: The second largest element is 34.
Explanation: The largest element of the
array is 35 and the second
largest element is 34
Input: arr[] = {10, 10, 10}
Output: The second largest does not exist.
Explanation: Largest element of the array
is 10 there is no second largest element
2. Given a sorted array A[] of size N, delete all the duplicated elements from A[]. Modify the array
such that if there are X distinct elements in it then the first X positions of the array should be filled
with them in increasing order and return the number of distinct elements in the array.
(Zoho, Morgan Stanley, Microsoft, Samsung, Google, Wipro, Xome)
Examples:
Input: arr[] = {2, 2, 2, 2, 2}
Output: arr[] = {2}
Explanation: All the elements are 2, So only keep one instance of 2.
Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output: arr[] = {1, 2, 3, 4, 5}
3. Given an array of N integers, and an integer K, find the number of pairs of elements in the array
whose sum is equal to K.
(Flipkart, Accolite, Amazon, FactSet, Hike, MakeMyTrip, Goldman Sachs, Adobe,
Salesforce)
Example 1:
Input:
N = 4, K = 6
arr[] = {1, 5, 7, 1}
Output: 2
Explanation:
arr[0] + arr[1] = 1 + 5 = 6
and arr[1] + arr[3] = 5 + 1 = 6.
Example 2:
Input:
N = 4, K = 2
arr[] = {1, 1, 1, 1}
Output: 3
Explanation:
Each 1 will produce sum 2 with any 1.
4. Given two numbers A and B, find Kth digit from right of AB.
(Flipkart)
Example:
Input:
A=5
B=2
K=2
Output:
2
Explanation:
52 = 25 and second digit from right is 2.
5. Given a number N, the task is to find the largest prime factor of that number.
(Yahoo)
Example:
Input:
N = 24
Output:
3
Explanation:
24 has 2 prime factors
3 and 2 in which 3 is Greater
6. Given a positive integer n, find the nth fibonacci number. Since the answer can be very large,
return the answer modulo 1000000007. Amazon
(Microsoft, OYO Rooms, Snapdeal, MakeMyTrip, Goldman Sachs, MAQ Software, Adobe)
Example 1:
Input:
n=2
Output:
1
Explanation:
1 is the 2nd number of fibonacci series.
Example 2:
Input:
n=5
Output:
5
Explanation:
5 is the 5th number of fibonacci series
7. Consider a sample space S consisting of all perfect squares starting from 1, 4, 9 and so on. You are
given a number N, you have to output the number of integers less than N in the sample space S.
Example:
Input: N = 36
Output: 5
Numbers are 4 = 12 * 22,
9 = 12 * 32,
16 = 12 * 42,
25 = 12 * 52,
36 = 12 * 62
Input:
N=9
Output:
2
Explanation:
1 and 4 are the only Perfect Squares
less than 9. So, the Output is 2.
Input: N = 1000000
Output: 999
8. Given an array a of size N which contains elements from 0 to N-1, you need to find all the
elements occurring more than once in the given array. Return the answer in ascending order. If no
such element is found, return list containing [-1].
(Paytm, Zoho , Flipkart, Amazon, D-E-Shaw, Qualcomm)
Note: The extra space is only for the array to be returned. Try and perform all operations within the
provided array.
Example 1:
Input:
N=4
a[] = {0,3,1,2}
Output:
-1
Explanation:
There is no repeating element in the array. Therefore output is -1.
Example 2:
Input:
N=5
a[] = {2,3,1,2,3}
Output:
23
Explanation:
2 and 3 occur more than once in the given array.
9. Given two positive numbers X and Y, check if Y is a power of X or not.
(Amazon, Housing.com)
Example:
Input:
X = 2, Y = 8
Output:
1
Explanation:
23 is equal to 8.
10. Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find
the missing element.
(Flipkart, Morgan Stanley, Accolite, Amazon, Microsoft, D-E-Shaw, Ola Cabs, Payu, Visa,
Intuit, Adobe, Cisco, Qualcomm )
Example 1:
Input:
N=5
A[] = {1,2,3,5}
Output: 4
Example 2:
Input:
N = 10
A[] = {6,1,2,8,3,4,7,10,5}
Output: 9
11. Given a string S consisting of lowercase Latin Letters. Return the first non-repeating character in
S. If there is no non-repeating character, return '$'.
(Flipkart, Amazon, Microsoft, D-E-Shaw, MakeMyTrip, Ola Cabs, Payu, Teradata, Goldman
Sachs, MAQ Software, InfoEdge, OATS Systems, Tejas Network)
Example:
Input:
S = hello
Output: h
Explanation: In the given string, the first character which is non-repeating is h, as it appears first and
there is no other 'h' in the string.
12. For an integer N find the number of trailing zeroes in N!.
(Microsoft, MakeMyTrip)
Example 1:
Input:
N=5
Output:
1
Explanation:
5! = 120 so the number of trailing zero is 1.
Example 2:
Input:
N=4
Output:
0
Explanation:
4! = 24 so the number of trailing zero is 0.
13. Given a number N. Find if it can be expressed as sum of two prime numbers.
(Zoho)
Example 1:
Input: N = 34
Output: "Yes"
Explanation: 34 can be expressed as sum of two prime numbers.
Example 2:
Input: N = 23
Output: "No"
Explanation: 23 cannot be expressed as sum of two prime numbers.
14. Given two numbers in the form of Strings. Find the count of carries in their addition.
Example 1:
Input:
N = "34", M = "25"
Output:
0
Explanation:
There are no carries when N and M are added.
Example 2:
Input:
N = "4634", M = "1757"
Output:
2
Explanation:
There are 2 carries when N and M are added
15. Given a number N. Find out the nearest number which is a perfect square and also the absolute
difference between them.
Example 1:
Input:
N = 25
Output:
25 0
Explanation:
Since 25 is a perfect square, it is the closest perfect square to itself and absolute difference is
25-25=0.
Example 2:
Input:
N = 1500
Output:
1521 21
Explanation:
Two of the closest perfect squares to 1521 are 1444.Thus, 1521 is the closest perfect square to 1500
and the absolute difference between them is 1521-1500=21.
16. Ishaan being curious as always was playing with numbers when he started to reverse the
numbers. He invented something called the "inverting factor" of two numbers.
The inverting Factor of 2 integers is defined as the absolute difference between the reverse of the 2
integers.
Ishaan has an array A of N integers. He wants to find out the smallest possible Inverting Factor
among all pairs of his integers. Help him find that.
Note: Trailing zeros in a number of ignored while reversing, i.e. 1200 becomes 21 when reversed.
Example 1:
Input: arr[ ] = [56, 20, 47, 93, 45]
Output: 9
Explanation:
The minimum inverting factor is 9, of the pair (56,47)
Reverse 56 -> 65 and 47 -> 74
difference between them is 9.
Example 2:
Input: arr[ ] = [48, 23, 100, 71, 56, 89]
Output: 14
Q.17 Given a positive integer N, find the sum of all prime numbers between 1 and N(inclusive).
(Samsung, Adobe)
Example 1:
Input: N = 5
Output: 10
Explanation: 2, 3, and 5 are prime
numbers between 1 and 5(inclusive).
Example 2:
Input: N = 10
Output: 17
Explanation: 2, 3, 5 and 7 are prime
numbers between 1 and 10(inclusive).
Q.18 Given a string N representing a positive number. The task is to round N to the nearest multiple
of 10.
(Microsoft)
Note: If you are having two multiples equally apart from N then we will choose the smallest element
among them.
Example 1:
Input : N = 15
Output : 10
Explanation:
Here N is 15. So, the number which is
multiple of 10 are 10, 20, 30. We can
see 10 and 20 are equally distant
from 20. So, we will choose the
smallest element among them i.e., 10.
Example 2:
Input : N = 29
Output : 30
Q.19 Given an integer N which has odd number of digits, find whether the given number is a
balanced or not.
An odd digit number is called a balanced number if the sum of all digits to the left of the middle digit
and the sum of all digits to the right of the middle digit is equal.
Example 1:
Input:
N = 1234006
Output: 1
Explanation: Total number of digits in
N = 7. Middle digit is 4. LHS = 1+2+3 = 6
and RHS = 0+0+6 = 6. LHS is equal to RHS.
Example 2:
Input:
N = 12345
Output: 0
Explanation: LHS = 1+2 = 3 and RHS = 4+5
= 9. LHS and RHS are not equal.
Q.20 Given a number N. Find its unique prime factors in increasing order.
(Yahoo)
Example 1:
Input: N = 100
Output: 2 5
Explanation: 2 and 5 are the unique prime
factors of 100.
Example 2:
Input: N = 35
Output: 5 7
Explanation: 5 and 7 are the unique prime
factors of 35.
Q.21 Given a number 'N'. The task is to find the Nth number whose each digit is a prime number i.e
2, 3, 5, 7. In other words you have to find nth number of this sequence: 2, 3, 5, 7, 22, 23 ,.. and so on.
Example 1:
Input:
N = 10
Output: 33
Explanation:10th number in the sequence of
numbers whose each digit is prime is 33.
Example 2:
Input:
N = 21
Output: 222
Explanation:21st number in the sequence of
numbers whose each digit is prime is 222.
Q.22 Given a number positive number N, find value of f0 + f1 + f2 + …. + fN where fi indicates i’th
Fibonacci number.
(Goldman Sachs)
Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, …
Example 1:
Input:
N=3
Output:
4
Explanation:
0+1+1+2 =4
Example 2:
Input :
N=4
Output :
7
Explanation :
0+1+1+2+3=7
Q.23 You are given two integer numbers, the base a and the index b. You have to find the last digit
of ab.
(Samsung)
Example 1:
Input:
a = "3", b = "10"
Output:
9
Explanation:
310 = 59049. Last digit is 9.
Example 2:
Input:
a = "6", b = "2"
Output:
6
Explanation:
62 = 36. Last digit is 6.
Q.24 Given an array A of n positive numbers. The task is to find the first equilibrium point in an
array. Equilibrium point in an array is a position such that the sum of elements before it is equal to
the sum of elements after it.
(Amazon, Adobe)
Note: Return equilibrium point in 1-based indexing. Return -1 if no such point exists.
Example 1:
Input:
n=5
A[] = {1,3,5,2,2}
Output:
3
Explanation:
equilibrium point is at position 3 as sum of elements before it (1+3) = sum of elements after it (2+2).
Example 2:
Input:
n=1
A[] = {1}
Output:
1
Explanation:
Since there's only element hence its only the equilibrium point.
Q.25 Given two numbers A and B. The task is to find out their LCM and GCD.
Q.26 Given an unsorted array A of size N that contains only positive integers, find a continuous sub-
array that adds to a given number S and return the left and right index(1-based indexing) of that
subarray.
In case of multiple subarrays, return the subarray indexes which come first on moving from left to
right.
(Amazon, Facebook, Google, Visa)
Example 1:)
Input:
N = 5, S = 12
A[] = {1,2,3,7,5}
Output: 2 4
Explanation: The sum of elements
from 2nd position to 4th position
is 12.
Example 2:
Input:
N = 10, S = 15
A[] = {1,2,3,4,5,6,7,8,9,10}
Output: 1 5
Explanation: The sum of elements
from 1st position to 5th position
is 15.
Q.27 Given an array A of positive integers. Your task is to find the leaders in the array. An element of
array is leader if it is greater than or equal to all the elements to its right side. The rightmost element
is always a leader.
(Payu, Adobe)
Example 1:
Input:
n=6
A[] = {16,17,4,3,5,2}
Output: 17 5 2
Explanation: The first leader is 17
as it is greater than all the elements
to its right. Similarly, the next
leader is 5. The right most element
is always a leader so it is also
included.
Example 2:
Input:
n=5
A[] = {1,2,3,4,0}
Output: 4 0
Explanation: 0 is the rightmost element
and 4 is the only element which is greater
than all the elements to its right.
Q.28 You are given a list of q queries and for every query, you are given an integer N. The task is to
find how many numbers (less than or equal to N) have number of divisors exactly equal to 3.
Examples:
Input: N = 16
Output: 4 9
Explanation: 4 and 9 have exactly three divisors.
Input: N = 49
Output: 4 9 25 49
Explanation: 4, 9, 25 and 49 have exactly three divisors.
Input:
q=1
query[0] = 6
Output:
1
Explanation:
There is only one number 4 which has exactly three divisors 1, 2 and 4 and less than equal to 6.
Q.29 Given an expression string x. Examine whether the pairs and the orders of {,},(,),[,] are correct
in exp.
For example, the function should return 'true' for exp = [()]{}{[()()]()} and 'false' for exp = [(]).
(Flipkart, Amazon, Microsoft, OYO Rooms, Snapdeal, Oracle, Walmart, Adobe, Google,
Yatra.com)
Note: The drive code prints "balanced" if function return true, otherwise it prints "not balanced".
Example 1:
Input:
{([])}
Output:
true
Explanation:
{ ( [ ] ) }. Same colored brackets can form
balanced pairs, with 0 number of
unbalanced bracket.
Example 2:
Input:
()
Output:
true
Explanation:
(). Same bracket can form balanced pairs,
and here only 1 type of bracket is
present and in balanced way.
Example 3:
Input:
([]
Output:
false
Explanation:
([]. Here square bracket is balanced but the small bracket is not balanced and Hence , the output will
be unbalanced)
Q.30 Given a Binary Number B, find its decimal equivalent.
(Adobe)
Examples :
Input : 111
Output : 7
Input : 1010
Output : 10
Input: 100001
Output: 33