I have been interviewed recently by an eminent silicon valley based firm . I had been asked to write the code for the following question with O(n) time complexity .
Question :
Write a function that is given an array of integers. It should return true if
any value appears at least twice in the array, and it should return false if
every element is distinct.
The crux to the question was to use hash maps . I wrote the following code in python :
def containsDuplicate(arr):
hashi={}
for i in arr:
try:
hashi[i]+=1
return 1
except KeyError:
hashi[i]=1
return 0
Overall interview went well and I was able to figure out the best solutions (efficient solutions) of all the questions asked .
Later I got a mail from the company that I was not the best match for the position.When asked the reason for my rejection I was told to work on my coding style .
What does the HR mean when she said that I need to improve on my coding style ? What would be the best way to code the solution to the problem asked above ?
Edit :
After reading the code again I believe it was not even necessary to do hashi[i]+=1
. Just return 1
was sufficient but I don't think that they rejected me solely due to this minor fault in my coding.
Copyright Notice:Content Author:「tusharmakkar08」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21719874/coding-style-in-python