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.

- Saket Goyal February 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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}

- Marcello Ghali February 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Saket Goyal February 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous February 19, 2015 | Flag
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

- Mikeo February 19, 2015 | Flag
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,

- kinsonljc February 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- sqf February 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's a design for load balancing.

- gwc February 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

graph? every 2 files make an edge

- lewie February 16, 2015 | Flag Reply
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;
	}

- Moses Wang February 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- nano February 17, 2015 | Flag
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).

- Idealdo FĂ©lix February 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- idealdofelix February 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- zaqwes8811 February 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a;

- Anonymous February 18, 2015 | Flag Reply
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?

- Arnie February 18, 2015 | Flag Reply
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
}

- Anonymous March 08, 2015 | Flag Reply
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;
        
    }

- AK March 26, 2015 | Flag Reply
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;
        
    }

- AK March 26, 2015 | Flag Reply
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[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);
	}
}

- llwire August 03, 2015 | Flag Reply
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[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);
	}
}

- llwire August 03, 2015 | 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