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).

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).

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.

→ More replies (45)

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.

→ More replies (12)

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

30

u/[deleted] May 08 '15

This doesn't work, try sorting [56, 565655, 56565665] with it. The 56565665 should come first, then 56, then 565655. I doubt that you would be able to even see that this was a problem let alone solve it in under an hour. Almost all non-brute force solutions posted here fails.

8

u/iqtestsmeannothing May 09 '15

Almost all non-brute force solutions posted here fails.

Right, and I've not seen a single person try to prove that their non-brute-force solution is correct. (I've tried to make such a proof and failed.) This problem is way harder than the others, and the author really cocked up by including this problem, with an incorrect solution and no attempt at a proof, on a list of "ridiculously simple" problems.

→ More replies (9)
→ More replies (4)

37

u/Drolyt May 08 '15

I think you are over-thinking 4. Just brute force it: find all the possible concatenations and then use the max function your language most likely provides. You can find an efficient way to do it after the hour is up.

→ More replies (43)
→ More replies (127)