[A picture of private offices at Fog Creek Software] Alert! This ancient trifle retrieved from the Joel on Software archive is well-past its expiration date. Proceed with care.

Joel on Software

Test Yourself

by Joel Spolsky
Thursday, December 29, 2005

By the time I got to Penn for my first year of college, I thought I was already a pretty good programmer. Completely self-taught, I had written two major systems in Turbo Pascal ... one of them was a complete inventory system for a small factory, while the other scheduled all the production at one of Israel's largest bakeries.

It took me until the midterm exams to realize I wasn't as smart as I thought. I completely screwed up some questions, because I still didn't get pointers and I still didn't get recursion.

Never one to hold a grudge, I share those midterm questions with you... see if you can do better than I did freshman year.

1a. (MIT-Scheme) Using the function


(define (accumulate combiner null-value l) 
   (if (null? l)
       null-value
       (combiner (car l)
                 (accumulate combiner
                             null-value
                             (cdr l)))))

Implement sum-of-squares, which calculates the sum of squares of a list, for example

(sum-of-squares '(1 2 3 4 5))

should evaluate to 55.

(For the answer, select the text in the box:)

(define (sum-of-squares l)
   (accumulate (lambda (x y) (+ (* x x) y))
               0
               l))

1b. (JavaScript) OK, maybe Scheme is not your thing. This question is equivalent to 1a, in JavaScript.

Using the function

function accumulate(combiner, nullValue, l)
{
	if (l.length == 0)
		return nullValue;
	var first = l.shift();
	return combiner(first, accumulate(combiner, nullValue, l));
}

Implement sumOfSquares, which calculates the sum of squares of a list, for example

sumOfSquares([1,2,3,4,5])

should evaluate to 55.

(For the answer, select the text in the box:)

function sumOfSquares(lst)
{
	return accumulate(function(x,y){return x*x+y}, 0, lst);
}

2. (ANSI C) Write a C program of the following form:

#include <stdio.h>
int main(int argc, char **argv)
{
   ???
}

such that, after compiling it, it can be executed as

% ./a.out could harold eat eight salami elephants

and it will print the first letter of every argument (in this case, "cheese").

(For the answer, select the text in the box:)

#include <stdio.h>
int main(int argc, char** argv)
{
  int i;
  for (i=1; i<argc; i++)
  {
    putchar(argv[i][0]);
  }
  putchar('\n');
  return 0;
}

3. (ANSI C) What is the output of the following C program?

#include <stdio.h>

char *fn(int i, char *s)
{
  while (i)
  {
    s++;
    i--;
  }

  return s;
}

int main(int argc, char** argv)
{
  int a = 2;
  static char c[] = "test";

  printf("%s\n", fn(a,c));

  return 0;
}

(For the answer, select the text in the box:)

st (followed by a newline)
For more programming challenges, check out TopCoder.
Have you been wondering about Distributed Version Control? It has been a huge productivity boon for us, so I wrote Hg Init, a Mercurial tutorial—check it out!

Want to know more?

You’re reading Joel on Software, stuffed with years and years of completely raving mad articles about software development, managing software teams, designing user interfaces, running successful software companies, and rubber duckies.



About the author.

I’m Joel Spolsky, co-founder of Fog Creek Software, a New York company that proves that you can treat programmers well and still be highly profitable. Programmers get private offices, free lunch, and work 40 hours a week. Customers only pay for software if they’re delighted. We make Trello, which lets you organize anything, together, FogBugz, enlightened issue tracking software for bug tracking, and Kiln, which provides distributed version control and code reviews. I’m also the co-founder and CEO of Stack Exchange. More about me.

© 2000-2014 Joel Spolsky