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?