r/programming • • May 09 '15

"Real programmers can do these problems easily"; author posts invalid solution to #4

https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k Upvotes

1.3k comments sorted by

View all comments

Show parent comments

2

u/vidude May 10 '15

Counter-example: 56 56554 56556

The correct answer is 565655656554, while your algorithm would give the answer 565565656554. I don't think you can escape doing some level of brute force analysis. The perl script below starts with a lexically ordered list but recursively tries all different orderings of numbers that have the same starting digit. Of course it degenerates to full brute force if all numbers start with the same digit. Dynamic Programming techniques could be used to improve this.

#!perl
use strict;

sub removeNthElement($@) {
  my $remove = shift;
  my @list = ();

  for (my $i = 0; $i < @_; $i++) {
    if ( $i != $remove ) {
      push @list, $_[$i];
    }
  }
  @list;
}

sub calc($@) {
  my $so_far = shift;

  if ( @_ == 1 ) {
    # only one number left, append it and we're done
    return $so_far.$_[0];
  }

  # calculate how many numbers at the head of the list have the same first digit
  my $n = 1;
  while ( $n < @_ && (substr($_[$n],0,1) eq substr($_[0],0,1)) ) {
    $n++;
  }

  # first let's assume we will use the next number
  my $value = calc($so_far.$_[0], removeNthElement(0, @_));

  # now let's see if we can do better with one of the other choices
  if ( $n > 1 ) {
    for ( my $i = 1; $i < $n; $i++) {
      my $alt_value = calc($so_far.$_[$i], removeNthElement($i, @_));
      if ( $alt_value gt $value ) {
        $value = $alt_value;
      }
    }
  }

  return $value;
}

my @list = qw/56 56554 56556/;
print calc("",sort { $b cmp $a } @list)."\n";
exit;

1

u/[deleted] May 10 '15

Nice example! In order for the sort to work on a dependable algorithm, it seems like we would need to consider for each match whether a shorter number is a subsequence (starting at the most significant digit) of the longer number, and then use the greatest digit of the subsequence as a pad. The amount of memory/computation to do this for every pair is almost certainly a complete waste of time. Brute force is probably the best solution, unless there's something really clever out there.

2

u/vidude May 10 '15

In the real world it's all about trade-offs. Hopefully the real value of such a question, if used in a job interview, would not be to see who can write correct code the fastest but to elicit appropriate questions as to the size and characteristics of the input, or how fast or often the process has to run (e.g. in a web request vs once a quarter). Because these are the kinds of questions you need to ask in real life.

1

u/chaszzzbrown May 11 '15

Brute force isn't necessary. Assuming the elements are already strings, just sort, in decreasing order, using (a+b)>(b+a) to determine your comparisons of elements a, b. A good way to test someone's algorithm is in fact to find a pair a,b where the algorithm sorts as [a,b] but a+b<b+a.