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

6

u/Boojum May 08 '15

I posted a comment and code on this hours ago, but it's way down below, so I'll echo it here.

The key is to sort using the predicate concat(l,r) > concat(r,l)

So for your first example:

5 comes before 526 since concat(5,526) > concat(526,5)
5 comes before 27 since concat(5,27) > concat(27,5)
526 comes before 27 since concat(526,27) > concat(27,526)

So the correct ordering must be 5,526,7.

For the second triplet:

5 comes before 526 since concat(5,526) > concat(526,5)
5 comes before 25 since concat(5,25) > concat(25,5)
526 comes before 25 since concat(526,25) > concat(25,526)

So the correct ordering must be 5,526,25.