Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
871 views
in Online Assessments by Expert (44,360 points)

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 (44,360 points)
...