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

275

u/eddiemon May 09 '15

Problems 4 and 5 were pretty stupid tbh. I couldn't believe the original post got upvoted in the first place.

59

u/JustinsWorking May 09 '15

5 was kinda fun; not as an interview question but just as a little problem to code up.

8

u/just_plain_me May 09 '15

Nah they were both fun!

→ More replies (4)

92

u/gnuvince May 09 '15

I didn't think so. #4 showed that there are a lot of edge cases that you must consider, and a candidate showing in an interview that they can think of those is highly valuable. #5 has many things going for it too: see if the candidate can recognize that brute force is a practical solution here, see how they handle constructing the expressions (linear strings or trees), etc.

I thought that problems 4 and 5 were very good questions if the goal is not necessarily to get a perfectly right solution, but to get a conversation going between the interviewer and the candidate. In actual fact, a member of another lab at my university recently had to answer question 4 during an interview with Google. He thought the question was really interesting and reportedly enjoyed the back and forth this created with his interviewer.

25

u/bat_country May 09 '15

Someone eventually showed that #4 succumbed easily to brute force instead of being clever...

def q4(list)
  puts list.permutation.map(&:join).max
end

I was actually really pleased when that answer showed up

3

u/drink_with_me_to_day May 09 '15

this is pretty,what language is it?

→ More replies (1)
→ More replies (4)

45

u/FlyingBishop May 09 '15

1-3 are great because they give pretty strong confidence that the interviewee hasn't actually spent much time coding.

4-5 are not so great because they're a little trickier and don't necessarily reflect on-the-job skills.

(Although, in an interview I'm not usually looking for the "right answer" I'm looking for something that looks like it could be the right answer, which is pretty easy to get to for 4-5. It's somewhat unfair to expect people to find all the edge cases for a potentially unfamiliar problem in an hour.)

25

u/donalmacc May 09 '15

4 perfectly reflects on the job skills. It's an arbitrary problem that could be anything, but has a whole pile of edge cases that the candidate must consider. I don't know about you, but in my day to day work I have to consider edge cases all the time.

8

u/timoumd May 09 '15

I agree. 4 is my life. Just don't be upset if they get it wrong.

→ More replies (7)
→ More replies (25)

39

u/ILikeBumblebees May 09 '15 edited May 09 '15

5 has many things going for it too: see if the candidate can recognize that brute force is a practical solution here

I actually started running through an imagined interview scenario in my mind, in which I explained to the interviewer that brute force seems to be the most effective solution to this, but because of the small total number of permutations, runtime performance could be trivially optimized by using a prepopulated lookup table, which would take up only 19,683 bytes of memory in the worst case, but since each sequence of operators can be represented as a string of 16 bits, it might be possible to treat the string of operators itself as a pointer, and therefore to put the lookup table in only 6,561 bytes, which itself is eight times as much memory as you really need, since you're only trying to store boolean values which represent whether a given sequence of operators causes the digits 1-9 to add up to 100, so you could lop off the last three bits of that 16-bit string and pack the boolean values for eight operator sequences into a single byte, using the three bits you chopped off as an index to which bit in that byte represents the value for the queried sequence, resulting in a lookup table that only takes up 821 bytes; then I accidentally spilled my coffee on the interviewer and didn't get the job.

31

u/Slime0 May 09 '15

Wouldn't the process of making a lookup table require computing all permutations of the operators, in which case it's better to just print the ones that work as you find them? What's the benefit of the lookup table?

38

u/jrmrjnck May 09 '15

Yeah, the point of the question was basically to calculate that table. I don't know what he's going on about.

→ More replies (9)
→ More replies (29)

22

u/argh523 May 09 '15

This, gentleman, is why "premature optimization is the root of all evil".

→ More replies (1)

12

u/wllmsaccnt May 09 '15

Then I accidentally spilled my coffee on the interviewer and didn't get the job.

I would absolutely still hire someone if they spilled coffee on me if they seemed like they knew what they were doing on the coding end.

6

u/MonsieurBanana May 09 '15

It's actually a good test you can do when you're in a interview to see if you can ask for a bit more in terms of salary : spill coffee on the interviewer, if he's cool about it then they want you and you can up your offer.

→ More replies (2)

6

u/njharman May 09 '15

As your interviewer I'd ask why you wasted so much effort on optimization? Was thre a business case? Did you profile? What are the hotspots? Do you always preoptimize? What other things could you have accomplished if you hadn't spent so much effort on efficiency? What is more important developer efficiency or code efficiency?

→ More replies (19)

7

u/more_oil May 09 '15

It's all in how the question is framed. "Do this in one hour or you aren't a real programmer" is bad, "I'm interested in seeing your thought process and approach to problem solving" is good.

→ More replies (1)
→ More replies (10)

17

u/manghoti May 09 '15

Stupid how? Stupid in the assertion that you could do them in an hour? Or just stupid questions?

Personally I found 4 to be interesting, and I already knew about 5, and also found that one interesting.

106

u/eddiemon May 09 '15

The original post was "Five programming problems every Software Engineer should be able to solve in less than 1 hour" as if it's some golden litmus test for software engineers, but 4 and 5 are really just cute puzzles (not even that cute tbh) that are highly unlikely to show up in real world. It's like recruiting a professional basketball player based on their ability to make trick shots.

The clickbait title was fucking retarded too.

61

u/Snoron May 09 '15

Yeah.. and the person who posted it clearly didn't solve it in an hour, therefore they are an idiot :D

25

u/danweber May 09 '15

4 depended on seeing the trick. Depending on seeing a trick during an interview is bad form.

5 was easy if using a language with an eval().

22

u/Slime0 May 09 '15

I don't think I agree that #4 depended on seeing any trick. I think it was a more difficult problem than the blogger thought it was, but it's definitely something you can think through, try a solution in your head, find a counterexample, and refine your solution. I wouldn't expect anyone to solve it in an hour necessarily, but I would expect them to think it through thoroughly enough to realize that it's difficult and be able to explain why. It's actually a pretty good problem for watching someone's problem solving process.

19

u/Boojum May 09 '15 edited May 09 '15

I believe that I was the first to post a correct solution to #4. I would agree that I didn't really do it by seeing a trick but solved it through methodical refinement. The prprocess I used was:

  1. Write a brute force solution using itertools.permutations() to exhaustively try all possible orderings and return the best one. So simple it obviously works.

  2. Factorial time algorithms suck. There has to be a better way.

  3. Hm... Let's look at some of the trickier pairs that are counter examples to thinks like just lexicographically sorting the numbers as strings. For something like wgunther's sets, for example, {9295,92} is a better order to start with than {92,9295} because 929592 > 929295. And {92,9265} is better than {9265,92} because 929265 > 926592.

  4. So now we've compared the first two. Once we've settled on one of the two to put first in the list, we can compare and possibly swap the second and third the same way. Then the third and fourth...

  5. Hm, this smells like a bubble sort. Seems like that should work. Let's try it.

  6. Yep, that seems to work on all the tricky cases in the thread. Now let's try some randomized testing against the brute force solution to be sure.

  7. (Hundreds of thousands of random tests later...) Okay, I'm pretty confident in that solution now. Bubble sort is nice and all, and always compares adjacent pairs which I'm pretty certain now is valid. But does that comparison predicate work with other, faster, sorts?

  8. Write the final, posted version that uses the built-in Timsort. Run a few hundred thousand tests to verify. Post solution to reddit.

All told, I think I spent about 20-25 minutes.

That said, I've never been asked to do whiteboard coding for an interview and never asked anyone else to do it either. I much prefer to just try to get a good conversation going. It helps to be working in a somewhat specialized niche.

→ More replies (14)
→ More replies (1)
→ More replies (6)

4

u/magmapus May 09 '15

Me too. I actually spent a few minutes prototyping a couple different ways to do it. I think all of those would be valid interview questions, certainly better than FizzBuzz for evaluating logical thinking.

→ More replies (19)