News for hackers. Stuff that matters.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by haberman on Thursday January 05, 2012 @07:36PM (#3431328)
I think some of your code is missing (like where N1 and MAX_ROWS are defined).

Here's mine:

  #include <stdio.h>
  #include <stdlib.h>

  long *pascal(int row) {
    long *ret = (row == 1 ? NULL : pascal(row - 1));
    ret = realloc(ret, sizeof(long) * row);
    ret[row - 1] = 1;
    for (int i = row - 2; i > 0; i--)
      ret[i] += ret[i - 1];
    return ret;
  }

  int main(int argc, char *argv[]) {
    int row = atoi(argv[1]);
    long *data = pascal(row);
    for (int i = 0; i < row; i++)
      printf("%ld ", data[i]);
    printf("\n");
    free(data);
    return 0;
  }
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by zyzzy on Friday January 06, 2012 @04:13PM (#3435048)
Hi,

I ran your code and it does run well. However fails for large cases such as pascal(200000).

You mentioned you worked for Google and Amazon, how can you modify the code to get an answer for pascal(200000)

Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by haberman on Friday January 06, 2012 @04:55PM (#3435217)
Yeah, for large cases the long will overflow (also it will do a lot of realloc()). Your two main options for this are:

  1. use double to get approximately-correct answers
  2. use an abitrary-precision library like GMP
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by zyzzy on Friday January 06, 2012 @05:51PM (#3435448)
What if I wanted an exact precision. Could I use a custom data type to get better precision? If I used a custom data type, how could I make the code work for a distributed system to get parallel behavior?
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by jiggy2011 on Thursday January 05, 2012 @08:13PM (#3431452)
upvoted for realloc trick
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by haberman on Thursday January 05, 2012 @08:37PM (#3431520)
This inspired me to write one that is even shorter and doesn't heap-allocate any memory at all. What can I say, I'm a sucker for minimal C.

  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[]) {
    int row = atoi(argv[1]);
    long data[row];
    for (int i = 0; i < row; i++) {
      data[i] = 1;
      for (int j = i - 1; j > 0; j--) data[j] += data[j - 1];
    }
    for (int i = 0; i < row; i++) printf("%ld ", data[i]);
    printf("\n");
    return 0;
  }
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by demallien on Friday January 06, 2012 @01:21AM (#3432255)
When did C start accepting non-constants for array size declarations?
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by hn_reader on Friday January 06, 2012 @01:52AM (#3432330)
I was wondering the same thing. But I can confirm it compiles and runs, though I had to supply a -std=c99 flag to gcc.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by demallien on Friday January 06, 2012 @06:02AM (#3432841)
Ahhh, useful tip. I've just been using alloca instead...
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by haberman on Friday January 06, 2012 @04:06AM (#3432604)
Variable-length arrays were added in C99.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by hn_reader on Thursday January 05, 2012 @11:10PM (#3431930)
Best solution I've seen here so far.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by shaggyfrog on Friday January 06, 2012 @02:47AM (#3432456)
If row is too big you can bust your stack frame when you define your data array.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by hn_reader on Friday January 06, 2012 @10:49AM (#3433696)
True, though in this example integer overflow would happen first.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by kunqiana on Friday January 06, 2012 @08:09PM (#3435783)
I think your code is off by 1.