Amazon Interview Question for SDE1s


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
9
of 11 vote

How about this approach?

With 4 placed in units place, there are 3! permutations of 123 in thousandths, hundredths and tenths place. That means 4 occurs in units place 3! times.

Similarly 3 2 and 1 each occur in units place 3! times. The sum of all numbers in units place is therefore 3!(4 + 3 + 2 + 1) = 6 * 10 = 60.

The same value happens to occur in tenths, hundredths and thousandths place as well, except that it gets multiplied by a power of 10 according to its position.

So, the total should be 60 + 600 + 6000 + 60000 = 66660.

- Murali Mohan June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

Yet another approach is to form pairs of numbers from 4!(=24) permutations. Pairing should happen in such a way that at a given position, the maximum digit of one number should match with the minimum digit of the other number, the second maximum digit should match with the second minimum and so on.. For ex:

Pair up 1234 with 4321, 2134 with 3421 etc. The sum of each of these pairs is equal to 5555 and we have 12 pairs out of the total 24 permutations.

5555 * 12 = 66660

- Murali Mohan June 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 6 vote

A simple solution--

3!(4+3+2+1)(1+10+100+1000)..easy logic

- Anonymous June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Awesome...........

- Anonymous July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Here we don't have to do actual calculation, else we will never get answer in written test. Think of this as mathematical + logical question. Lets say we have all permutation of 1234. It will be 4! = 24. Now if we look at each column of this list carefully, every number will appear for 6 time.
1234
1324
2134
2314
3124
3214
...

Now if you just concentrate on 4th column, "4" is coming 6 times, same way if we list all number each number will come 6 time in 4th column. So sum of 4th column is (4*6 + 3*6 + 2*6 + 1*6) = 60.

Same way, for all other columns sum would be 60.

Now we are just one step far away from answer. And that is consider "carry". 4th column will give 6 as carry to 3rd columns, again in turn 3rd column will give 6 as carry to 2nd column and so on.

Therefore, final answer would be 66660.

- amitp49 June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Total no. of permutations = 4! = 24
Each digit 1,2,3 and 4 will occur exactly 6 times at each position.
Sum of digits at each position = (1+2+3+4)*6 = 60
Total sum
60000
6000
600
60
---------
66660 = Answer
--------

- Shikhar Sharad June 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Consider the unit's digit place,each of 4,3,2,1 appears 6 times each.So at the digit place ,this adds to (4+3+2+1)*6=60.Now 6 gets carried forward to the tens place.Similarly at ten's place,it adds to (4+3+2+1)*6+6(the carry)=66.Now again the 6 is carried forward till we get 66660.

- Sibendu Dey June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let assume we have n digits. Each of them will appears on i-th position n!/n times. So the general formula is :
n!/n = 6
(1 + 2 + 3 + 4) * 6 * 4 = 240

- gstepanov@griddynamics.com October 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

mind blowing...

- anil October 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its 4! i.e 4*3*2*1=24
now if we pair the number like 1234+4321=5555
2134+3421=5555
in every case we come across 5555
therefore there will be 12 pairs
hence 12*5555=66660

- shubham December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the java program which will print the combinations and sum of all combinations. Little more info apart from sum.

package com.bala.logical;

import java.util.ArrayList;
import java.util.List;

public class SumCombination {
static String finalString = "";

public static void main(String[] args) {
String s = "1234";
List<String> numList = new ArrayList<String>();
System.out.println(arrangeNumbers(s, numList));
System.out.println("Size of the elements "+numList.size());
int sum = 0;
for(String combination: numList){
sum = sum + Integer.parseInt(combination);
}
System.out.println(sum);
}

public static String[] swap(String s) {
String[] combinations = new String[s.length()];
if (s.length() == 1) {
combinations[0] = s;
return combinations;
}
for (int i = 0; i < s.length(); i++) {
combinations[i] = s.substring(1, s.length()) + s.substring(0, 1);
s = s.substring(1, s.length()) + s.substring(0, 1);
}
return combinations;
}

public static List<String> arrangeNumbers(String s, List<String> numberList) {
String[] combinations = swap(s);
for (int i = 0; i < combinations.length; i++) {
finalString = finalString + combinations[i].substring(0, 1);
if (combinations.length == 1) {
numberList.add(finalString);
} else {
String subS = combinations[i].substring(1,
combinations[i].length());
arrangeNumbers(subS, numberList);
}
finalString = finalString.substring(0, finalString.length() - 1);
}
return numberList;
}
}

- Anonymous December 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

it can be done by finding all permutation of the given digits without rep, then add each numbers formed. there are totally 4! numbers to add.

Let number = 1234
let sum = 1234.

1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
2. Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l.
3. Swap a[k] with a[l].
4. Reverse the sequence from a[k + 1] up to and including the final element a[n].
5. sum = sum + newly formed number on step 4.

- Vin June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

A simple solution--

3!(4+3+2+1)(1+10+100+1000)..easy logic

- Amit June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is the java program which will print the combinations and sum of all combinations. Little more info apart from sum.

package com.bala.logical;

import java.util.ArrayList;
import java.util.List;

public class SumCombination {
	static String finalString = "";

	public static void main(String[] args) {
		String s = "1234";
		List<String> numList = new ArrayList<String>();		
		System.out.println(arrangeNumbers(s, numList));
		System.out.println("Size of the elements "+numList.size());
		int sum = 0;
		for(String combination: numList){
			sum = sum + Integer.parseInt(combination);
		}
		System.out.println(sum);
	}

	public static String[] swap(String s) {
		String[] combinations = new String[s.length()];
		if (s.length() == 1) {
			combinations[0] = s;
			return combinations;
		}
		for (int i = 0; i < s.length(); i++) {
			combinations[i] = s.substring(1, s.length()) + s.substring(0, 1);
			s = s.substring(1, s.length()) + s.substring(0, 1);
		}
		return combinations;
	}

	public static List<String> arrangeNumbers(String s, List<String> numberList) {
		String[] combinations = swap(s);
		for (int i = 0; i < combinations.length; i++) {
			finalString = finalString + combinations[i].substring(0, 1);
			if (combinations.length == 1) {
				numberList.add(finalString);
			} else {
				String subS = combinations[i].substring(1,
						combinations[i].length());
				arrangeNumbers(subS, numberList);
			}
			finalString = finalString.substring(0, finalString.length() - 1);
		}
		return numberList;
	}
}

- Balakrishna Konduri July 07, 2013 | 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