## Amazon Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
6
of 8 vote

This problem can be solved as follows:
1) Sort the files on their size.
2) Maintain a counter to count how many tapes are needed.
3) Maintain two pointers, one on first element and another on last element, now add the sizes of the elements pointed by two pointers
Now two cases will arise:
a) If the sum is less than the tape size increment the tape count, increment the left point and decrement the right pointer.
b) If the sum is greater than the tape size then increment the tape count and only decrement the right pointer.

Check to see if left and right pointer are coincident or crossed each other to break this loop.
The tape counter will carry the number of tapes we would need.
Complexity O(nlogn) for sorting the file sizes.

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

you need to specify how you sort file sizes asc. or desc. order. Using below order, algorithms doesn't work: {90, 70, 50, 20, 15}

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

The files need to be sorted in ascending order of their sizes.
So, the files will be sorted as { 15, 20, 50, 70, 90 }

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

Minor optimization: if the right pointer is on a file size that is less or equal to half of the tape size then the remaining tape count is (rightPointer - leftPointer + 1) / 2 rounded up

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

Minor optimization: if the right pointer is on a file who's size is less or equal to the tape size the the remaining tape count is (rightPointer - leftPointer +1) /2 rounded up

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

never place more than 2 files in a tape, is that right ?
or never place same files in a tape,

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

I think it's a question of load balancing, you should not put more than two files on the same tape. it's a popular question for distributed system design

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

It's a design for load balancing.

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

graph? every 2 files make an edge

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

This should be a very simple question

``````public int minimumTapeCount(List<Integer>files, int tapeCapacity){
Collections.sort(files, new Comparator<Integer>() {
public int compare(Integer t1, Integer t2){
return (t2.intValue() - t1.intValue());
}
});

int count = 0;

while(files.size() > 0){
// get largest file first.
int largest = files.remove(0);
int second = 0;
int lastFileIndex = files.size() - 1;
if(lastFileIndex >= 0 ){
if((tapeCapacity - largest) >= files.get(lastFileIndex)){
second = files.remove(lastFileIndex);
}
}
count++;
}
return count;
}``````

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

I guess this should work, yet it may not need a List to do so, two pointers will work

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

Solution. Using algorithm design.

1.- Sort the files on their size. Using an algorith of O(n log n) complexity.
2.- From the biggest actual file do binary search in the array of file sizes adding another file size such that the sum is the maximum possible value under the tape capacity, when done repeat with the remaining files. This would take O(log n). Repeat n times, at most. Worst case complexity, O(n log n).

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

I forget to say O(log n) by each iteration while finding the maximum sum. As you repeat this at most n times, the complexity is n * O(log n) = O (n log n). Also you had the sorting complexity, so it would take you O(n log n) + O(n log n) = O(n log n). As you can see, this is the idea from a theoretical point of view.

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

May be here more important non calculation efficient but optimality of result.

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

``int a;``

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

This question is the Bin Packing Problem, modified to have a maximum number of objects per container. Bin Packing usually requires a heuristic algorithm because there exists no guaranteed optimality, but is it possible to have an optimal solution for this variant because of the max object per container clause?

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

1) count number is file size is >50 ==> m
2) file size <50 ==> n = total file size - m
if (m>n) then the answer is m
if(n>m) {
if((n-m)%2==1)
then the answer is m + n/2+1
else
the answer is m + n/2
}

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

``````public static Integer findCapacity(List<Integer> files, int tapeSize){
int fileCapacity = tapeSize/2;
Integer filesSum = 0;
for(Integer i : files){
filesSum += i;
}
return filesSum/fileCapacity;

}``````

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

``````public static Integer findCapacity(List<Integer> files, int tapeSize){
int fileCapacity = tapeSize/2;
Integer filesSum = 0;
for(Integer i : files){
filesSum += i;
}
return filesSum/fileCapacity;

}``````

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

``````public class Tapes {

public static void main(String[] args){
int tapeSize = 100;
int [] fileSizes = {70, 10, 35, 30, 5, 52, 22, 91, 100};
int end = fileSizes.length - 1;
quickSort(fileSizes, 0, end);
for(int i=0; i < fileSizes.length; i++){
System.out.println(fileSizes[i]);
}
int count = numberOfTapes(tapeSize, fileSizes);
System.out.println("Count: " + count);
}

public static int numberOfTapes(int tapeSize, int[] fileSizes){
if(fileSizes.length <= 1) return fileSizes.length;
int tapeCount = 0;
int baseCount = (fileSizes.length/2) + (fileSizes.length%2);
int minSize = fileSizes;
int end = fileSizes.length - 1;
for(int i= end; i > baseCount; i--){
System.out.println("End Elem: " + fileSizes[i]);
if( fileSizes[i] + minSize <= tapeSize){
break;
}
tapeCount++;
}
tapeCount += baseCount;
return tapeCount;
}

public static void swap(int[] input, int source, int destination){
int temp = input[source];
input[source] = input[destination];
input[destination] = temp;
}
public static int partition(int[] input, int start, int end){
int handle = input[end];
int pivot = 0;
int j = start - 1;
for(int i = start; i < end; i++){
if(input[i] <= handle){
j++;
swap(input, j, i);
}
}
j++;
swap(input, j, end);
pivot = j;
return pivot;
}
public static void quickSort(int[] input, int start, int end){
if(end - start <= 0) return;

int pivot = partition (input, start, end);
quickSort(input, start, pivot - 1);
quickSort(input, pivot + 1, end);
}
}``````

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

``````public class Tapes {

public static void main(String[] args){
int tapeSize = 100;
int [] fileSizes = {70, 10, 35, 30, 5, 52, 22, 91, 100};
int end = fileSizes.length - 1;
quickSort(fileSizes, 0, end);
for(int i=0; i < fileSizes.length; i++){
System.out.println(fileSizes[i]);
}
int count = numberOfTapes(tapeSize, fileSizes);
System.out.println("Count: " + count);
}

public static int numberOfTapes(int tapeSize, int[] fileSizes){
if(fileSizes.length <= 1) return fileSizes.length;
int tapeCount = 0;
int baseCount = (fileSizes.length/2) + (fileSizes.length%2);
int minSize = fileSizes;
int end = fileSizes.length - 1;
for(int i= end; i > baseCount; i--){
System.out.println("End Elem: " + fileSizes[i]);
if( fileSizes[i] + minSize <= tapeSize){
break;
}
tapeCount++;
}
tapeCount += baseCount;
return tapeCount;
}

public static void swap(int[] input, int source, int destination){
int temp = input[source];
input[source] = input[destination];
input[destination] = temp;
}
public static int partition(int[] input, int start, int end){
int handle = input[end];
int pivot = 0;
int j = start - 1;
for(int i = start; i < end; i++){
if(input[i] <= handle){
j++;
swap(input, j, i);
}
}
j++;
swap(input, j, end);
pivot = j;
return pivot;
}
public static void quickSort(int[] input, int start, int end){
if(end - start <= 0) return;

int pivot = partition (input, start, end);
quickSort(input, start, pivot - 1);
quickSort(input, pivot + 1, end);
}
}``````

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.