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

584

u/__Cyber_Dildonics__ May 08 '15

The fifth question doesn't seem nearly as easy as the rest (the fourth question is not that hard guys).

186

u/orclev May 08 '15

That fifth one honestly has me a bit stumped... I can see how to brute force it, but there's got to be a simple solution. All the others are pretty simple and shouldn't require too much thought even if you've never seen them before.

180

u/youre_a_firework May 08 '15

#5 is probably NP hard since it's kinda similar to the subset-sum problem. So there's probably no way to do it that's both simple and efficient.

23

u/whydoismellbacon May 08 '15

What you could do is create a 3 state class that represents the points between the digits. 1 would be add (+), 2 minus (-), and 3 append/group. Then you have a recursive function that tries every combination and the moment it gets above 100 it returns 0 (or if it adds to 100 it prints the combination that works on a new line).

Definitely possible, however it would probably take the whole hour (or more) to complete.

37

u/youre_a_firework May 08 '15

Yeah, that's the "simple" and not "efficient" solution. :)

96

u/nkorslund May 08 '15 edited May 08 '15

Um no the simple solution is just try all 3**8 = 6561 possibilities. Which should run in tenths of a second (at most) on any modern hardware.

1

u/serrimo May 08 '15

As a wild guess, I think it shouldn't take more than a few milliseconds to do this.

15

u/allak May 08 '15 edited May 08 '15

yep

 [11:12][~] time ./test_perl_5.pl
 1+2+3-4+5+6+78+9=100
 1+2+34-5+67-8+9=100
 1+23-4+5+6+78-9=100
 1+23-4+56+7+8+9=100
 12+3+4+5-6-7+89=100
 12+3-4+5+67+8+9=100
 12-3-4+5-6+7+89=100
 123+4-5+67-89=100
 123+45-67+8-9=100
 123-4-5-6-7+8-9=100
 123-45-67+89=100

 real    0m0.267s
 user    0m0.217s
 sys     0m0.046s

(i wonder if it is correct ...)

1

u/s-mores May 08 '15

Hey! I did it in Perl, too. With 1-4 being solvable by one-liners in Perl it was fun to spend some time figuring out the most sensible way of throwing arrays around in #5.

How did you build the brute force tree or traverse it btw?

1

u/allak May 08 '15

I made a pastebin of the source in my other comment; as I've said, it's a disgusting mix of brute force and too much cleverness, but I was actually timing myself, and manged to do all five under one hour, so in this sense it was a success (number 5, that is; number 4 was wrong).

→ More replies (0)