List of questions which were asked during the online test
1. Number of Unique Characters
Problem Statement:
You are required to implement the following function:
NumberUniqueCharacter(str)
The function accepts a string ‘str’ as its argument. You are required to calculate the number of unique characters in string ‘str’ and return the same.
Note:
If ‘str’=NULL, return -1
Assumptions:
- length of ‘str’ <=100000 characters
- ‘str’ contains only lower case alphabets.
Example:
Input
str: abcdabc
Output:
4
Explanation:
The string ‘str’ is “abcdabc”, and unique characters in string ‘str’ are {‘a’,’b’,’c’,’d’}, count of them is 4, hence 4 is returned.
Sample Input
str: efeefhjg
Sample Output
5
Solution:
Method 1
def NumberUniqueCharacter(str):
count=0
temp=""
if str=="":
return -1
for i in str:
if i not in temp:
temp+=i
count+=1
return count
str="abcdabc"
print(NumberUniqueCharacter(str))
Method 2
def NumberUniqueCharacter(str):
if str=="":
return -1
str=set(str)
return len(str)
str="abcdabc"
print(NumberUniqueCharacter(str))
2. Maximum Index Product
Problem Statement
Implement the following function:
MaxIndexProduct(arr,n)
The function accepts an integer array ‘arr’ of size n. Implement the function to find and return maximum index product. For every index ‘j’, index product =Left(j) x Right(j). Left(j)=an index ‘k’ which is closest to index ‘j’ ,such that k<j and arr[k] > arr[j], if no such index ‘k’ exist then Left(j) = 0. Right(j) = an index ‘l’ which is closest to index ‘j’, such that l > j and srr[l] > arr[j], if no such index ‘I’ exists then Right(j) = 0.
Assumption:
Element at index 0 is the smallest.
Note:
- Return -1 if the array is empty(or None in the case of Python).
- Indexing starts from 0.
Example:
Input
arr: -3 4 3 6 4 5 -2
Output
15
Explanation:
Index Product:
- Index product of index 0 = 0 x 1 = 0
- Index product of index 1 = 0 x 3 = 0
- Index product of index 2 = 1 x 3 = 3
- Index product of index 3 = 0 x 0 = 0
- Index product of index 4 = 3 x 5 = 15
- Index product of index 5 = 3 x 0 = 0
- Index product of index 6 = 5 x 0 = 0
The maximum index product is 15. Thus, the output is 15.
Sample Input
arr: 1 5 4 3 5 2
Sample Output
8
Solution
def maxIndexProduct(arr,n):
product=0
if n==0:
return -1
for j in range(1,n-1):
left=0
right=0
for k in range(j-1,-1,-1):
if arr[k]>arr[j]:
left=k
break
for l in range(j+1,n):
if arr[l]>arr[j]:
right=l
break
if (left*right>product):
product=left*right
return product
ls=[int(x) for x in input().split()]
n=len(ls)
print(maxIndexProduct(ls,n))
Amazon Software Development Engineering Hiring Coding Question & Answer!
Pingback: Uber hiring 2021 coding questions and answers! - CodeScript
Pingback: HCL Recruiting Fresher Engineers Across India! - CodeScript