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

Show parent comments

167

u/droogans May 08 '15

The fourth question is a cleverly disguised string manipulation problem.

Number five is the only one I found myself doubting my ability to solve in an hour.

49

u/WeAreAllApes May 08 '15

It's only 6561 combinations, and they didn't say it had to run in under a millisecond.

16

u/Doctor_McKay May 08 '15
  1. Post question to Stack Overflow

  2. Wait for answer

Problem solved.

→ More replies (1)
→ More replies (3)

59

u/timeshifter_ May 08 '15

It'd be piss-easy to solve with brute force JS. Just create every possible valid string combination of those 9 digits with + and -, and eval() them for whichever ones come out to be 100.

11

u/Backfiah May 08 '15

That's 9! runs though.

50

u/anttirt May 08 '15

between the numbers 1, 2, ..., 9 (in this order)

The original problem is only on the order of 38 steps brute-forced.

→ More replies (3)

28

u/lord_braleigh May 08 '15

The digits must stay in order.

0

u/[deleted] May 08 '15

[deleted]

3

u/jlt6666 May 08 '15

A language with eval makes it far easier. Or just call out to bash :)

4

u/yanetut May 08 '15

Bash? Did someone say bash?

#!/bin/env bash

function prob5() {
  if [[ $# -eq 1 ]]; then
    [[ $(($1)) -eq 100 ]] && echo $1
  else
    local exp="$1" ; shift
    local dig="$1" ; shift

    prob5 "$exp + $dig" "$@"
    prob5 "$exp - $dig" "$@"
    prob5 "$exp$dig" "$@"
  fi
}

prob5 1 2 3 4 5 6 7 8 9
→ More replies (4)
→ More replies (4)
→ More replies (2)
→ More replies (1)

16

u/Jonyb222 May 08 '15 edited May 08 '15

In this case programmer time is more precious that computing time, get it to work and then make it better.

And while Factorial run-time is horrendous 9! is "only" 362 880 38 is only 6561 runs of a maximal set of 8 addition/subtractions which gives us an upper bound of 2 903 040 52 488 operations.

It's obviously not a good solution, but it's better than not solving it at all, I don't know how long it would take, not long at all for sure and you can work on a better solution while you go along.

5

u/LazinCajun May 08 '15

The numbers have to stay in order, so it's only 38 expressions to check

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

6

u/sbelljr May 08 '15 edited May 08 '15

9! = 362880

Shouldn't take too long. The point of the question is to get the answer, not to get the answer that works for extremely large cases too.

Edit. There are 38 = 6561 possibilities to check, not 9!. The whole point of the question is to brute force it. My point stands.

4

u/jeffhawke May 08 '15

38 not 9!, it's combination of three elements in eight positions, that's less that 10000.

→ More replies (7)

3

u/Sluisifer May 08 '15

Yup, just brute force that fucker. You can get clever when you need to, and the brute force solution is often a good starting point, if nothing else to get you thinking about the problem clearly.

→ More replies (9)

35

u/Komputer9 May 08 '15

Here's my solution to 5, took about 45 mins to write in C (after looking at some other solutions, though). Runs pretty much instantly and doesn't use string manipulation, recursion, or the heap.

#include <stdio.h>

int main() {
	int digits[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int digitCount = sizeof(digits) / sizeof(int);
	int operationCount = digitCount - 1;
	
	int total = 1;
	for (int i = 0; i < operationCount; ++i) {
		total *= 3;
	}
	
	for (int i = 0; i < total; ++i) {
		int operations[operationCount];
		
		int sub = i;
		for (int b = 0; b < operationCount; ++b) {
			operations[b] = sub % 3;
			sub /= 3;
		}
		
		int numbers[digitCount];
		numbers[0] = digits[0];
		
		int count = 0;
		for (int b = 1; b < digitCount; ++b) {
			switch (operations[b - 1]) {
			case 0: // ""
				numbers[count] *= 10;
				numbers[count] += digits[b];
				break;
			
			case 1: // "+"
			case 2: // "-"
				++count;
				numbers[count] = digits[b];
				break;
			}
		}
		
		++count;
		
		int numbersTotal = numbers[0];
		int numberIndex = 0;
		for (int b = 1; b < digitCount; ++b) {
			int operation = operations[b - 1];
			
			if (operation == 0) continue;
			
			++numberIndex;
			
			switch (operation) {
				case 1: // "+"
					numbersTotal += numbers[numberIndex];
					break;
				
				case 2: // "-"
					numbersTotal -= numbers[numberIndex];
					break;
			}
		}
		
		if (numbersTotal == 100) {
			printf("%d", numbers[0]);
			
			int numberIndex = 0;
			for (int b = 1; b < digitCount; ++b) {
				int operation = operations[b - 1];
			
				if (operation == 0) continue;
			
				++numberIndex;
			
				switch (operation) {
					case 1: // "+"
						printf(" + %d", numbers[numberIndex]);
						break;
				
					case 2: // "-"
						printf(" - %d", numbers[numberIndex]);
						break;
				}
			}
			
			printf(" = 100\n");
		}
	}
}

Results:

123 - 45 - 67 + 89 = 100
12 - 3 - 4 + 5 - 6 + 7 + 89 = 100
12 + 3 + 4 + 5 - 6 - 7 + 89 = 100
123 + 4 - 5 + 67 - 89 = 100
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100
12 + 3 - 4 + 5 + 67 + 8 + 9 = 100
1 + 23 - 4 + 56 + 7 + 8 + 9 = 100
1 + 2 + 34 - 5 + 67 - 8 + 9 = 100
1 + 23 - 4 + 5 + 6 + 78 - 9 = 100
123 + 45 - 67 + 8 - 9 = 100
123 - 4 - 5 - 6 - 7 + 8 - 9 = 100

19

u/farfaraway May 08 '15

You get the job.

8

u/_teslaTrooper May 08 '15 edited May 08 '15

I'd be more productive though:

// found on reddit
printf("123 - 45 - 67 + 89 = 100\n\
12 - 3 - 4 + 5 - 6 + 7 + 89 = 100\n\
12 + 3 + 4 + 5 - 6 - 7 + 89 = 100\n\
123 + 4 - 5 + 67 - 89 = 100\n\
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100\n\
12 + 3 - 4 + 5 + 67 + 8 + 9 = 100\n\
1 + 23 - 4 + 56 + 7 + 8 + 9 = 100\n\
1 + 2 + 34 - 5 + 67 - 8 + 9 = 100\n\
1 + 23 - 4 + 5 + 6 + 78 - 9 = 100\n\
123 + 45 - 67 + 8 - 9 = 100\n\
123 - 4 - 5 - 6 - 7 + 8 - 9 = 100\n");

Actually did the first 4 in about 40 minutes, in C, so I'm gonna say if the problem set was constant difficulty I'd be fine.

4

u/somekindofprogrammer May 08 '15

This entire thread has made me a lot more at ease about my future career options. I always feel like I'm just the worst programmer, but I do math stuff like this all the time.

2

u/leeeeeer May 08 '15

Here's my solution in JavaScript, runs in half a minute and uses string manipulation, recursion, the heap, and eval().

function getToWith(getTo, nums) 
{
	var operations = [ "+", "-", "" ];

	var solutions = [];

	(function helper(pos, process) {
		if (pos >= nums.length) {
			if (eval(process) === getTo)
				solutions.push(process);
			return;
		} else {
			operations.forEach(function(a){
				helper(pos + 1, process + a + nums[pos]);
			});
		}
	})(1, ""+nums[0]);

	return solutions;
}

var getTo100 = getToWith.bind(null, 100, [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]);

var solutions = getTo100();
solutions.forEach(function(a){ console.log(a); });

Results:

"1+2+3-4+5+6+78+9"
"1+2+34-5+67-8+9"
"1+23-4+5+6+78-9"
"1+23-4+56+7+8+9"
"12+3+4+5-6-7+89"
"12+3-4+5+67+8+9"
"12-3-4+5-6+7+89"
"123+4-5+67-89"
"123+45-67+8-9"
"123-4-5-6-7+8-9"
"123-45-67+89"
→ More replies (3)
→ More replies (8)

4

u/__Cyber_Dildonics__ May 08 '15

It would probably be easiest to do with strings and move on although it could be also be done numerically.

→ More replies (2)

4

u/Inondle May 08 '15

Number 4 was fun and a little tricky. I decided to tackle it with a radix-style sort.

  • Load a hashMap<Integer, List<Integer>> with 1-9 keys and their respective lists.

  • Then take each number you're trying to sort and convert it to a string, take the first digit.

  • map.get(digit), add number to that list, then sort the list from highest to lowest.

  • After you're done with all the original numbers, go through each map entry (9->1) and take all the values from the entries' list and put it in a bigger list. return bigger list. Done :)

0

u/[deleted] May 08 '15

[deleted]

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

2

u/sharknice May 08 '15

It says find them all. You can solve it with simple brute force loops.

→ More replies (23)