Bridge crossing (Microsoft Hiring test)

Posted

A good friend of mine send me this quiz which was found in compendium at her university. The course was about Mathematical Programming and from what I understood no one could solve this. I have copied the quiz directly.

The assignments:

Warning, this assignment is rather hard. It should have been used in a Microsoft during hiring interviews.

Four persons have to cross a bridge. The times each needs to cross it are:

  • 5 min
  • 10 min
  • 20 min
  • 25 min.

It is dark and they only have one lamp, we have the following constraints:

  • the bridge can only be crossed if one has a lamp, while it is too dark.
  • 2 persons at most are allowed to be on the bridge at the same time.
  • the speed of a pair is determined by the speed of the slower person.
  • the lamp can last only an hour.
  • the lamp cannot be thrown.

Can you figure out a way so that the four men can cross the bridge in 60 minutes.

Solution

Here is how I solved it. It is a classical problem where you need to move back and forth a couple of times.

Crossing time for each person

A – 5 minutes.
B – 10 minutes.
C – 20 minutes.
D – 25 minutes.

The plan is as the following

Time Start Movement End
0 minutes A B C D
10 minutes C D A & B walks over A B
15 minutes A C D A walks back B
40 minutes A C & D walks over B C D
50 minutes A B B walks back C D
60 minutes A & B walks over A B C D

Done.

Tracking outbound links with Google Analytics

Posted

Recently someone asked me if it was possible to track outbound links on his website with Google Analytics.

Here is a working solutions. Note that you might have to wait 24 hours before it will show up under Events.

It is not a bullet since it depends Google Analytics to registre the event fast enough. You can always tweak the setTimeout value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready(function() {
	$("a").on('click',function(e){
	    var url = $(this).attr("href");
	    if (e.currentTarget.host != window.location.host) {
	        _gaq.push(['_trackEvent', 'Outbound Links', e.currentTarget.host, url, 0]);
	        if (e.metaKey || e.ctrlKey) {
	             var newtab = true;
	        }
	        if (!newtab) {
	             e.preventDefault();
	             setTimeout('document.location = "' + url + '"', 200);
	        }
	    }
	});
});

Problem when uninstalling ASP.NET Web Pages

Posted

I had just recently installed Service Pack 1 for Visual Studio 2010, which broke the Intellisense in the Razor ViewEngine. A quick search on Google showed that other people has had the same issue. For me it came down to uninstalling ASP.NET Web Pages (and ASP.NET MVC). For some reason that turned out to be difficult.

The Event Viewer showed the following error message.

”Error 1722. There is a problem with this Windows Installer package. A program run as part of the setup did not finish as expected. Contact your support personnel or package vendor.”

Application: WebConfigCA.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException
Stack:
at System.Web.Configuration.VirtualDirectoryMapping..ctor(System.Web.VirtualPath, System.String, Boolean, System.String)
at System.Web.Configuration.VirtualDirectoryMapping..ctor(System.String, Boolean)
at WebConfigCA.Program.GetWebConfig(System.String)
at WebConfigCA.Program.AddAssemblyReferences(System.String, System.String[])
at WebConfigCA.Program.Main(System.String[])

A few people around the web suggested the following solution:

1. Remove the trailing backslash from the following registry keys:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\4.0.30319.0\Path
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\ASP.NET\4.0.30319.0\Path

2. Uninstalled the old version of “Microsoft ASP.NET Web Pages”.

3. Add the trailing backslash back to those keys.

This did not work for me.

My Solution

So I fired up ILSpy and opened up WebConfigCA.exe, just to get an idea about why it had thrown an exception. Nothing really stood out, so I created a Console Application Project in Visual Studio and pasted the code. I added the needed references and ran it (when testing, remember elevated permissions) – No exception got thrown. I thought to myself that was strange, so I toke my newly created console app and replaced it with WebConfigCA.exe (It might be a good idea to keep a backup). I ran the uninstaller again and this time it ran fine and I finally got ASP.NET Web Pages uninstalled.

By going through the code again, I see that I did add a line of code (which I did just to test it in VS but forgot to remove, not sure if that was the real trick, but don’t think so).

Anyway, here it is.

1
args = new string[1]{"-U"};

Giving and Taking Advice

Posted

Everyone around us, parents, friends, co-workers, our boss etc. is always giving us advices.

In my experience, I have found that it can be difficult for people to understand the message if they are not open for it, no matter how good your intentions is.

One reason for this could be that people have something more important on their mind, something that worries them or there is lack of basic trust.

A positive emotional connection is a very important aspect. People should feel that they can trust you and that you have their interest at heart.

What ever you do, always give an advice with sincere compassion.

The Fringe Benefits of Failure, and the Importance of Imagination by J.K. Rowling

Posted

I recently stumble upon this speech given by J.K. Rowling at Harvard. She talks about failures (her own) and how it helped her to become the one she is today.

One of the things that I took away with me is the importance of failure. Everybody is so focused on doing everything right, being the best and that failure cannot be an option. We are so afraid of messing up because we believe that doing so will be the end of the world.

However, failing is actually a big part of life and something we should not be afraid of.

Good judgment comes from experience, and experience comes from bad judgment. – Fred Brooks

So if you are trying to accomplish something, don’t be afraid if you will not make it the first time, because chances are that you probably will the second or third time around.

J.K. Rowling on Vimeo
Transcription and video on Hardvard Magazine

Adding Tiger Stripes to Tables with jQuery

Posted

I have been a Prototypejs fan for a long time, but I finally got around to work with jQuery and it is truly an amazing framework.

In the old days when I would want to add stripes to my tables (with a class named tiger), I would have done something like this.

Doing it old school

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
window.onload = function () {
    var i, n, tables, table, trs, tr;
    tables = document.getElementsByTagName("table");
    for (i = 0; (table = tables[i]); i++){
        if (/btigerb/.exec(table.className)){
            var tbodys = table.getElementsByTagName("tbody");
            if (tbodys.length == 1){
                trs = tbodys[0].getElementsByTagName("tr");
            }else{
                trs = table.getElementsByTagName("tr");
            }
            for (n = 0; (tr = trs[n]); n++){
                tr.className += (n % 2 == 0) ? " alternate" : "";
            }
        }
    }
}

With jQuery magic

1
2
3
$(document).ready(function() {
    $("table.tiger tr:even").addClass("alternate");
});

Pretty simple, right?

Clean Code by Robert C. Martin

Posted

The book wastes no time, so right form the start you are introduced to some basic, but important aspect of what quality code is all about.

It starts out with naming conventions, how to right comments, error handling, refactoring, Unit testing and lots more. So the book covers a lot of ground and if you are looking to improve you developer skills, then I highly recommend it.

Another good thing is that, it is very well written and contains a good balance between theory and practice.

How ever, if you have already read books like

Code Complete by Steve McConnell
Refactoring by Martin Fowler etc.
Test Driven Development by Kent Beck

Then you probably wont learn very much from Clean Code.