## Amazon Interview Question

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the question?

Comment hidden because of low score. Click to expand.
0
of 0 vote

We can store in a boolean array, the staus (occupied / free) of every toilet. Find the distance between every pair of consecutive occupied toilets & report the mean of the indices of the pair with the maximum distance.

Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the problem statement?

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````static int sumOfOccupiedToilets(int toilets, int people) {
if (people >= toilets) {
return (toilets * (1 + toilets)) / 2;
}

int[] currentDistances = new int[toilets];
Arrays.fill(currentDistances, Integer.MAX_VALUE);
int sum = 0;
int maxDistIndex = 0;

for (int i = 0; i < people; i++) {
int pos = maxDistIndex;
currentDistances[pos] = 0;
sum += pos + 1;
maxDistIndex = 0;
for (int j = 0; j < currentDistances.length; j++) {
currentDistances[j] = Math.min(Math.abs(pos - j), currentDistances[j]);
if (maxDistIndex < currentDistances[j]) {
maxDistIndex = currentDistances[j];
}
}
}
return sum;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

This question can be solved using a Priority queue.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````import sys

def getToiletSum(t, p):
if p >= t:
return (t * (t + 1)) / 2

row = [sys.maxsize] * t
result = 0
currIndex = 0
for _ in range(p):
result += (currIndex + 1)
row[currIndex] = 0
maxDistIndex = 0
maxDist = 0
for i in range(len(row)):
row[i] = min(abs(i - currIndex), row[i])
if maxDist < row[i]:
maxDist = row[i]
maxDistIndex = i
currIndex = maxDistIndex
return result

def main():
toilets = 5
ppl = 5

print "Running for t# " + str(toilets) + ", ppl# " + str(ppl)
s = getToiletSum(toilets, ppl)
print "sum: " + str(s)

if __name__ == '__main__':
sys.exit(main())``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.