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.

90

u/__Cyber_Dildonics__ May 08 '15

Other people have mentioned brute forcing it, and if I was in an interview that's what I would do in that situation.

80

u/mccoyn May 08 '15

It will take longer to write a more complicated solution than to run the brute force algorithm, so brute forcing is the fastest solution.

4

u/rdewalt May 08 '15

Seeing as the total space is only 6561 (38) brute forcing it seemed to be faster than spending the time hacking it out. even adding two more digits would increase the space to 59k possibles. I found a way to do it, brute force, but I want to manually proof my results since the "10" that are linked, are far less than the 17 that I got on my first pass.

→ More replies (8)

0

u/[deleted] May 08 '15

[deleted]

46

u/DrStalker May 08 '15

Also,management needs the code ready within an hour (along with four other deliverables) so implementing a solution that you know will work and have reasonable run time is the best thing to do.

5

u/rubsomebacononitnow May 08 '15

the sprint of all sprints

→ More replies (8)
→ More replies (6)

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.

161

u/Oberheimz May 08 '15

I actually went ahead and tried solving them, it took me 42 minutes to write a solution for the first 4 problems and I was unable to finish the fifth within one hour.. Am I a bad software engineer?

378

u/mariox19 May 08 '15

If it's on the Internet, it must be true. Every good software engineer knows that.

→ More replies (2)

100

u/gizzardgullet May 08 '15

Now you write your own quiz based on logic tricks that you are comfortable with and challenge the author complete your quiz in an hour.

→ More replies (6)

29

u/akshay2000 May 08 '15

Nah, you're just fine. It's the implementation that took time. The hour criteria is rough since one might just go on and on.

→ More replies (2)

16

u/ours May 08 '15 edited May 08 '15

Are you able to solve the problems that present themselves at work? If so, then no you are not a bad engineer.

Edit: if(solvesProblems) not if(!solvesProblems)

4

u/Oberheimz May 08 '15

Well, of course, wouldn't have a job otherwise ;)

11

u/ours May 08 '15

of course

I've worked with enough people to learn that sadly some that can't/don't want to code can coast surprisingly long in a sufficiently shitty environment.

Best one was a new hire who was supposed to be my senior ask me the most basic question about how to make a "if" condition. I chalked it of to one of those "doh I forgot something super basic" that can happen to any of us.

They realised the dude didn't know how to code at all only because I left for a month-long sabbatical and noticed nothing was done during my absence.

→ More replies (2)

11

u/Dakarius May 08 '15

You're looking at it the wrong way. It takes 5 hours to do 5 problems. You got the first 4 knocked out in 42 minutes leaving you 4 hours and 18 minutes for the last. You've still got 3 hours and 18 minutes left!

6

u/s-mores May 08 '15

Did you just write code to solve them, or put the code in action and tested it?

The only one I tested out was #4 and some syntax parts of #5. Since this is an interview question, it should mostly be measuring the approach and that you know how to do something or at least have some idea how to approach the problem instead of the actual result, which is pretty uninteresting, and in the case of #1-#4, trivial.

I mean, I'm not going to use 5-10 minutes per task in figuring out where I'm missing a semicolon, or where I've misremembered argument order and the code would fail because of that, that's what the compiler is for.

→ More replies (27)

25

u/[deleted] May 08 '15 edited May 08 '15

In prolog #5 looks something like this:

sum([Head|Tail],Signs,Result):-sum(Head,Tail,Signs,Result).

sum(X,[],[],X).

sum(First,[Second|Tail],['+'|Signs],Result):-Head is First + Second, sum(Head,Tail,Signs,Result).

sum(First,[Second|Tail],['-'|Signs],Result):-Head is First - Second, sum(Head,Tail,Signs,Result).

sum(First,[Second|Tail],[''|Signs],Result):-Head is First*10 + Second, sum(Head,Tail,Signs,Result).

sum(First,[Second|[Third|Tail]],['+'|[''|Signs]],Result):- C is Second*10+Third, Head is First + C, sum(Head,Tail,Signs,Result).

sum(First,[Second|[Third|Tail]],['-'|[''|Signs]],Result):-C is Second*10+Third, Head is First - C, sum(Head,Tail,Signs,Result).

edit: minor correction as others have pointed out I messed up the grouping slightly. further correction to follow

7

u/eelvex May 08 '15

In J one could solve it with:

L {~ I. 100 = +/"1 L=:([:".'9',~[:;(;/":"0>:i.8),.])"1 ('';',';',_') {~"1 (3&#.)^:_1 i.3^8

6

u/floccinaucin May 09 '15

I've been programming for a couple years now... what the heck am I looking at here?

5

u/eelvex May 09 '15

It's J, an array, functional-level tacit language. It's really cool and makes you think in completely new ways because a) you'll be thinking in array transformations [especially instead of loops] and b) you'll avoid variables.

For example, where in C you write y = function1(function2(x), function3(x)) in J you write y =: (function2 function1 function3) x

→ More replies (17)
→ More replies (73)

5

u/karlhungus May 08 '15

I agree, this is a challenging problem Erik Meijer did a solution to this in his edX haskell course, which involved parsing then searching the space for solutions. I did look but couldn't find the video.

→ More replies (2)
→ More replies (23)