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

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 (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)

3

u/Zarkdion May 09 '15

Well, he did say "trivially optomized."

→ More replies (1)
→ More replies (27)

22

u/argh523 May 09 '15

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

3

u/dccorona May 09 '15

Suddenly you've burned several hours creating a way to statically hold the solution in memory, only to decide you'd rather do it with arbitrary inputs and that it's back to the drawing board.

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?

9

u/genericlurker369 May 09 '15

I was trying to figure out why your comment seemed pretentious or more likely, hard to follow, but then I realized: RUN-ON SENTENCES

→ More replies (1)

10

u/[deleted] May 09 '15

Wow. If I'm competing against people like you for jobs...I'm fucked.

21

u/ILikeBumblebees May 09 '15

Don't worry: I spill a lot of coffee.

4

u/wongsta May 09 '15 edited May 09 '15

Aren't there three possible choices (+, -, and concatenate?). I thought it'd work something like this (just the lookup part):

Convert the sequence of into ternary

Lets assume + is 0, - is 1, and concat (_) is 2 (doesn't really matter)

For example, 1+23-4 it would be [ + , _ , - ] which is [ 0, 2, 1 ]

Now convert the ternary number to decimal: 0 * 1 + 2 * 3 + 1 * 9 = 15

Lookup the bit array containing the number

To get the correct offset (assuming it's stored as unsigned chars) it would be something like:

isValidSequence = lookup_array[bitoffset/8] & (1 << (bitoffset % 8)) (this might be wrong)

[1101 0010 0010 0001 1000]
                     ^this bit

6

u/hansdieter44 May 09 '15

Thats what I did as well,

you have the potential to run in a fencepost error here (9 digits, but you have 8 slots between them).

That gives you 3 ** 8 = 6561 values to iterate through, and convert to ternary and then merge with the digits. I actually managed to reuse my homegrown 'zip' function from task two here to combine my array of digits with the array of operators & spaces.

I then just looped over this, eval()'d the string (I know, I know) and if the sum was 100 printed/yielded the result.

http://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error

1 - 3 I literally wrote down within 10 minutes, but 4 and 5 got me thinking for quite a while. After thinking about the edge-cases I felt a bit stupid because I went over the 1hr mark when I finished. Also, in an interview stress situation there would have been no way I would have succeeded at all tasks.

→ More replies (1)
→ More replies (5)
→ More replies (6)

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)

2

u/NinlyOne May 09 '15

if the goal is not necessarily to get a perfectly right solution, but to get a conversation going

This right here. The biggest problem with the original blog post was the attitude that if you didn't nail it in under an hour, you should (to paraphrase) reconsider your career choice.

→ More replies (9)