Monthly Archives: November 2007

The Pros and Cons of Being a Contractor

During my job hunting process, I have received many offers for interview from contracting agencies such as Volt and Excell. Since I have never been working as a contractor before, I needed to do some research to see what the pros and cons of being a contractor are. Here are what I found out.
Pros

  1. Money. Contractor might earn more since most of them get paid on an hourly basis. That also means overtime pay.
  2. Job variations. Contract might change job frequently, contracts are typically short (6 months to a year).
  3. Tax Benefit. There might be a tax benefit, especially if you have your own business.

 Cons

  1. Contract tied up. If an opportunity comes along, a contractor might not be able to pursuit it without risking breaking the current contract.
  2. Too expensive to be hired. Employers feel reluctant to hire a contractor for fulltime job for fear that he/she might quit soon to pursuit higher pay positions.
  3. Day off benefits. Depend on the agency, contractors might not have the benefits of sick day or holiday.
  4. From my own experience: Contractors and fulltime employees are treated differently. For example, if the manager decided that the whole group should go see a movie; it means “the whole group minus contractors”.
  5. Health benefits generally are not as good as those offered to fulltime employees.
  6. Usually, 401k are not matched
  7. Not permanent. Duh!

References

Mueller, Carl. Contract Work: The Pros and Cons of Being a Contractor. [Link]

Legless. IT Contractor – pros and cons of a contractor’s life [Link]

Niznik, Steven (1999). About Working as a Indenpendent Contractor [Link]

Bourque, Holly (2007). The Pros and Cons of Working as a Contractor. [Link]

Windows XP/Vista Auto-completion Mystery

I have an XP and a Vista box. On the XP box, the auto-completion works beautifully: it automatically suggests the name of the files or directory based on the first few characters that I typed. Auto-completion works in places such as the file open/save dialogs, the Run command, and the Internet Explorer’s web address line. On my Vista box, auto-completion does not seem to work. After some research, I finally came to this thread which basically told me to turn on a setting in Internet Explorer. Here is how:

  1. Start Internet Explorer (IE)
  2. Click the Internet Options menu off the Tools Menu
  3. Click the Advanced tab
  4. Click to select the Use inline AutoComplete check box
  5. Click OK to commit the change

That’s all there to it. Perhaps Microsoft can re-think the situation and put the settings somewhere in the control panel instead of IE.

Java Exceptions List

Sometimes when I write Java code and need to throw an exception, I vaguely remember the exception’s name, but not to the exact spelling. For that, I am compiling this on-going list of exceptions to remind myself.

UPDATE 2013-09-25:

After years of using WordPress to maintain the list, I am moving the list over to a Git repository at BitBucket.org to improve my workflow. You can find that list here.

Colorize Your Code for Free Online

As I started to blog about programming, I run into the frustration of seeing my source code get munched because of symbols such as < or >. Surrounding my code with pre block does not help. After some research, I found help in the form of the Code Colorizer.

The first thing I love about this online tool is there is nothing to install (and probably cross-platform, too). Secondly, the output is simply gorgeous. Did I mention that is it free?

Using Code Colorizer is simple, the application spells out the steps for you. After generating the output, I simply select the third option near the end of the page which said, “Colorize source code (partial HTML block)”, then cut-and-paste the result into my blog. It can’t be easier than this. I just love it. If you need to publish your code online, check out the Code Colorizer.

How to Reverse Words within a sentence in C++

This is yet another popular interview question: given a string, for example, “Ask, and you might get it.”, write a C/C++ function to reverse the words so that it becomes: “it. get might you and Ask,”. Since I have documented my source code fairly extensively, you can read the source code and see how to do it: ReverseWords.txt

Update: I have tested ReverseWords.txt (after renaming it to .cpp) under Mac OS X 10.4.11 and verified that it worked. However, some one has reported crashing when building and running under Visual Studio 2005. For that, I submitted my vs2005 project and hope that the same person can help me to verify the correctness of my code. I appreciate your help. Click here for the vs2005 project.

How to Print a Long Integer Using Only putchar()

How to Print a Long Integer Using Only putchar()

Here is a popular interview question: write a C/C++ function which outputs a long integer to the console:

void putlong(long number);

To make it harder, the interview often specifies the following restrictions:

  1. You can only use putchar(char c) as your only mean of outputing to the console
  2. Do not create a string buffer to store the digits (this is necessary to reverse the order of the digits)
  3. No stack to reverse the digits
  4. No recursion allowed

Before writing any code, let’s take a look at a simple example for number = 123. In the table below, we repeatedly extracting the last digit from the number using the modulus operator % and reduce the number by dividing it by 10.

Operation Result Note
123 % 10 3 This gives the third/last digit
123 / 10 12 The gives the remaining digits
12 % 10 2 This gives the second digit
12 / 10 1 This gives the remaining digits
1 % 10 1 This gives the first digit
1 / 10 0 This is when we stop

The problem with this approach is the extraction is in reverse order of what we should be printing out. There are several ways to address this issue. We can push the digits onto a stack and retrieve them in reverse order. However, restriction #3 prevents us from doing so. We can also use a char[] buffer to store the digits and later on output them in reverse order. As you guessed it, restriction #2 makes sure this will never happend. Restriction #4 also prevent us to use recursion as a way to print the digits in reverse order. So what’s left?

There are several solutions, but here is one that I devised: if we can reverse the digits in a number, then we can print them out in correct order. In the previous example where number == 123, if we can somehow reverse it so that number == 321 then we effectively solved the problem.

It turns out the reversing a number is not that hard, but we need to be aware of cases when overflow might occur. For example, on some machine, the range for long is -2147483648 to 2147483647. If we reverse the number near the limits, we get an overlow error. To remedy this problem, we can save off the last digit of the number, then reduce it by a factor of 10. This way, we can ensure that overflow will not happen. The code for saving the last digit and reducing the number looks like this:

char lastDigit = (char)((number % 10) + ‘0’); // Extract the last digit

number /= 10; // reduce the number

One last problem we need to address: the negative sign. At first, we might attempt to do something like:

if (number < 0)

{

putchar(‘-‘);

number *= -1;

}

However, it is not that simple. Recall that the range for long is -2147483648 to 2147483647. When number == -2147483648, changing sign the way we just did will produce an overflow. Thus, we will need to treat negative case with more care:

if (number < 0)
{

putchar(‘-‘);
lastDigit = (char)(‘0’ – (number % 10));
number /= -10;

}
else
{

lastDigit = (char)((number % 10) + ‘0’);
number /= 10;

}

Now that we save the last digit and reduce the number to prevent overflow, the fun begin. Our next task is to reverse the digits:

long reversed = 0;
while (number > 0)
{
reversed = reversed * 10 + (number % 10);
number /= 10;
}

Next, we will print out the digits of reverse:

while (reversed > 0)
{
char c = (char)((reversed % 10) + ‘0’);
putchar(c); // print it
reversed /= 10; // reduce the number
}

putchar(lastDigit); // don’t forget this

That’s all there to it. Below is the complete code in C, which also works in C++:


// putlong.c : Defines the entry point for the console application.// 
#include <stdio.h>
#include <limits.h>

void putlong(long number)
{
    char lastDigit;
    long reversed;

    // Extract the last digit and reduced the number by 10 to
    // avoid overflow. Also take care of the negative sign
    if (number < 0)
    {
        putchar('-');
        lastDigit = (char)('0' - (number % 10));
        number /= -10;
    }
    else
    {
        lastDigit = (char)((number % 10) + '0');
        number /= 10;
    }

    // Reverse the number
    reversed = 0;
    while (number > 0)
    {
        reversed = reversed * 10 + (number % 10);
        number /= 10;
    }

    // Now, output the number using only putchar()
    while (reversed > 0)
    {
        char c = (char)((reversed % 10) + '0');
        putchar(c);
        reversed /= 10;
    }
    putchar(lastDigit);
}

int main()
{
    putlong(0);
    putchar('\n');

    putlong(123456);
    putchar('\n');

    putlong(-123);
    putchar('\n');

    putlong(LONG_MAX);
    putchar('\n');

    putlong(LONG_MIN);
    putchar('\n');

    return 0;
}

If you have any comment or question, please post them at my blog.

Blogged with Flock