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

6

u/[deleted] May 08 '15

Overall these problems were fun but I'd rather just do FizzBuzz or something else that's quick during an interview. I feel that an hour is pushing it and I'm sitting comfortable in my home with my favorite tools, not sweating in front of a whiteboard. Use the interview time to engage with the candidate and get a feel for more than just puzzle coding ability. An hour of time is worth money to both of you and I wouldn't want to waste that much time with a heads-down pop quiz.

I did it in a little over an hour but messed up #4 when sorting as strings, as some people here have pointed out. I fixed it using padding with the letter "A" which is later stripped out.

    static long ProblemFour(List<int> myInts)
    {
        List<string> myStrings = new List<string>();
        int longestString = 0;
        foreach (int x in myInts)
        {
            myStrings.Add(x.ToString());
            if (myStrings.Last().Length > longestString)
            {
                longestString = myStrings.Last().Length;
            }
        }
        for (int i = 0; i < myStrings.Count; i++)
        {
            while (myStrings[i].Length < longestString)
            {
                myStrings[i] += "A"; // "A" is greater than "9" when sorting
            }
        }
        myStrings.Sort();
        string s = "";
        for (int i = myStrings.Count - 1; i >= 0; i--)
        {
            s += myStrings[i];
        }
        return Convert.ToInt64(s.Replace("A", ""));
    }

One and two were easy. Three was easy except whoops, integer overflow. Recoded as longs and whoops, long overflow. Fibonacci gets big fast.

My solution to number five is a brute forcer shown here but I'm sure it can be improved upon:

    static void ProblemFive()
    {
        string[] operands = new string[8];
        ProblemFiveInner(operands, 0);
    }

    static void ProblemFiveInner(string[] operands, int cursor)
    {
        if (cursor < operands.Length)
        {
            // go deeper
            string[] newOperands = (string[])operands.Clone();
            int newCursor = cursor + 1;
            newOperands[cursor] = "-";
            ProblemFiveInner(newOperands, newCursor);
            newOperands[cursor] = "";
            ProblemFiveInner(newOperands, newCursor);
            newOperands[cursor] = "+";
            ProblemFiveInner(newOperands, newCursor);
        }
        else if (cursor == operands.Length)
        {
            // check validity
            int sum = 0;
            string s = "";
            int i = 9;
            while (i > 1)
            {
                s = i.ToString() + s;
                if (operands[i-2] == "-")
                {
                    sum -= Convert.ToInt32(s);
                    s = "";
                }
                else if (operands[i-2] == "+")
                {
                    sum += Convert.ToInt32(s);
                    s = "";
                }
                i--;
            }
            sum += Convert.ToInt32("1" + s);
            if (sum == 100)
            {
                string res = "1";
                for (int j = 2; j < 10; j++)
                {
                    res += operands[j - 2] + j.ToString();
                }
                Console.WriteLine(res + " = 100");
            }
        }
    }

Answers to number five are as follows:

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

3

u/abeliangrape May 08 '15

It pays to use a high level language for these types of problems. Check out my python solution:

from itertools import *
l = [str(x/2) if x%2==0 else '' for x in range(2,19)]
for ops in product(['','+','-'],repeat=8):
  l[1::2] = ops
  if eval(''.join(l))==100:
    print "%s = 100"%''.join(l)

And the output:

123+45-67+8-9 = 100
123+4-5+67-89 = 100
123-45-67+89 = 100
123-4-5-6-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
1+23-4+56+7+8+9 = 100
1+23-4+5+6+78-9 = 100
1+2+34-5+67-8+9 = 100
1+2+3-4+5+6+78+9 = 100

1

u/[deleted] May 08 '15

Nice elegant solution here!