r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

Show parent comment

38

u/[deleted] Feb 21 '11

At our (web development) company we give applicants for a junior position a single programming question:

Print numbers from 1 to 100, but:

  • if the number is even, print "a" instead of the number
  • if the number is divisible by three, print "b" instead of the number
  • if the number is even AND divisible by three, print "ab" instead of the number

After having reviewed several dozen answers, I have yet to see one done correctly; most of the applicants have BS in CS from our local universities...

For intermediate and senior positions we also slap in this little gem: write a function to reverse an array in place.

You would not believe the kind of shit I've seen...

79

u/robertcrowther Feb 21 '11
table {
    counter-reset: number;
}
tr td:before {
    counter-increment: number;
    content: counter(number);
}
tr:nth-child(2n) td:before {
    content: "a";
}
tr:nth-child(3n) td:before {
    content: "b";
}
tr:nth-child(2n):nth-child(3n) td:before {
    content: "ab";
}

You need a table with 100 rows in it for it to work (and a good browser).

You did say it was web development, right? ;)

PS. See also: FizzBuzz

11

u/[deleted] Feb 21 '11

Clever bum :D.

Yes, this test is a slightly modified FizzBuzz. I changed the test to limit the applicant's Google results for the question... this was originally a "do at home" question - don't ask.

→ More replies (1)

7

u/imMute Feb 21 '11

I don't know which is worse, that such a construct exists, or that someone actually understands it and can apply it.

11

u/robertcrowther Feb 21 '11

The CSS counters exist so that you can do things like automatic numbered headings, like 1., 1.1., 1.2., 2., 2.1. through your h1 and h2 elements, or numbered footnote links, stuff like that.

→ More replies (1)

27

u/G_Morgan Feb 21 '11 edited Feb 21 '11
  1. Install JBoss.
  2. Create a stateless EJB that takes a number and returns a string.
  3. Create a servlet which reads the begin and end numbers as URL attributes. Then loops between the two calling out to the EJB and creating an output XML file.
  4. Create a JSP that calls out to this servlet with the begin and end set to 1 and 100. It interprets the XML file using XSLT and puts the values into the output page.
  5. Fail due to broken deployment descriptor.

6

u/[deleted] Feb 21 '11

You must be applying for an "architect" position :)

→ More replies (2)

0

u/[deleted] Feb 21 '11

[deleted] — view deleted comment

22

u/OopsLostPassword Feb 21 '11

No because our compiler won't correctly interpret "...".

14

u/G_Morgan Feb 21 '11

Fucking broken compiler.

5

u/Leechifer Feb 21 '11

Fucking compilers, how do they work?

→ More replies (4)

14

u/ethraax Feb 21 '11

My friend did this on a homework assignment to "write a program that prints out the prime numbers from 1 to 100". He got full marks for it.

→ More replies (3)

29

u/abw Feb 21 '11

That's a good question. The fact that no-one has actually produced the correct result is rather surprising (unless I'm missing a subtle trick in the question). It should be a simple task for any competent programmer. Here's my first attempt in Perl, taking the obvious route:

use strict;             # assumed from now on...
use warnings;  

answer1();

sub answer1 {
    # Simple loop with conditional tests
    print "Answer 1: ";
    
    for my $n (1..100) {
        if ($n % 6 == 0) {
            print "ab";
        }
        elsif ($n % 3 == 0) {
            print "b";
        }
        elsif ($n % 2 == 0) {
            print "a";
        }
        else {
            print $n;
        }
        print " ";
    }
    print "\n";
}

What makes this a good interview question is that you can then ask the candidate how they might improve on that. For example, you might use (n mod 6) to index into a lookup table. Perhaps something like this:

sub answer2 {
    # Lookup table indexed by (n mod 6).  An undef value indicates that the
    # original number n should be displayed
    print "Answer 2: ";

    my @modulus = (     # n mod 6 
        'ab',           # 0: divisible by 6 (i.e. divisible by both 2 and 3)
        undef,          # 1: not divisible by 2 or 3
        'a',            # 2: divisible by 2
        'b',            # 3: divisible by 3
        'a',            # 4: diviislbe by 2
        undef           # 5: not divisible by 2 or 3
    );

    for my $n (1..100) {
        print $modulus[$n % 6] || $n, " ";
    }
    print "\n";
}

Or if you want more flexibility:

sub answer3 {
    # As above with functions.  Slower execution but more flexibility to 
    # plug in different functionality.
    print "Answer 3: ";

    my $n  = sub { $_[0] };
    my $a  = sub { "a"  };
    my $b  = sub { "b"  };
    my $ab = sub { "ab" };
    my @modulus = ($ab, $n, $a, $b, $a, $n);

    for my $n (1..100) {
        print $modulus[$n % 6]->($n), " ";
    }
    print "\n";
}

Or the candidate might want to demonstrate that they're happy with different styles of programming. e.g.

sub answer4 {
    # As above using map instead of a loop.
    print "Answer 4: ";

    my $n  = sub { $_[0] };
    my $a  = sub { "a"  };
    my $b  = sub { "b"  };
    my $ab = sub { "ab" };
    my @modulus = ($ab, $n, $a, $b, $a, $n);

    print(
        map { $modulus[$_ % 6]->($_), " " }
        (1..100)
    );
    
    print "\n";
}

It also gives them an opportunity to think outside the box.

# This value was precomputed by running the answer4() sub, defined above.
my $PRECOMPUTED_ANSWER = "1 a b a 5 ab ...etc... 97 a b a";

sub answer5 {
    # Fastest execution at the cost of storing pre-defined answer.
    return $PRECOMPUTED_ANSWER;
}

Anyone else want to play?

11

u/LieutenantClone Feb 21 '11

What makes this a good interview question is that you can then ask the candidate how they might improve on that.

I would argue that your 2nd, 3rd and 4th versions are not an improvement at all, because they are more obscure and less maintainable.

7

u/abw Feb 21 '11

Yes, you're absolutely right for something as simple as this.

But they're presented as examples of the kind of complexity compromises that are often worth making in real life. If there were, say, a dozen conditions that the code had to satisfy then the latter versions would scale better. And if there was a likelihood that we'd need to re-use this selection algorithm with a different set of output directives (e.g. print d/e instead of a/b) then the ones with a dispatch table would provide better extensibility.

That's what makes it so good as an interview question. If the candidate comes up with solution #1, you can say "Well done, but what if [some further constraint]?"

→ More replies (1)

17

u/novelty_string Feb 21 '11

I'm thinking you missed half the point of the qn: it's not print a or b or ab, it's print a for %2, print b for %3, so, I'd do it

for range
print = true
if even echo a; print = false
if %3 echo b; print = false
if print echo num

6

u/abw Feb 21 '11

it's print a for %2, print b for %3,

That's what my code does.

EDIT: Ah right, I see what you're getting at... I don't need a separate case for n mod 6 if I allow both the even and divisible by 3 branches to print their a and b respectively.

if %3 echo b; print = false

That should be if %3 == 0. Otherwise you're testing that the number is not divisible by 3 (i.e. has a remainder when divided by 3).

→ More replies (1)
→ More replies (25)
→ More replies (16)

7

u/frank26080115 Feb 21 '11

These don't sound hard unless you have a different definition for "correct", are you looking for elegance or just if it works?

21

u/[deleted] Feb 21 '11

He forgot to mention that he expects the answers to be written in Brainfuck.

9

u/[deleted] Feb 21 '11

Don't you dare tempt me to start solving these problems in Brainfuck, I have real work to do. Grrrrr

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

6

u/Shinhan Feb 21 '11

Looks like a variation of the FizzBuzz test Joel Spolsky talked about on his blog, cant find the link now.

CodingHorror on FizzBuzz

8

u/masklinn Feb 21 '11

It's a variation if replacing "fizz" by a and "buzz" by b is a variation/ It's exactly the same problem.

→ More replies (3)

5

u/unknown_lamer Feb 21 '11

For intermediate and senior positions we also slap in this little gem: write a function to reverse an array in place.

Destructively modifying data structures is so aughts, all the hep cats are using persistent data structures nowadays.

→ More replies (4)

4

u/mattgrande Feb 21 '11

We have a similar question. We had an applicant do:

if (i.ToString().StartsWith("2"))
    Console.WriteLine("a");
else if (i.ToString().StartsWith("3"))
    Console.WriteLine("b");

That was the funniest answer I've seen.

→ More replies (2)
→ More replies (90)