Keep Calm and Code On

If you give in to pressure, it will impede you from writing good Software.  Many times, as Software Developers, we find ourselves in challenging situations and the responses are flight or fight; this time though, I decided to stay and face the challenge head-on. Ever had to complete a task in a limited space of time? And of course, the answer is yes.

Here is my recent experience where I was asked to create an algorithm that singles out a unique prime number within a range of two numbers. Don't get me wrong, It's not the hardest task ever, I knew I could do it but, the devil was in the detail.

The Problem

A unique prime number is selected by the position it belongs to. For example, if the prime numbers are within the range of 1 - 10, what number is in position 4? Got it? To expound further, prime numbers between 1 & 10 are 1, 2, 3, 5, 7 and the number of position 4 is 5.

My Approach

Firstly, I created a method to return a List of all prime numbers between the given range. Here's an example in Java.

List<Integer> getPrimeListBetweenTwoNumbers(int firstNumber, int secondNumber) {
        List<Integer> primeList = new ArrayList<>();
        for (int i = Math.min(firstNumber, secondNumber); i < Math.max(firstNumber, secondNumber); ++i) {
            Boolean isPrime = true;
            for (int j = 2; j < i; ++j) {
                if (i % j == 0) {
                    isPrime = false;
            if (isPrime) primeList.add(i);
        return primeList;

In order to find the unique number for the given position, I called the method above, passed the given range as the argument and then called the List get() method which takes the position as the argument.

The First Test

int position = 4;
System.out.print(getPrimeListBetweenTwoNumbers(1, 10).get(position));

Output: 7

Clearly, the output is incorrect, it should have printed out 5. Hmm, looks like there is a bug somewhere in the code.

The Trouble Begins

This is the part where I ran out of the set time, trying to figure out if I had a bug or my approach was wrong. To verify, I printed out the list of prime numbers to see if I'm getting the expected values.

System.out.print(getPrimeListBetweenTwoNumbers(1, 10));

Output: [1, 2, 3, 5, 7]

Looks okay to me, but time was going...

In my head, the method was right, but the output was the unexpected result. At this point, pressure took over and I stopped paying attention to detail. I looked over and over the code again, still couldn't find the problem — I was focusing on the wrong stuff.

The Aftermath

A few minutes later, after the scene was over I went back to review my code — I found the bug! The problem was an easy fix, but during the challenge, it was cryptic because I gave in to pressure. I just wasn't at ease enough to think straight. I snubbed a very important detail, and if you're thinking about how indexing works — I'm of the same judgment now.

The Fix

Since I've finally found the bug, the given position should have been altered in regards to the customary way of counting as opposed to how indexing works in programming. My improvement, therefore, was to modify the given position by subtracting one from it.

int position = 4;
System.out.print(getPrimeListBetweenTwoNumbers(1, 10).get(--position));

Output: 5

Now I have the correct output — happy days.

Moving Forward

The lesson learned here is: Keep calm and code on! Always pay attention to every detail when solving any problem. Most times, if not all, we aren't so far away from the solution.

What about you, how do you deal with pressure when coding?