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

5

u/[deleted] May 08 '15

Write a function that given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021.

Is the solution to this one simply concatenate the numbers in descending order by the first digit (e.g. [9] [5]0 [2] [1]) plus the cases where there are multiple numbers that begin with the same digit, where you would move to the 2nd, 3rd etc digit in that number?

37

u/wgunther May 08 '15

You have to be careful: {9295,92,7} requires the choice 9295 92 7, {9265,92,7} requires the choice 92 9265 7. There's some decision making that needs to happen with ties where one is an initial substring of the other.

45

u/Boojum May 08 '15

Nice. I don't have a proof for this, but I believe that you can determine the proper ordering of any two numbers by concatenating them both ways and then taking the larger of two. Given that, here's my O(n log n) solution in python:

def prob4(l):
    l = map(str, l)
    l.sort(lambda i, j: cmp(j + i, i + j))
    return map(int, l)

Lots of random testing against a brute-force permuting solution has yet to turn up a case where this fails.

5

u/wgunther May 08 '15

Yeah, that seems like a more clever comparison than what I was thinking.

→ More replies (12)
→ More replies (10)

19

u/BlueRenner May 08 '15

I get the feeling a couple of these are deeper than he thinks and could stand substantial mathematical analysis beyond anything available in a 1-hour interview window. In the best case scenario, the interviewer just wants you to take a reasonable shot and be able to explain yourself; in the worst case the interviewer is just looking to show off how smart they are in their pet problem field.

30

u/[deleted] May 08 '15

The first three seemed simple to me. The fourth and fifth struck me as a solid order of magnitude more difficult than the rest.

11

u/thirdegree May 08 '15

The fourth was interesting enough to hold my attention, the fifth was more involved than I was willing to be for an impromptu internet programming challenge.

10

u/erewok May 08 '15

Same here. I looked at the last one and wondered if I really could code a solution in an hour. "Maybe it's not like the others and there's a trick to it?" I wondered.

→ More replies (1)
→ More replies (5)
→ More replies (3)

6

u/LOOKITSADAM May 08 '15

My first instinct is to just treat them like strings and sort by reverse alphabetical order, then join them all together.

But then again, I'm close to half dead and fairly delirious at the moment, I could be way off.

→ More replies (11)
→ More replies (9)