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

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.

4

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

Yeah, the point of the question was basically to calculate that table.

Well, that's where the "brute force" comes into play, and implementing a brute force algorithm to solve this problem is itself very trivial: you just enumerate every permutation of operators, then evaluate each permutation, test to see if the returned value is 100, and store the resulting boolean value.

The point is that it's more efficient to do this once and store the results as data than to actually compute all of the results at runtime, even with a more clever recursive algorithm.

0

u/[deleted] May 09 '15

[deleted]

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

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.

2

u/Alexandur May 09 '15 edited May 13 '15

That's a really good idea. During my next interview I think I'll bring a big thermos of hot coffee, and when the salary negotiation begins I'll begin slowly pouring the coffee onto the interviewer while simultaneously increasing my salary requirements vocally. I'll stop when he begins to look uncomfortable.

→ More replies (1)

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

2

u/Berberberber May 09 '15

I think it's supposed to be kinda stream of consciousness.

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.

2

u/wongsta May 10 '15

the fact that you were able to explain your answer and think carefully about the solution is probably worth more to an interviewer than solving all the problems really quickly but not thoroughly :)

→ More replies (5)

2

u/dccorona May 09 '15

Interviewers will usually sidestep this by first asking you to implement the brute-force solution proposed, and then asking you to modify it to accept an arbitrary input array and desired sum value.

3

u/wherethebuffaloroam May 09 '15

Why did you randomly make that sentence end. You were so close to making it all the way through to the end in one breath

→ More replies (1)

1

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?

1

u/thehalfwit May 09 '15

You're hired.

→ More replies (1)