r/programming • • May 08 '15

Five programming problems every Software Engineer should be able to solve in less than 1 hour

https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k Upvotes

2.1k comments sorted by

View all comments

Show parent comments

1

u/Inondle May 08 '15

Yeah this problem is a little more complex than it looks which is true of #5 as well. My algorithm works for most cases but if you have 2,2,21 then it'll put it in 2122 rather than 2221. Off the top of my head I'm not sure how you would cover that case though...

1

u/djk29a_ May 08 '15

I made a note of it with my wordy solution. (The code isn't so important given people are having trouble even getting a correct solution, so concept / approach is what I'm going to blab about)

You treat the 2s as 22 with extension for each digit beyond the first rather than just 2 for the sake of comparison. If you have a number like 29 in the list, that will bump ahead of the 2. 2, 28, 2111, 23, 222 Think of that list like this instead: 2222, 2888, 2111, 2333, 2222 Then, sort the list as you normally would greatest to least (keep track of index obviously): 2888, 2333, 2222, 2222, 2222, 2111 28, 23, 2333, 2, 2, 2, 222, 2111

For a subset, think of some of the numbers in a columnar format:

28
23
222
2111

First iteration through the list is for greatest of the first column, all 2. With the third iteration, the comparison is to lead with the value to the left if none is present:

2888
2333
2222
2111

I'll use your case plus one more that would mess you up:

2
21
2
2000

=>

22
21
22 <- this will percolate up because 22 > 21
2000 <- this will not percolate because 0 is the smallest value in column 1 (0-indexed)

=>

22
22
21
2000

Basically, keep track of the original number and keep sorting (descending) the interpreted value based upon the value for the column as you go to the right carrying the right-most digit over. It would be something like O(n * m * sortalg) where n is number of values, m is string length of values, which is a pretty terrible runtime and the brute force method might work. However, the brute force of trying all concatenation permutations will fail for very large lists of numbers, for example, due to integer overflow (this is why I balked at brute forcing it - 64-bit integers are big but you're screwed at a 32 element list already). With my solution, it uses constant space extra and you can use as many numbers or size values all you want.

Optimizations to this approach would be built around when to stop considering values at the extreme ends of your column for consideration. If you have 9 and all your other numbers start with 3, 4, 5, you don't need to think of the 9, it's not going to move anywhere. The efficient logic for that is not obvious to me when I'm hungry and getting hangry in the afternoon on a Friday.

And to contribute to why this list can be a poor, arbitrary measure, I have had easier questions than these in coding interviews I've failed because I tend to be terrible and do the naive solution given time pressure - that's the real world, folks. Ironically, I tend to come up with better solutions [i]and[/i] faster when nobody asks me for anything. I literally got the solution sitting on the toilet after I woke up and wanted to snap out of my stupor.