code jam

Christian Harms's picture

facebook Hacker Cup 2012 Qualification Round

The facebook hacker cup is a google-code-jam like coding contest - starting with the 2012 qualification round this weekend. Try to solve the 2011 practice round. If you can submit your solution (the spinner after submit dont stops) try to check the result response with HTTPFox (it shows the correct / incorrect response) - hoping this will be fixed for this weekend.

All solutions can be testet with codejammer.com - a fine javascript solution for running code contests like facebook hacker cup or google code jam.Read more

Nico Heid's picture

Google Code Jam - Space Emergency

Space Emergency is a nice little puzzle from Google Code Jam 2011.

The solution is quite simple. Fly as long as the boosters are built. Once they are done, reorder your remaining distance. Use the boosters on longest distances. Voila - that's it.

Let’s see the straight forward implementation for the small input.

  1.  for (int casenr = 1; casenr <= testcases; casenr++) {
  2.  
  3.             Integer time = 0;
  4.             Integer boosters = scanner.nextInt();       // L
  5.             long buildTime = scanner.nextLong();      // t
  6.             Integer finalStar = scanner.nextInt();      // N
  7.             Integer stars = scanner.nextInt();          // C
  8.             List<Integer> distances = new ArrayList<Integer>();
Read more

Nico Heid's picture

Google Code Jam - Picking Up Chicks

This is a problem I particularly enjoyed. I had to turn it around a few times in my head before I found the solution.

First I was thinking about simulation the chickens moving before noticing I am going in the totally wrong direction.
Then it dawned to me, that the solution is quite simple.

We can break the solution down into two parts.

  • Is the specific case solvable
  • How many swaps do we need

Is the specific case solvable

Leaving all other aspects aside, just check if enough chickens can make it to the barn in time. If this is not the case, it's an IMPOSSIBLE one to solve.
For this to find out, you only need to get their position, add the speed multiplied by the time you have at hands and see if they make it into safety, also known as the barn.

  1.         int finishers = 0;
  2.         boolean[] isFastEnough = new boolean[startposition.length];
Read more

Nico Heid's picture

Google Code Jam - Minimum Scalar Product

With the Google Code Jam 2011 less than four weeks away, it is time for some finger exercises.

Let's start with the Minimum Scalar Product, which should only take you a few minutes.

Here's the Java version, which will only work for the small input set. I'll explain later.

  1. public static int getMinimumScalarProduct2(int[] x, int[] y) {
  2.  
  3.         Arrays.sort(x);
  4.         Arrays.sort(y);
  5.         int sum=0;
  6.  
  7.         for (int i = 0; i < x.length; i++) {
  8.             sum += x[i] * y[x.length -1 -i];
  9.         }
  10.         return sum;
  11.     }

What we do is sort both arrays, then multiply them. One starting from the beginning, the other from the end. So our biggest numbers are multiplied with the smallest. The math is on Google's solution page, we're just looking at coding here.Read more

Christian Harms's picture

Google code jam solution for alien numbers

Time again for a new google code jam article about alien numbers. This time instead of decoding words from an other alien language we have to convert numbers from one alien digit system to another.Read more

Nico Heid's picture

Google Code Jam - Rotate

It's time for some basic finger exercise. The Google Code Jam Rotate is very trivial, so relax and fire up your IDE.

I was a bit lazy, so there is no reading of the input sets, just a two-dimensional array and two functions

Rotating

As the Google solution pointed out, there is actually no need to really rotate the 2dim array. Just push everything to the right, as if gravity would be to the right. That's the same as rotating everything and keeping gravity towards the bottom. But we save a few lines this way.

So here is the "gravity from the right" code

  1. public static void fakeRotate(char[][] board) {
  2.  
  3.     for (int i = 0; i < N; i++) {
  4.         for (int j = N - 1; j >= 0; j--) {
  5.             if (board[i][j] != '.') {
  6.                 // push to right
  7.                 int m = 1;
  8.                 while ((j + m) < N && board[i][j + m] == '.') {
Read more

Nico Heid's picture

Google Code Jam - Theme Park

Theme Park is another good example of a problem which can be solved with a fairly simple algorithm. The simple version will solve the small input set without a problem, but will run almost infinitely with the large dataset.

You can read the problem on the Google Code Jam site: Theme Park

simple solution

The simple solution is to put the groups in a static array, use a pointer that "wraps around" using modulo and fill the roller coaster until it's so full, that the next group does not fit in, then let it ride earning income accordingly to the seats taken.

This is a correct solution, but unfortunately a bit slow. The input set can have a roller coaster with up to 109 seats and 108 rides and groups as large as 107. Read more

Nico Heid's picture

Google Code Jam - The Snapper Chain

The Google problems are always nice, sometimes even a bit too tricky. This years entry problem was quite nice with some difficulties you could run in.

You need to read the problem first, to follow the article, so here's the link: http://code.google.com/codejam/contest/dashboard?c=433101#s=p0&a=0

If you're a hands on programmer first you might just want implement the chain without looking at the problem more extensively. So you might end up with something like this:

  1.                         // snapping
  2.                         boolean toggle = true;
  3.                         for (int j = 0; j < snaps; j++) {
  4.                                 toggle = true;
  5.                                 for (int k = 0; k < snapperCount; k++) {
  6.                                         if(toggle == false){
  7.                                                 break;
  8.                                         }
  9.                                         if (toggle == true) {
  10.                                                 snapper[k] = !snapper[k];
  11.                                         }
  12.                                         if (snapper[k] == true) {
  13.                                                 toggle = false;
  14.                                         }
  15.  
  16.                                 }
  17.                         }
  18.  
  19.                         // we need power on all snappers
  20.                         boolean power = true;
  21.                         for (int j = 0; j < snapperCount; j++) {
  22.                                 if (snapper[j] == false) {
Read more

Nico Heid's picture

Google Code Jam - Africa Qualification Round 2010

The Google Code Jam is an interesting option to work on some challenging problems.
With the first qualification round coming up at May 7th, it's time to look at the Africa qualification round, to see what to expect.

You have 24 hours to solve three rather simple problems. Solving two problems brings you into the next round.

I will present my Java solutions. The Code Jam site provides the code of every contestant.

Store Credit

read problem description
You just need two nested loops to find the matching articles. Just a finger exercise.

  1. for (int i = 0; i < items.size(); i++) {
  2.                         for (int j = i + 1; j < items.size(); j++) {
  3.  
  4.                                 // is this the right shopping?
  5.                                 int newSum = items.get(i) + items.get(j);
  6.  
  7.                                 if (newSum == credit) {
  8.                                         return "Case #" + caseNr + ": " + (i + 1) + " " + (j + 1);
  9.                                 }
  10.                         }
  11.  
  12. }
Read more

Syndicate content