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.

→ More replies (9)

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.

→ More replies (1)
→ 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)

→ More replies (4)

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!

→ More replies (28)

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

→ More replies (20)
→ More replies (73)
→ More replies (26)

181

u/codebje May 08 '15

I got stuck on #1 trying to write a for-loop in Haskell.

26

u/spacelibby May 08 '15 edited May 08 '15

for i end body

| i == end = id

| otherwise = body i . for (i+1) end body

You should be able to do that in less than an hour.

26

u/[deleted] May 08 '15

As someone who hasn't used haskell before... Are for loops even supposed to be used in Haskell? It looks so alien to me.

38

u/eagle23 May 08 '15

You are right, they aren't. Haskell is (for the most part, there are exceptions) purely functional and all data structures immutable and thus no point in conventional for loops. Instead Haskell users employ folds, maps and recursive functions. /u/spacelibby wrote a recursive function to emulate a for loop so as to try and hack one in.

→ More replies (7)
→ More replies (10)
→ More replies (4)

112

u/Zequez May 08 '15 edited May 09 '15

Ruby 7-liner:

['+', '-', ''].repeated_permutation(8).each do |perm|
  sum_text = ('1'..'8').zip(perm).flatten.join + '9'
  sum = eval(sum_text)
  if sum == 100
    puts "#{sumText} = 100"
  end
end

Ruby bring tears to my eyes <3

Took me more than 1 hour though, I did it in JS first, which yield a much less efficient result. I won't post the JS code because the things I did to get to the result are horrific and should not be seen by mortals. Ok here it is. I know, it's bad.

Edit 1: Optimised it a bit with something I saw someone doing below, adding the permanent '9' at the end of each string.

Edit 2: Yes! As mentioned below, you can make it shorter, 4 easily readable lines:

['+', '-', ''].repeated_permutation(8).each do |perm|
  sum_text = ('1'..'8').zip(perm).flatten.join + '9'
  puts "#{sum_text} = 100" if eval(sum_text) == 100
end   

Also, added underscores for convention, sorry, too much JS lately.

Also, obligatory thanks for the gold, although I feel I didn't deserved it, too many mistakes, the code could have been 4 lines from the start!

Edit 3: Ok, since someone asked, here is a version without eval, using String#to_i

['+', '-', ''].repeated_permutation(8).each do |perm|
  sum_text = ('1'..'8').zip(perm).flatten.join + '9'
  puts "#{sum_text} = 100" if sum_text.split(/(?=[+-])/).map(&:to_i).reduce(&:+) == 100
end

Edit 4: Ok, here is a version without any kind of string manipulation, all integers, no one can complain now. And still in technically 4 lines! Because I expanded the chain, so it could be just 1 line. Although I cheated with a ; inside one inject. So let's call it 4 1/2 lines and call it a day:

# The operators are:
# 0 = no operator
# 1 = + operator
# 2 = - operator
[0, 1, 2].repeated_permutation(8).each do |perm|
  sum_array = perm
    .zip(2..9) # [[0, 2], [1, 3], [0, 4], [2, 5]] etc, equivalent to 2+34-5 
    .inject([[0, 1]]) {|arr, (op, num)| op == 0 ? arr.last.push(num) : arr.push([op,num]); arr } # [[0, 2], [1, 3, 4], [2, 5]]  etc
    .map{|arr| ((arr.shift == 2) ? -1 : 1) * arr.reverse.each_with_index.inject(0){ |sum, (n, i)| sum += n * (10**i) } } # [2, 34, -5] etc
  puts "#{sum_array.join} = 100" if sum_array.reduce(&:+) == 100
end
→ More replies (42)

60

u/Watley May 08 '15

Number 4 requires dealing with substrings, e.g. [4, 50, 5] should give 5-50-4 and [4, 56, 5] would be 56-5-4.

Number 5 I think can be done with a recursive divide and conquer, but it would be super tricky to make efficient.

105

u/__Cyber_Dildonics__ May 08 '15 edited May 08 '15

4 is definitely non trivial and doesn't really belong with the rest of the problems that make me feel like a genius.

I think it could be done by sorting based on the left most digit (obviously) and then resolving conflicts in the first digit by the double digit number being greater if the second digit is greater than or the same as the first digit. The rest of the sorting should happen naturally I think, so a standard sort algorithm could be used.

Edit: Before you reply, think about if your method (which is probably 'sort them as strings directly') would sort 56 then 5 then 54 in the correct order (which is 56 5 54).

→ More replies (287)
→ More replies (43)

0

u/[deleted] May 08 '15

[deleted]

24

u/Frexxia May 08 '15

I think you mean 38 .

→ More replies (4)
→ More replies (3)

15

u/goomyman May 08 '15

umm number 4 is definitely non trivial as is number 5 ( if efficiency matters ) otherwise brute force seems ok.

Answering numbers 4 and 5 in an hour interview - with 15 minutes of chitchat about your former work and resume - is unlikely. Especially at a smaller company that isnt willing to pay a silicon valley salary.

→ More replies (5)
→ More replies (106)