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

3

u/manghoti May 08 '15

ditto. Still scratching my noodle on the specifics of 4, and the comment in this topic have brought up some edge cases that make the question seem so much nastier. Best algorithm I've seen so far is a divide and conquer strategy.

2

u/Inondle May 08 '15

Number 4 was a little tricky. I decided to tackle it with a radix-style sort.

  • Load a hashMap<Integer, List<Integer>> with 1-9 keys and their respective lists.

  • Then take each number you're trying to sort and get the first digit. (I converted to string and got first character)

  • map.get(digit), add number to that list, then sort the list from highest to lowest.

  • After you're done with all the original numbers, go through each map entry (9->1) and take all the values from the entries' list and put it in a bigger list. return bigger list.

In theory that should get the correct answer. Only reason I thought of that was I saw radix sort on this algorithm site and it blew my mind you could sort with no comparisons.

1

u/djk29a_ May 08 '15

Yeah, first thing I thought after I realized it's not a "just sort the concatenate numbers after splitting them all by character" that I've seen so many times was "this looks a lot like a radix sort." If there's repeat values allowed, you'd want say, 6, 6, 6... then 65 because we're doing something like the most significant bit based radix sort. So you can probably do something like an O(n2) algorithm like selection sort and swap for values that match your current radix 9 and smallet number of digits as priority. If there's no repetition the logic is slightly simpler (no need to consider 9, 9, 9 v 9, 99). But bubbling up to the front for each successive digit considered (9 down to 0) with the fewest place numbers as priority is the sort order we're looking for. 6, 688, 611 should sort to 688, 6, 611. The ordering that should numerically be expressed in a purely numeric comparison sort as 688, 6(66), 611 where the extra 66s are extending the leading digit - you should have any larger sort values already sorted ahead. If we iterate it out from there I see what I'd need to write but I've stopped caring and want my 2 minutes on the toilet back as I typed this out.

I think figuring the radix sorting nuance and understanding the corner cases of the fact that 4, 455, 499 should sort to 499, 455, 4 out is half the problem though. Earlier in my thought process I started working from 9, 99, 999 down and forgot cases where there can be a digit higher than the one I'm on. This is where starting with the average case would have led me on the right track while edge case consideration starting with the extremes wouldn't help.

All the brute force folks seem to have stopped caring by the time they got here though I think and are on their way to work as am I.

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...

→ More replies (1)