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?

- onsite December 22, 2017 | Flag Reply
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.

- Dhruv December 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the problem statement?

- Nitin December 26, 2017 | Flag Reply
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;
  }

- amid.salaad January 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question can be solved using a Priority queue.

- PJ March 21, 2018 | Flag Reply
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())

- Anonymous July 06, 2018 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More