Solving An Amazon Interview Puzzle - Sum of 5 digit numbers without repetition

preview_player
Показать описание
What is the sum of all 5 digit numbers formed from 1, 2, 3, 4, 5 without repetition? This is an old Amazon interview question, and versions of it also have been asked in exams.

CareerCup
Amazon Interview Question for SDE1s
Toppr

Send me suggestions by email (address at end of many videos). I may not reply but I do consider all ideas!

If you purchase through these links, I may be compensated for purchases made on Amazon. As an Amazon Associate I earn from qualifying purchases. This does not affect the price you pay.

If you purchase through these links, I may be compensated for purchases made on Amazon. As an Amazon Associate I earn from qualifying purchases. This does not affect the price you pay.

Book ratings are from January 2023.

My Books (worldwide links)

My Books (US links)
Mind Your Decisions: Five Book Compilation
A collection of 5 books:
"The Joy of Game Theory" rated 4.3/5 stars on 290 reviews
"The Irrationality Illusion: How To Make Smart Decisions And Overcome Bias" rated 4.1/5 stars on 33 reviews
"40 Paradoxes in Logic, Probability, and Game Theory" rated 4.2/5 stars on 54 reviews
"The Best Mental Math Tricks" rated 4.3/5 stars on 116 reviews
"Multiply Numbers By Drawing Lines" rated 4.4/5 stars on 37 reviews

Mind Your Puzzles: Collection Of Volumes 1 To 3
A collection of 3 books:
"Math Puzzles Volume 1" rated 4.4/5 stars on 112 reviews
"Math Puzzles Volume 2" rated 4.2/5 stars on 33 reviews
"Math Puzzles Volume 3" rated 4.2/5 stars on 29 reviews

2017 Shorty Awards Nominee. Mind Your Decisions was nominated in the STEM category (Science, Technology, Engineering, and Math) along with eventual winner Bill Nye; finalists Adam Savage, Dr. Sandra Lee, Simone Giertz, Tim Peake, Unbox Therapy; and other nominees Elon Musk, Gizmoslip, Hope Jahren, Life Noggin, and Nerdwriter.

My Blog

Twitter

Instagram

Merch

Patreon

Press
Рекомендации по теме
Комментарии
Автор

Perhaps I spoke too soon? Chat GPT-4 released on March 14 does give the correct answer...But it's logic is hilariously wrong: the numbers are clearly not in an arithmetic sequence! It's a shock to me that the calculation coincidentally gives the correct answer...wow.

GPT-4 response
To find the sum of all 5-digit numbers formed from the digits 1, 2, 3, 4, 5 without repetition, we need to first determine how many such numbers there are. Since we are selecting from a set of 5 digits without repetition, there are 5 choices for the first digit, 4 choices for the second digit, 3 choices for the third digit, 2 choices for the fourth digit, and 1 choice for the fifth digit. Therefore, there are 5 × 4 × 3 × 2 × 1 = 120 different 5-digit numbers that can be formed using these digits.

To find their sum, we can use the formula for the sum of an arithmetic series:

S = (n/2)(a + l)

where S is the sum of the series, n is the number of terms in the series, a is the first term, and l is the last term.

In this case, the first term is 12, 345 (the smallest 5-digit number that can be formed using these digits), the last term is 54, 321 (the largest 5-digit number that can be formed using these digits), and the number of terms is 120.

Plugging these values into the formula, we get:

S = (120/2)(12, 345 + 54, 321) = 60 × 66, 666 = 3, 999, 960.

Therefore, the sum of all 5-digit numbers formed from 1, 2, 3, 4, 5 without repetition is 3, 999, 960.

MindYourDecisions
Автор

120 permutations. Each digit is equally likely to be 1, 2, 3, 4 or 5. Thus the "average digit" is 3, so the average number is
120 * = 3 999 960

NestanSvensk
Автор

I figured it out by thinking about the singles digit. There are 120 permutations, all five numbers go into the same spot the same amount of times, 1 through 5 are in the last spot 24 times each. 1 through 5 sum to fifteen, so for the 10^0 spot you get 15 * 24 = 360. The other four spots work the same way, so you get 360 + 3600 + 36000 + 360000 + = 3999960

donnathelightningbug
Автор

Hi Presh, really nice video! But I think that such interviewing question need a even faster and smarter solution and I think I figured it out.
We know that without digits repetition we can make 5! (120) differents numbers and we can pair each one with another in such a way that we get as the sum.
An example is 12345 with 54321 or 13524 with 53142.
So now we have 5!/2=60 pairs and their sum is just

Thank you again for your videos!

(PS:
Fun fact, I just noticed that if we multiply (with n 6 digits) times 6 we get 39999....9996 (with n-1 9 digits) and it's very simple to demonstrate. It isn't quite useful but beautiful)

samueleprandini
Автор

1) There are 120 numbers in the list to be summed, since there are 5 possibilities for the first digit, 4 for the second, etc.
2) Each of the digits 1-5 will appear in each of the positions an equal number of times (24 for each digit in each position)
3) Since the digits are in arithmetic sequence, the average value for each digit in each position is 3
4) The answer is therefore x 120, which, since and 1/3 x 120 is 4 million exactly, is 40 less: 3 999 960
Do I get the job?

brucemcintyre
Автор

I solved it in another way. First I found that there are 5!=120 elements in S. then I paired the numbers in a way where each pair has the same sum. For example, one of the pairs is (12345, 54321) the sum of these 2 numbers is Similarly, the sum of (12435, 54231) is also We can see that we choose the second number's digits corresponding to the first number's digits such that each pair of digits in the same position in both numbers add up to 6. Since we have 120 numbers, then there are 60 pairs. Multiplying this by the sum of each pair we get

josephram
Автор

-Amazon interviewer: "That answer is correct! Next question: How long can you go without needing to use the bathroom?"

jacktaylor
Автор

5! is 120. the average of 1, 2, 3, 4, 5 is 3 exactly. so the average combination of the five digits is 33, 333 exactly. multiple 33, 333 by 120 unique outcomes to get 3, 999, 960.

crossgibson
Автор

The Gauss method works, but a generic approach works as well, leaving out the multiplication with 11.111 for the last.
For any digit on any position there are 4! (24) ways to arrange the other digits.
Since we have 5 possible digits for each position and each occurs 24 times in a number, each position contributes the sum of digits times the occurrence = 15 * 24 = 360 to each position That's 3.600.000 + 360.000 + 36.000 + 3.600 + 360 = 3.999.960

barttemolder
Автор

I did mine by hand, literally. I used my fingers to discover that it was 5! permutations, but instead of adding the "values" of each iteration, I summed the digits and came up with 1800.

Then I saw the answer as being nearly 4 000 000 and realized that I had read the problem and misunderstood it. If I were to have understood it correctly, I would have used either a spreadsheet with formulas or written some easily understood VBA to populate the values and then removed any duplicates just to be sure that the code didn't goof up.

I like the methods that others are sharing below equally as well as yours. Thanks for forcing me to "start" my brain at 6 in the morning. I needed that! 😄

hotflashfoto
Автор

I ended up doing the same calculation but in a slightly different way.

if there are 5! = 120 total permutations, then since each digit can appear in any place, then each digit must show up a total of 24 times in each place.

So in the 10000th place, 1-5 must show up 24 times each, so the sum for that place is just (1+2+3+4+5)(24), and then multiplied by 10000.

Then just do the same for the other place values, and you get the answer.

soogasooga
Автор

There's an even easier way to do this:
For each number in S, there is a corresponding number that adds with it to sum to 66, 666. For example, 12345 + 54321 =
Since there are 5! = 120 numbers in S, there are 60 pairs of these numbers, and thus the sum is * 60 = 3999960.

nsnick
Автор

I really love seeing everyone's different method of getting to the answer and how various people use a combination of strategies in different ways.

musicisinspration
Автор

I'm sure other commenters have said something similar, but this is how I got the solution.

There are just as many elements in the array where 5 is in the first digit spot as there are where 1 is in the first digit spot. The same goes for 2 3 and 4. So 5n + 4n + 3n + 2n + 1n = the sum of all of the first digits where n is just how many times 5 is in the first digit spot.

That works out to be 24. So (24)(5+4+3+2+1) = 24*15 = 360.

So now I know what each individual digit sums to. I just need to add those times the powers of 10 to give me my solution. Sure enough = 3, 999, 960

The_True_J
Автор

My way seemed simpler, I took some inspiration from Carl Friedrich Gauss. For every permutation of the 5 digits, there exists a unique element that causes the summation to equal (e.g. for 23145, the unique element is 43521). So pairs equal and there are 5!/2 =60 pairs to choose from. So the sum is = 3999960

jakecumber
Автор

Do it by the method of Gauss: To every number (of the 5! = 120) we find a unique "complementary number" where all digits sum up to 6. So we get 60 * = 3999960.

bernhardriemann
Автор

A wonderfully complicated solution to a problem that can be solved without paper in a few seconds. Strengthens your confidence.

pimogens
Автор

For each digit there are 24 options (4!). Therefore sum of each digit location is (5+4+3+2+1) =15.
24*15=360. = sum for each location of digits ( whether it 10, 000 or 1, 0000 or …..). Multiply 360 by gives the answer. I’m glad I was able to calculate this far without calculator or pen and paper.

Ima-hoot
Автор

So that was an interesting problem. I really liked it because the numbers are easy enough to calculate in your head and big enough that you can't just count them.^^
Every digit is in every place for 4!=24 numbers. So
I guess, that will be the solution presented in the video too.

Mystery
Автор

“We have to be sure that our code is correct and that the computer is not making some sort of goofy calculation”
As a programmer, I cringed so hard to this. The code he wrote is quite inefficient too.

LD-dtsk