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

42

u/Aeolun May 08 '15

1-3: I can easily do by virtue of having seen them before, or them being basic math.

4-5: These seem a solid order of magnitude more difficult. There's undoubtedly some way to solve them, but they're more akin to riddles than programming questions. I would not expect any programmer to know the solution to these (aside from brute force) if he'd never had to deal with problems of this kind before.

3

u/manghoti May 08 '15

ditto. Still scratching my noodle on the specifics of 4, and the comment in this topic have brought up some edge cases that make the question seem so much nastier. Best algorithm I've seen so far is a divide and conquer strategy.

3

u/MSgtGunny May 08 '15 edited May 08 '15

I would sort based on first digit of each number, if there is a conflict, it checks the next digit of both or next digit of one and the current digit if there are no more digits for the number.

Ie 5 53 55 59 501 would be sorted as 59, 55, 5, 53, 501

Maybe a modified radix sort would do the job.

2

u/Weezy1 May 08 '15

You've described an alphabetic String sort :)

→ More replies (1)

1

u/[deleted] May 08 '15

What if you found the longest number, expanded other numbers with some character that sorts higher(in case of reverse lower) than 0. And then just sorted it, concated it and stripped the character...

2

u/Inondle May 08 '15

Number 4 was 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 get the first digit. (I converted to string and got first character)

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

In theory that should get the correct answer. Only reason I thought of that was I saw radix sort on this algorithm site and it blew my mind you could sort with no comparisons.

2

u/rabbitlion May 08 '15

That doesn't get anywhere near a correct answer. If you have for example 2, 2, 21 all three gets placed in the same sublist and when they are sorted you will get 2122 which is clearly smaller than 2221.

→ More replies (1)

1

u/djk29a_ May 08 '15

Yeah, first thing I thought after I realized it's not a "just sort the concatenate numbers after splitting them all by character" that I've seen so many times was "this looks a lot like a radix sort." If there's repeat values allowed, you'd want say, 6, 6, 6... then 65 because we're doing something like the most significant bit based radix sort. So you can probably do something like an O(n2) algorithm like selection sort and swap for values that match your current radix 9 and smallet number of digits as priority. If there's no repetition the logic is slightly simpler (no need to consider 9, 9, 9 v 9, 99). But bubbling up to the front for each successive digit considered (9 down to 0) with the fewest place numbers as priority is the sort order we're looking for. 6, 688, 611 should sort to 688, 6, 611. The ordering that should numerically be expressed in a purely numeric comparison sort as 688, 6(66), 611 where the extra 66s are extending the leading digit - you should have any larger sort values already sorted ahead. If we iterate it out from there I see what I'd need to write but I've stopped caring and want my 2 minutes on the toilet back as I typed this out.

I think figuring the radix sorting nuance and understanding the corner cases of the fact that 4, 455, 499 should sort to 499, 455, 4 out is half the problem though. Earlier in my thought process I started working from 9, 99, 999 down and forgot cases where there can be a digit higher than the one I'm on. This is where starting with the average case would have led me on the right track while edge case consideration starting with the extremes wouldn't help.

All the brute force folks seem to have stopped caring by the time they got here though I think and are on their way to work as am I.

→ More replies (2)

1

u/vegiimite May 08 '15

sort using compare class

class comp : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return -(x + y).CompareTo(y + x);
    }
}

2

u/matthieum May 08 '15

There is a slight trick to number 4, in that you are interested not in the magnitude of the number per se, but its decimal representation.

Thus, you can either arrange the numbers and then convert to string, or convert to string and then arrange the strings. I suspect you've only tried the former ;)

2

u/mccoyn May 08 '15

Yeah, I thought 4 was easier at first until I saw some of the edge cases. Now I think the best approach is to just brute force it. That way, it is easy to get the right answer and won't take a lot of investigation to find the edge cases.

1

u/campbell1373 May 08 '15 edited May 08 '15

In case you're still curious, I did these in C# this morning. Problem 4 was actually rather simple once you realize that the first digit for each number in the list is the most important.

class Problem4
{
  public Problem4()
  {
    Console.WriteLine(Solution(new[] { 1, 50, 51, 2, 1, 9, 9 }.ToList()));
    Console.WriteLine();
  }

  private int Solution(List<int> ints)
  {
    if (ints.Count > 0)
    {
      int max = 0;
      foreach (var i in ints)
      {
        if (GetFirstDigit(i) > GetFirstDigit(max))
          max = i;

         **EDIT:**
        // I forgot to check for numbers with similar first digits
        var similar = numbers.Where(maybe => GetFirstDigit(maybe) == GetFirstDigit(max));
        if (similar.Count() > 1)
          max = similar.Max();
      }
      ints.Remove(max);

      // Shift the number based on the total number of remaining digits
      max *= (int)Math.Pow(10, String.Concat(ints).Length);

      return max + Solution(ints);
    }
    else return 0;
  }

  private int GetFirstDigit(int i)
  {
    return (i > 10) ? GetFirstDigit(i / 10) : i;
  }
}

2

u/SortaEvil May 08 '15

I think that's the sign of a good programming question, then. In an interview, you want to see how the person thinks; the rote questions show that they know anything at all, and serve as an excellent short-circuit (I mean, if you can't solve fizzbuzz or an equivalent, you're probably not even at a junior level), and the other questions show that you can actually work your way through a novel problem to a reasonable solution.

1

u/newpong May 08 '15

so in other words you're complaining that the questions are too easy or too hard?

3

u/Aeolun May 08 '15 edited May 08 '15

Neither. In my opinion, 1-3 could be used to determine -any- software engineer, but 4-5 would only be relevant in a subset of cases. The problem is that these are 'trick' questions, so if you've never seen them before, you'll be searching for solutions that aren't there.

→ More replies (1)

1

u/DerJawsh May 08 '15

It seems like 4 would require you to devise an algorithm that would compare the largest first digit, then if we have multiple numbers with the largest first digit, keep checking the next largest digit until we hit an end of one number or find one larger than the other, whichever fit that criteria first is the next to be added.

So 913 9134, 654, 72, and 9 would be: 9-913-9134-72-654 which fits the criteria. Main issue is transposing it into code which would be an annoying task but the algorithm doesn't seem too difficult, definitely similar to something you would learn in an algorithms course.

2

u/jcdyer3 May 08 '15

Try that with 56, 5, 54

1

u/DerJawsh May 08 '15

I think that can be solved by making it so we only take the shorter number if the next digit of the larger number is smaller than the first digit of the shorter number.

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

1

u/bstamour May 08 '15

4 isn't that bad if you think of it as a kind of modified insertion sort.

  1. If you have only one number in the list, the result is just that number.

  2. If you have K > 1 numbers in the list, your task is to find the position for where to put the K'th number. So apply the algorithm recursively for the list of K-1, and insert the K'th number in the list.

The trick is designing a comparison routine to find where to place a new number into an already solved list. One naive way to do it is to simply place the new number into every possible position and compute the resulting number. The winner is whatever resulting number is biggest. This results in an O(n2) algorithm, but we can make it faster if we can find a better way of discovering the insertion points so we don't have to scan the entire list each time.

1

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

I created a /r/shittyprogramming solution for the fourth problem:

import ast
mylist = [50, 2, 1, 9]
stringlist = [str(x) for x in mylist]
stringlist.sort()
stringlist.reverse()
stringlist = ''.join(stringlist)
result = ast.literal_eval(stringlist)
print result

It works since strings are sorted differently from ints - calling sorted(mylist) and then reversing it would return [50, 9, 2, 1] while the above returns 95021 - the correct answer.

→ More replies (6)