Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
464 views
in Online Assessments by Expert (46,090 points) | 464 views

1 Answer

0 like 0 dislike

I got this question in microsoft OA:

 

The following find_min() is incorrect:

 

def find_min(A):
ans =0
for i in range(1, len(A)):
if A[i] < ans:
ans = A[i]
return ans

 

Given an integer N, generate a counter example ie an array A consisting of N integers for which find_min will give the wrong answer.
Ex: N=4, ans can be [4,2,4,5]

 

My Answer:
Since find_min initialises ans = 0, it will return 0 for any array with all positive element greater than or equal to 1. So given N, I am just generating an array of 1s of size N.

 

Did I understand the question right? Or is there some trick I am not getting?

by Expert (46,090 points)