gravityrahul
BAN USER
- 5of 5 votes
AnswersI recently, appeared for second phone interview with CloudEra, the interviewer asked me to
- gravityrahul in United States
write a function which takes in an array of integers and returns the highest positive product
possible by multiplying 3 distinct numbers. NO SORTING is ALLOWED
PS: Please write a solution in JAVA or Python. (Not interviewer request)
example:
[1, 3, 5, 2, 8, 0, -1, 3]
=> 8 * 5 * 3 = 120
[0, -1, 3, 100, -70, -5]
=> -70*-50*100=350000| Report Duplicate | Flag | PURGE
Cloudera Software Engineer in Test Algorithm
Please clear my doubt. Are all arrangements unique? What I mean it consider a 2x3 board, there can be 2 unique ways of arranging 2x1 tiles. However, you can also do another ways if you consider all arrangements unique.
_____
| |___|
I_ |___| is same as
_____
|___| |
|___| _|
Here's a short Implementation in Python
count = 0
def MaximumRepeatingInteger(arrayofnumber):
global count
dict_of_counts = {}
for integer in arrayofnumber:
if dict_of_counts.has_key(str(integer)):
count +=1
dict_of_counts[str(integer)]=count
else:
count = 1
dict_of_counts[str(integer)]=count
return max(dict_of_counts.values())
number = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5]
print MaximumRepeatingInteger(number) : 14
number=[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5]
print MaximumRepeatingInteger(number) : 21
Moreover there should be a global way of storing them, you may want to remember the earlier state of the system before actually executing the newer values. Perform sufficient checks to ensure that pressure and speed are always positive at any given time (real time scenario). If the state of the system changes to negative values after performing the operation we may simple raise error.
- gravityrahul January 02, 2014Changed the algorithm to Python Dictionary as per your suggestion
def FindString(String, subString):
pLen = len(subString)
pString = ''
found = False
pHashTable={}
pHashTable[subString]=subString
if pLen > 0:
for i in range(len(String)):
pString = String[i:i+pLen]
if pHashTable.has_key(pString):
return True
return found
def PrintMatch(String, subString):
if FindString(String, subString):
print "Found %s inside %s" %(subString, String)
else:
print "No match Found. "
PrintMatch('bottle', 'tl')
PrintMatch("dceababac", "abac")
Found tl inside bottle
Found abac inside dceababac
def FindString(String, subString):
pLen = len(subString)
pString = ''
found = False
if pLen > 0:
for i in range(len(String)):
pString = String[i:i+pLen]
if pString == subString:
return True
return found
def PrintMatch(String, subString):
if FindString(String, subString):
print "Found %s inside %s" %(subString, String)
else:
print "No match Found. "
PrintMatch('bottle', 'tl')
PrintMatch("dceababac", "abac")
Found tl inside bottle
Found abac inside dceababac
RepSherriMooney, Network Engineer at Arista Networks
I am not a model but as a photographer, I can't see how one could call it fun. Matter ...
Repdianamowery95, Consultant at Delve Networks
My Name is Diana Mowery. I am from St. Louis and received a Bachelor Degree and My Masters Degree from ...
Repman254183, Project Leader at GoDaddy
I am working as Human Resources Associates, and my duties are for obtaining, recording, and interpreting human resources information within ...
Strt with two pointers. One slow and anothoer Fast. Below is the code in Python.
Output:
- gravityrahul June 07, 2014Inserting num = 70
Inserting num = 60
Inserting num = 50
Inserting num = 30
Inserting num = 20
Inserting num = 10
=================================
Displaying elements in LinkedList
Current element is [10]
Current element is [20]
Current element is [30]
Current element is [50]
Current element is [60]
Current element is [70]
=================================
The Length of LinkedList is 6
=================================
Inserting 40 in LInkedList
Displaying elements in LinkedList
Current element is [10]
Current element is [20]
Current element is [30]
Current element is [40]
Current element is [50]
Current element is [60]
Current element is [70]