Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
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}
The files need to be sorted in ascending order of their sizes.
So, the files will be sorted as { 15, 20, 50, 70, 90 }
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
never place more than 2 files in a tape, is that right ?
or never place same files in a tape,
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
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;
}
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).
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.
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?
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[0];
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);
}
}
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[0];
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);
}
}
This problem can be solved as follows:
- Saket Goyal February 16, 20151) 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.