Given a piece of code, explain why it is wrong and how it could be improved.
Asked in the technical interview stage. Reported by a hired USAA SWE as part of their interview.
You are given a Python function that is intended to return the first duplicate value in a list, or -1 if no duplicate exists.
def first_duplicate(nums):
seen = []
for i in range(len(nums)):
if nums[i] in seen:
return nums[i]
seen.append(nums[i])
return 0
Write a corrected version of the function and explain what is wrong with the original code.
nums: a list of integers-1 if there is no duplicate.def first_duplicate(nums):