Are simple interview tests really simple?

Introduction

I was listening to a developer podcast several weeks ago and remember the podcast host discussing how to identify a proficient programmer.

He advocated giving an applicant programmer a programming test in which the test was to identify if a number was a prime number.

So what’s a prime number?

A prime number is a number that cannot be divided by a number other than 1 and itself. By Euclid’s theorem, there is an infinite number of prime numbers. Subsets of the prime numbers may be generated with various formulas for primes.

My Failure

As I listened to the challenge, I immediately thought to myself. I wonder how long it would take me to write the logic to identify prime numbers.

So I decided to write some code to do it.

Embarrassingly I spent about 15 minutes flushing out the logic.

Does that make me a bad programmer?

I mean seriously, what’s so hard about writing a function to identify if it’s a prime number or not?

Well the answer is not about the actual code that solves the problem.

Instead, I believe the answer is the procedures used to identify the code.

SDET-it

I don’t think this question is geared towards programmers more than it’s geared towards SDETs.

An SDET is a special breed of developer that resides on the Quality Assurance side of the house that focuses on breaking software programmatically rather than manually.

They are developers that tend to focus more on writing code to test sad paths of requirements than they do happy paths.

In addition SDET’s focus on writing code that tests code versus just writing code to a spec.

When I put on my developer hat, I failed the test. Meaning, I spun my wheels by writing code and then deleting some of it and than rewriting it and deleting it again. For such a simple problem, I struggled to spit out a clean solution that I could be proud of.

I then put on my SDET hat and things became enjoyable. I applied TDD with edge-cases and things just fell into place.

The following solution accounts for zero and negative numbers as inputs.

My Solution

Logic:

using System;
using System.Collections.Generic;
using MoreLinq;

namespace TestLogic
{
    public static class HelperMethod
    {
        public static bool IsPrime(this int value)
        {
            var fundamentalPrimes = new List<int>() { 1, 2, 3, 5, 7 };
            var absoluteValue = Math.Abs(value);

            if (fundamentalPrimes.Contains(absoluteValue)) return true;

            var isPrime = true;
            fundamentalPrimes.Remove(1);
            fundamentalPrimes.ForEach(p =>
                {
                    if (absoluteValue != 1 &&
                        absoluteValue % Math.Abs(p) == 0)
                    {
                        isPrime = false;
                        return;
                    }
                });

            return isPrime;
        }
    }
}

Tests:

using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestLogic;

[TestClass]
public class Test1
{
    [TestMethod]
    public void zero_is_not_prime()
    {
        // Setup
        int number = 0;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(!isPrime);
    }

    [TestMethod]
    public void one_is_prime()
    {
        // Setup
        int number = 1;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void negative_1_is_prime()
    {
        // Setup
        int number = -1;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void negative_2_is_prime()
    {
        // Setup
        int number = -2;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void negative_3_is_prime()
    {
        // Setup
        int number = -3;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void negative_4_is_not_prime()
    {
        // Setup
        int number = -4;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(!isPrime);
    }

    [TestMethod]
    public void two_is_prime()
    {
        // Setup
        int number = 2;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void three_is_prime()
    {
        // Setup
        int number = 2;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void four_is_not_prime()
    {
        // Setup
        int number = 4;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(!isPrime);
    }

    [TestMethod]
    public void five_is_prime()
    {
        // Setup
        int number = 5;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void tweentyThree_is_prime()
    {
        // Setup
        int number = 23;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(isPrime);
    }

    [TestMethod]
    public void thirtyTwo_is_not_prime()
    {
        // Setup
        int number = 32;

        // Test
        var isPrime = number.IsPrime();

        // Verify
        Assert.IsTrue(!isPrime);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: