Five programming problems every Software Engineer should be able to solve

True dat. Many a "temporary solution" has been running for years. At least in my previous job I had the mandate to go fix old/crap code. Now I don't touch a damn thing until all necessary formalities has been followed. No one will ever accept a quote and pay for something to be done again, same functionality, but better.

That moment you pick up a potential problem on a client's server, notify your manager and suggest a pro-active fix, and get told 'No, we do nothing until it breaks, that way we can charge more hours.' :wtf:
 
I've interviewed people who can't put together pseudo code on a white board :-( it's like you wanna smack them with their cv that shows several yrs experience
 
I've only started programming this year (First year student, started end of Feb), but thought I would try some of the challenges.
I have not looked at any of the pages except for the first page and have not googled anything.
Would someone be kind enough to check my work?

This code just contains the main:
Code:
package generalpurpose;

import java.util.Arrays;

public class GeneralPurpose {

    public static void main(String[] args) {
        
        Problem1 prob1 = new Problem1();
        System.out.println("These are the numbers: " + Arrays.toString(prob1.listing));
        System.out.println("Sum of the listing (while loop): " + prob1.sumWhileLoop());
        System.out.println("Sum of the listing (for loop)  : " + prob1.sumForLoop());
        System.out.println("Sum of the listing (recursive) : " + prob1.selfCall(0));
        
        Problem2 prob2 = new Problem2();
        System.out.println("Listing for problem 2: " + Arrays.toString(prob2.ProblemTwo()));
        
        Problem3 prob3 = new Problem3();
        System.out.println("The sum of the first 100 Fibonnaci numbers: " + prob3.ProblemThree() );
    }
}
Problem One:
Code:
package generalpurpose;

/*
Write three functions that compute the sum of the numbers in a given list using a:
for-loop, a while-loop, and recursion.
*/

public class Problem1 {
    //Variables used.
    int[] listing = {1,8,14,20};
    private int sum,sumSelfCall;
    
    public int sumWhileLoop(){
        int a = 0;
        while( a < listing.length){
            sum+=listing[a];
            a++;
        }
        return sum;
    }
    
    public int sumForLoop(){
        sum = 0; //resetting sum so no new variable is required
        for(int b = 0; b < listing.length; b++){
            sum+=listing[b];
        }
        return sum;
    }
    
    public int selfCall(int selfCaller){
      
      sumSelfCall = listing[selfCaller] + sumSelfCall; //had to assign a new variable in this case or it would keep resetting itself.
      if(selfCaller < listing.length-1){
          selfCaller++;
          selfCall(selfCaller);
      }
      return sumSelfCall; 
    }
}

Problem Two
Code:
package generalpurpose;

/*
    Write a function that combines two lists by alternatingly taking elements. 
For example: given the two lists [a, b, c] and [1, 2, 3], the function should return [a, 1, b, 2, c, 3].
*/

public class Problem2 {
    
    public String[] ProblemTwo(){
        final String list1[] = {"1","2","3"};
        final String list2[] = {"a","b","c"};
        String list3[] = new String[list1.length + list2.length];
        int b = 0;

        for(int a = 0 ; a < (list1.length); a++){
            list3[b] = list1[a];
            b++;
            list3[b] = list2[a];
            b++;
        }
     
        return list3;
    } 
}

Problem Three:
Code:
package generalpurpose;
/*
Write a function that computes the list of the first 100 Fibonacci numbers. 
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent 
    number is the sum of the previous two. 
As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.
*/

public class Problem3 {
    
    public long ProblemThree(){
        
        int max = 80;
        
        String fibonacci[] = new String[max]; //made this max in order to test more easily. 
        fibonacci[0] = "0";
        fibonacci[1] = "1";
        fibonacci[2] = "1";
        
        long sumOfFibonacci = 0;
        
        for(int count = 3; count < max; count++){
            fibonacci[count] = String.valueOf(Integer.parseInt(fibonacci[count - 2]) + Integer.parseInt(fibonacci[count-1]));
        }
        
        for(int num = 0; num < max; num++){
            sumOfFibonacci+= Long.parseLong(fibonacci[num]);
        }
        
        return sumOfFibonacci;
    }
}

Question 3 might be wrong due to the numbers getting too big. Just googled, seems I can use a BigInt.
 
Last edited:
I've only started programming this year (First year student, started end of Feb), but thought I would try some of the challenges.
I have not looked at any of the pages except for the first page and have not googled anything.
Would someone be kind enough to check my work?

Question 3 might be wrong due to the numbers getting too big. Just googled, seems I can use a BigInt.

Don't use recursion for Fibonacci.
 
My solution to number 4:
Code:
def large( nums ):
  return ''.join(sorted([str(n)for n in nums],cmp=lambda x,y:int(y+x)-int(x+y)))

Ruby version

Code:
[2,1,901,92].map(&:to_s).sort.reverse.join.to_i
 
Don't use recursion for Fibonacci.

recursion = call itself right? So you are complaining about the return?

The return I am using in that question is just so that it prints out in the main string args, wanted to keep each problem in a separate class.
Everything is done in those two for loops (I could combine it into one, but I find it neater this way.)
 
recursion = call itself right? So you are complaining about the return?

The return I am using in that question is just so that it prints out in the main string args, wanted to keep each problem in a separate class.
Everything is done in those two for loops (I could combine it into one, but I find it neater this way.)

No, he is saying there is a far more efficient (cpu tick / execution time wise) method. It does however entail some relatively advanced mathematics.
 
Last edited:
No, he is saying there is a far more efficient (cpu tick / execution time wise) method. It does however entail some relatively advanced mathematics.

Square roots and the golden ratio which is a constant. Not very advanced. Unless you're referring to cguy's matrix method.
 
They just reinvented fizzbuzz : http://rosettacode.org/wiki/FizzBuzz

There's also a fizzbuzz 2 which is even simpler, you just have to print numbers from 0 to 100, but you must start with for example: for(int i=100;
Then complete the rest.
 
Top
Sign up to the MyBroadband newsletter
X