r/programming • • May 09 '15

"Real programmers can do these problems easily"; author posts invalid solution to #4

https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k Upvotes

1.3k comments sorted by

View all comments

Show parent comments

3

u/hpp3 May 09 '15

Sort the numbers in reverse, as strings. You need to also pad all the strings to be the same length in order to compare, by repeating the string until it's the right length. E.g. 123 padded to 7 digits would be 1231231.

6

u/[deleted] May 09 '15

This doesn't work since it would see 56 and 565 as equal but they aren't. If you solve it this way then you need to pad till their least common multiple length.

1

u/hpp3 May 09 '15

That's true.

10

u/elcapitaine May 09 '15

See the author's solution - they originally thought the same thing, but that's wrong. It doesn't work for all cases. https://blog.svpino.com/2015/05/08/solution-to-problem-4

I never said that you'll be hired if you know how to answer these problems, but I won't consider you if you can't.

Guess they should fire themselves. Problems like these are fine - I liked solving this one, but if you're looking for the right answer as opposed to trying to see how the interviewee approaches problems you're interviewing wrong. This author comes across like an ass, and I'd never want to work for him.

2

u/psymunn May 09 '15

No, hpp3 is correct. The important part is HOW you pad the number. padding 123 with 0s to make it 1230000 doesn't work. Repeating the number does.

1

u/Phildos May 09 '15 edited Nov 12 '25

future quiet wakeful tender liquid cable friendly middle cover theory

This post was mass deleted and anonymized with Redact

1

u/psymunn May 09 '15

Here's the discussion boojum and I had about it. At the end of the day, his solution is really just the simplest. all he's doing is reducing the overall problem to the same comparison but for 2 numbers. It works because numbers will always have the same relative sorting, so you can sort them in any order.

2

u/Kaligraphic May 09 '15 edited May 09 '15

I did it a lazy way. I copied the numbers into a second array, but converted to floats. For each value, while that value is >10, I divided it by 10. That gave me single-digit numbers that I could sort.

Then I sorted the copy, but with a twist - I did the comparisons on the second array, but I did the moves on both arrays. This effectively sorted both arrays by the highest first digit. (I just did a bubble sort for proof of concept and because I'm a lazy git who normally uses Array.Sort() or equivalent, but a quicksort or better is fine too.)

then I spit out the string representations of every value in the first array, in order.

Then I realized that 9.1 sorts before 9, when I should put the 9 first. At this point, I realize that even if I had used a padding trick, 910 sorts before 900, so it would have been the same result, and I ended up concluding that brute force may actually be the only workable option.

0

u/[deleted] May 09 '15

[deleted]

1

u/Kaligraphic May 09 '15

True, but the constant overhead seemed like it would be greater than the average time the while loop would take, especially if we're mostly looking at one and occasionally two digit numbers. If the average expected value of n is less than 1.5, and the maximum expected value is 3, even O(n2) wouldn't seem so bad.

Then again, I could challenge myself on the grounds that CPU pipelines are long enough that the while loop might cost more in terms of branch misprediction. And now I want to devise an experiment to find out, but the results may well vary depending on the CPU that runs it. I suspect that a comparison and simple division would still be cheaper, but...

1

u/glemnar May 09 '15

log() isn't O(1) for big enough numbers.

It's all perspective : D

0

u/[deleted] May 09 '15

[deleted]

2

u/glemnar May 09 '15 edited May 09 '15

It depends. http://stackoverflow.com/questions/7317414/what-is-the-complexity-of-the-log-function

Besides, if you allow for arbitrary precision difference you just get a wrong answer. At high enough scales, even simple operations aren't O(1) is the point.

http://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations

1

u/hpp3 May 09 '15

You've effectively done a lexigraphical sort, which is what the string sort does.

1

u/Kaligraphic May 09 '15

Yeah, which is why it fails the same way.

1

u/MikeSz May 09 '15

my approach in python

import itertools

def maxval(inlist):

# first make an array of strings
strlist = []
for v in inlist:
    strlist.append(str(v))

maxval = 0

# brute force - try every permutation of strings
for thisone in itertools.permutations(strlist):
    teststr = ""
    for d in thisone:
        teststr += d
    thisval = int(teststr)
    if thisval > maxval:
        maxval = thisval
print 'max val is',maxval

2

u/glemnar May 09 '15

My go! As hilariously dumb as these threads have been I enjoy small problems on occasion : )

solver = lambda list_: max(int(''.join(map(str, s))) for s in itertools.permutations(list_))

1

u/MikeSz May 10 '15

yep, there it is. very nice.