How would you answer Joel's sample programming questions?

Posted by Khorkrak on Stack Overflow See other posts from Stack Overflow or by Khorkrak
Published on 2010-05-28T19:43:42Z Indexed on 2010/05/28 19:52 UTC
Read the original article Hit count: 466

I recently interviewed a candidate for a new position here. I wish though that I'd read Joel's Guerrilla Guide to Interviewing prior to that interview - naturally I happened upon it the night afterwards :P http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html

So I tried answering the easy questions myself - yeah I used the python interpreter to type stuff in and tested the results a bit - I didn't look up any solutions beforehand though and I also thought about how long it took me to come up with answers for each one and what I'd look for the next time I interview someone. I'd let them type stuff into the interpreter and see how did used python's introspection capabilities too to find out things like what's the re module's method for building a regex etc.

Here are my answers - these are in python of course - what are yours in your favourite language? Do you see any issues with the answers I came up with - i.e. how could they be improved upon - what did I miss?

Joel's example questions:

Write a function that determines if a string starts with an upper-case letter A-Z.

import re
upper_regex = re.compile("^[A-Z]")

def starts_with_upper(text):
    return upper_regex.match(text) is not None

Write a function that determines the area of a circle given the radius.

from math import pi

def area(radius):
    return pi * radius**2

Add up all the values in an array.

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

Harder Question:

Write an example of a recursive function - so how about the classic factorial one:

def factorial(num):
    if num > 1:
        return num * factorial(num - 1)
    else:
        return 1

© Stack Overflow or respective owner

Related posts about interview-questions

Related posts about Interviewing